small reworks, structure, extension
authorNils Faerber <nils.faerber@kernelconcepts.de>
Wed, 23 Mar 2011 15:26:04 +0000 (16:26 +0100)
committerNils Faerber <nils.faerber@kernelconcepts.de>
Wed, 23 Mar 2011 15:26:04 +0000 (16:26 +0100)
lmain.c
uart.c

diff --git a/lmain.c b/lmain.c
index a74ea2f..19ab4a3 100755 (executable)
--- a/lmain.c
+++ b/lmain.c
@@ -64,54 +64,76 @@ unsigned int sbpos = 0;
        free(sbuf);
 }
 
+void print_help(void)
+{
+       fprintf(stderr, "r              -       initial RESET, required after power-on\n");
+       fprintf(stderr, "i              -       C:2 insert permit request\n");
+       fprintf(stderr, "s              -       C10 C/R status request\n");
+       fprintf(stderr, "S              -       C11 sensor status\n");
+       fprintf(stderr, "1              -       Reads ISO #1 and sends data\n");
+       fprintf(stderr, "2              -       Reads ISO #2 and sends data\n");
+       fprintf(stderr, "3              -       Reads ISO #3 and sends data\n");
+       fprintf(stderr, "m              -       Sends the read data in memory by this command\n");
+       fprintf(stderr, "M              -       Sends multi-tracks data in the memory by this command and parameter\n");
+       fprintf(stderr, "q              -       close serial and quite\n");
+       fprintf(stderr, "h              -       this help\n");
+}
+
 int process_user_cmd(char *rxbuf, int len, int ufd)
 {
 int in_cmd = 0;
 
-       if (rxbuf[0] == 'i') {
-               fprintf(stderr, "Init permit request... ");
-               send_command(ufd, "C:2", 3);
-               in_cmd = 1;
-       }
-       if (rxbuf[0] == 'r') {
-               fprintf(stderr, "RESET request... ");
-               send_command(ufd, "C00", 3);
-               in_cmd = 1;
-       }
-       if (rxbuf[0] == 'e') {
-               fprintf(stderr, "Eject request... ");
-               send_command(ufd, "C30", 3);
-               in_cmd = 1;
-       }
-       if (rxbuf[0] == 's') {
-               fprintf(stderr, "Status request... ");
-               send_command(ufd, "C10", 3);
-               in_cmd = 1;
-       }
-       if (rxbuf[0] == 'S') {
-               fprintf(stderr, "Status request... ");
-               send_command(ufd, "C11", 3);
-               in_cmd = 1;
-       }
-       if (rxbuf[0] == '1') {
-               fprintf(stderr, "Status request... ");
-               send_command(ufd, "C61", 3);
-               in_cmd = 1;
-       }
-       if (rxbuf[0] == '2') {
-               fprintf(stderr, "Status request... ");
-               send_command(ufd, "C62", 3);
-               in_cmd = 1;
-       }
-       if (rxbuf[0] == '3') {
-               fprintf(stderr, "Status request... ");
-               send_command(ufd, "C63", 3);
-               in_cmd = 1;
-       }
-
-       if (rxbuf[0] == 'q') {
-               fprintf(stderr, "Exiting...\n");
-               in_cmd = -1;
+       switch (rxbuf[0]) {
+               case 'i':
+                       fprintf(stderr, "Insert permit request... ");
+                       send_command(ufd, "C:2", 3);
+                       in_cmd = 1;
+                       break;
+               case 'r':
+                       fprintf(stderr, "RESET request... ");
+                       send_command(ufd, "C00", 3);
+                       in_cmd = 1;
+                       break;
+               case 'e':
+                       fprintf(stderr, "Eject request... ");
+                       send_command(ufd, "C30", 3);
+                       in_cmd = 1;
+                       break;
+               case 's':
+                       fprintf(stderr, "Status request... ");
+                       send_command(ufd, "C10", 3);
+                       in_cmd = 1;
+                       break;
+               case 'S':
+                       fprintf(stderr, "Status request... ");
+                       send_command(ufd, "C11", 3);
+                       in_cmd = 1;
+                       break;
+               case '1':
+                       fprintf(stderr, "Status request... ");
+                       send_command(ufd, "C61", 3);
+                       in_cmd = 1;
+                       break;
+               case '2':
+                       fprintf(stderr, "Status request... ");
+                       send_command(ufd, "C62", 3);
+                       in_cmd = 1;
+                       break;
+               case '3':
+                       fprintf(stderr, "Status request... ");
+                       send_command(ufd, "C63", 3);
+                       in_cmd = 1;
+                       break;
+               case 'h':
+                       print_help();
+                       in_cmd = 0;
+                       break;
+               case 'q':
+                       fprintf(stderr, "Exiting...\n");
+                       in_cmd = -1;
+                       break;
+               default:
+                       break;
        }
 
        return in_cmd;
@@ -181,23 +203,29 @@ int bpos;
 
 int main(int argc, char **argv)
 {
+char *ser_dev;
 char rxbuf[256];
 int ufd, rxlen=0, rxpos=0, ret;
 int in_cmd = 0;
 fd_set rfds;
 struct timeval tval;
 
-       if (open_uart("/dev/ttyUSB0", B9600) != 0)
+       if (argc > 1)
+               ser_dev = argv[1];
+       else
+               ser_dev = "/dev/ttyS0";
+       if (open_uart(ser_dev, B9600) != 0)
                return -1;
        ufd = uart_get_fd();
        // fprintf(stderr, "UART fd = %d\n", ufd);
+
        FD_ZERO(&rfds);
        FD_SET(ufd, &rfds);
        FD_SET(0, &rfds);
        tval.tv_sec = 0;
        tval.tv_usec = 5000000;
 
-       fprintf(stderr, "OMRON MVFW card reader terminal\nEnter 'h' for help\n");
+       fprintf(stderr, "OMRON MVFW card reader terminal\non serial %s\nEnter 'h' for help\n", ser_dev);
        while (1) {
                ret = select((ufd+1), &rfds, NULL, NULL, &tval);
                if (ret == 0) {
diff --git a/uart.c b/uart.c
index ba08a46..580a7c7 100755 (executable)
--- a/uart.c
+++ b/uart.c
@@ -35,7 +35,7 @@ int open_uart(char *device, speed_t baud)
                device = "/dev/ttyS0";
        fd = open(device, O_RDWR | O_NOCTTY);
        if (fd < 0) {
-               fprintf(stderr, "Can't open serial port: %s (%d)\n", strerror(errno), errno);
+               fprintf(stderr, "Can't open serial port '%s': %s (%d)\n", device, strerror(errno), errno);
                fd = -1;
                return -1;
        }