int main(int argc, char **argv) { int r; int handle; struct termios attr; char * name = argv[0]; char * device = 0; struct stat statbuf; int pid = 0; int speed; setProgName(argv[0]); while (argc > 1) { if (strcasecmp(argv[1], "-version") == 0) { printf("%s\n", VERSION); exit(0); } else if (strcasecmp(argv[1], "-w") == 0) { writeonly = 1; } else if (strcasecmp(argv[1], "-p") == 0) { passthru = 1; } else if (strcasecmp(argv[1], "-r") == 0) { readonly = 1; } else if (strcasecmp(argv[1], "-v") == 0) { verbose = 1; } else if (strcasecmp(argv[1], "-t") == 0 && argc > 2) { argc--; argv++; timeout = strtol(argv[1], 0, 10); logDebug("timeout set to %ld seconds\n", timeout); } else if (strcasecmp(argv[1], "-s") == 0 && argc > 2) { argc--; argv++; speed = strtol(argv[1], 0, 10); switch (speed) { case 38400: baudRate = B38400; break; case 57600: baudRate = B57600; break; case 115200: baudRate = B115200; break; case 230400: baudRate = B230400; break; #ifdef B460800 case 460800: baudRate = B460800; break; #endif #ifdef B921600 case 921600: baudRate = B921600; break; #endif default: baudRate = speed; break; } logDebug("speed set to %d (%d) baud\n", speed, baudRate); } else if (strcasecmp(argv[1], "-d") == 0) { setLogLevel(LOGLEVEL_DEBUG); } else if (strcasecmp(argv[1], "-o") == 0) { outputCommands = 1; } else if (!device) { device = argv[1]; } else { device = 0; break; } argc--; argv++; } if (!device) { fprintf(stderr, "Usage: %s [-w] -[-p] [-r] [-v] [-d] [-s <n>] [-t <n>] device\n" "\n" "Options:\n" " -w writeonly mode, no data is read from device\n" " -r readonly mode, no data is sent to device\n" " -p passthru mode, data on stdin is sent to stdout but not to device\n" " -v verbose\n" " -d debug\n" " -s <n> set baudrate to 38400, 57600, 115200, 230400" #ifdef B460800 ", 460800" #endif #ifdef B921600 ", 921600" #endif "\n" " -t <n> timeout, if no message is received after <n> seconds the program quits\n" " -o output commands sent to stdin to the stdout \n" " <device> can be a serial device, a normal file containing a raw log,\n" " or the address of a TCP server in the format tcp://<host>[:<port>]\n" "\n" " Examples: %s /dev/ttyUSB0\n" " %s tcp://192.168.1.1:10001\n" "\n" COPYRIGHT, name, name, name); exit(1); } retry: logDebug("Opening %s\n", device); if (strncmp(device, "tcp:", STRSIZE("tcp:")) == 0) { handle = open_socket_stream(device); logDebug("socket = %d\n", handle); isFile = true; if (handle < 0) { fprintf(stderr, "Cannot open NGT-1-A TCP stream %s\n", device); exit(1); } } else { handle = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK); logDebug("fd = %d\n", handle); if (handle < 0) { logAbort("Cannot open NGT-1-A device %s\n", device); } if (fstat(handle, &statbuf) < 0) { logAbort("Cannot determine device %s\n", device); } isFile = S_ISREG(statbuf.st_mode); } if (isFile) { logInfo("Device is a normal file, do not set the attributes.\n"); } else { logDebug("Device is a serial port, set the attributes.\n"); memset(&attr, 0, sizeof(attr)); if (cfsetspeed(&attr, baudRate) < 0) { logAbort("Could not set baudrate %d\n", speed); } attr.c_cflag |= CS8 | CLOCAL | CREAD; attr.c_iflag |= IGNPAR; attr.c_cc[VMIN] = 1; attr.c_cc[VTIME] = 0; tcflush(handle, TCIFLUSH); tcsetattr(handle, TCSANOW, &attr); logDebug("Device is a serial port, send the startup sequence.\n"); writeMessage(handle, NGT_MSG_SEND, NGT_STARTUP_SEQ, sizeof(NGT_STARTUP_SEQ)); sleep(2); } for (;;) { unsigned char msg[BUFFER_SIZE]; size_t msgLen; int r = isReady(writeonly ? INVALID_SOCKET : handle, readonly ? INVALID_SOCKET : 0, INVALID_SOCKET, timeout); if ((r & FD1_ReadReady) > 0) { if (!readNGT1(handle)) { break; } } if ((r & FD2_ReadReady) > 0) { if (!readIn(msg, sizeof(msg))) { break; } if (!passthru) { parseAndWriteIn(handle, msg); } if (outputCommands) { fprintf(stdout, "%s", msg); fflush(stdout); } } else if (writeonly) { break; } } close(handle); return 0; }
int main(int argc, char ** argv) { int r; int handle; struct termios attr; char * name = argv[0]; char * device = 0; struct stat statbuf; int pid = 0; setProgName(argv[0]); while (argc > 1) { if (strcasecmp(argv[1], "-w") == 0) { writeonly = 1; } else if (strcasecmp(argv[1], "-p") == 0) { passthru = 1; } else if (strcasecmp(argv[1], "-r") == 0) { readonly = 1; } else if (strcasecmp(argv[1], "-v") == 0) { verbose = 1; } else if (strcasecmp(argv[1], "-t") == 0 && argc > 2) { argc--; argv++; timeout = strtol(argv[1], 0, 10); logDebug("timeout set to %ld seconds\n", timeout); } else if (strcasecmp(argv[1], "-d") == 0) { debug = 1; setLogLevel(LOGLEVEL_DEBUG); } else if (!device) { device = argv[1]; } else { device = 0; break; } argc--; argv++; } if (!device) { fprintf(stderr, "Usage: %s [-w] -[-p] [-r] [-v] [-d] [-t <n>] device\n" "\n" "Options:\n" " -w writeonly mode, no data is read from device\n" " -r readonly mode, no data is sent to device\n" " -p passthru mode, data on stdin is sent to stdout but not to device\n" " -v verbose\n" " -d debug\n" " -t <n> timeout, if no message is received after <n> seconds the program quits\n" " <device> can be a serial device, a normal file containing a raw log,\n" " or the address of a TCP server in the format tcp://<host>[:<port>]\n" "\n" " Examples: %s /dev/ttyUSB0\n" " %s tcp://192.168.1.1:10001\n" "\n" COPYRIGHT, name, name, name); exit(1); } retry: if (debug) fprintf(stderr, "Opening %s\n", device); if (strncmp(device, "tcp:", STRSIZE("tcp:")) == 0) { handle = open_socket_stream(device); if (debug) fprintf(stderr, "socket = %d\n", handle); isFile = true; if (handle < 0) { fprintf(stderr, "Cannot open NGT-1-A TCP stream %s\n", device); exit(1); } } else { handle = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK); if (debug) fprintf(stderr, "fd = %d\n", handle); if (handle < 0) { fprintf(stderr, "Cannot open NGT-1-A device %s\n", device); exit(1); } if (fstat(handle, &statbuf) < 0) { fprintf(stderr, "Cannot determine device %s\n", device); exit(1); } isFile = S_ISREG(statbuf.st_mode); } if (isFile) { if (debug) fprintf(stderr, "Device is a normal file, do not set the attributes.\n"); } else { if (debug) fprintf(stderr, "Device is a serial port, set the attributes.\n"); memset(&attr, 0, sizeof(attr)); cfsetispeed(&attr, B115200); cfsetospeed(&attr, B115200); attr.c_cflag |= CS8 | CLOCAL | CREAD; attr.c_iflag |= IGNPAR; attr.c_cc[VMIN] = 1; attr.c_cc[VTIME] = 0; tcflush(handle, TCIFLUSH); tcsetattr(handle, TCSANOW, &attr); if (debug) fprintf(stderr, "Device is a serial port, send the startup sequence.\n"); writeMessage(handle, NGT_MSG_SEND, NGT_STARTUP_SEQ, sizeof(NGT_STARTUP_SEQ)); sleep(2); } for (;;) { unsigned char msg[BUFFER_SIZE]; size_t msgLen; enum ReadyDescriptor r; r = isready(writeonly ? -1 : handle, readonly ? -1 : 0); if ((r & FD1_Ready) > 0) { if (!readNGT1(handle)) { break; } } if ((r & FD2_Ready) > 0) { if (!readIn(msg, sizeof(msg))) { break; } if (!passthru) { parseAndWriteIn(handle, msg); } fprintf(stdout, "%s", msg); fflush(stdout); } else if (writeonly) { break; } } close(handle); return 0; }