main (int Parm_Count, char *Parms[]) { long BAUD, DATABITS, STOPBITS, PARITYON, PARITY; char devicename[80]; char debug_flag[80]; int fd, tty, slipin, slipout, res, i, error; int received = 0, port_num,tmp_pcount; uint8_t c; FILE *np; int mode, val; //place for old and new port settings for serial port struct termios oldtio, newtio; int start_flag; got_connection = 0; reply_mode = LAST_CONNECTION; mode = ASCII; BAUD = B115200; DATABITS = CS8; STOPBITS = 0; //STOPBITS = CSTOPB; PARITYON = 0; PARITY = 0; //PARITYON = PARENB; //PARITY = PARODD; if (Parm_Count < 3 || Parm_Count > 6) print_usage (); strcpy (buf, Parms[1]); i = sscanf (buf, "%s", devicename); if (i != 1) print_usage (); //open the device(com port) to be non-blocking (read will return immediately) fd = open (devicename, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { perror (devicename); exit (-1); } strcpy (buf, Parms[2]); i = sscanf (buf, "%d", &port_num); if (i != 1) print_usage (); debug = 0; tmp_pcount=3; //if (Parm_Count > 3) { while(tmp_pcount<Parm_Count) { strcpy (buf, Parms[tmp_pcount]); i = sscanf (buf, "%s", debug_flag); if (i != 1) print_usage (); if (strcmp (debug_flag, "-d") == 0) debug = 1; else if (strcmp (debug_flag, "-s") == 0) debug = 2; else if (strcmp (debug_flag, "-a") == 0) { reply_mode=STATIC_CLIENT; tmp_pcount++; if(tmp_pcount>=Parm_Count) print_usage(); strcpy (reply_address, Parms[tmp_pcount]); } else print_usage (); tmp_pcount++; } if(debug!=2) printf ("opened: %s\n", devicename); tcgetattr (fd, &oldtio); // save current port settings // set new port settings for canonical input processing newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD; newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; newtio.c_lflag = 0; //ICANON; newtio.c_cc[VMIN] = 1; newtio.c_cc[VTIME] = 0; tcflush (fd, TCIFLUSH); tcsetattr (fd, TCSANOW, &newtio); server_open (port_num); start_flag = 0; while (1) { server_non_blocking_rx (fd); res = read (fd, &c, 1); if (res > 0) { if (c == START) mode = SLIP; if (mode == ASCII) if(debug!=2) printf ("%c", c); if (mode == SLIP) { slip_rx (fd); mode = ASCII; } } else usleep(10000); } } //end of main
int main (int argc, char *argv[]) { char devicename[80]; int fd, res; uint8_t c; struct termios oldtio, newtio; int start_flag; got_connection = 0; mode_t mode = ascii; long BAUD = B115200; long DATABITS = CS8; long STOPBITS = 0; // STOPBITS = CSTOPB; long PARITYON = 0; // PARITYON = PARENB; long PARITY = 0; // PARITY = PARODD; unsigned int portNum = 4000; sprintf(devicename, "/dev/ttyUSB0"); int opt; while ((opt = getopt (argc, argv, "hdc:p:")) != -1) { switch (opt) { case 'h': print_usage(); break; case 'd': debug = 1; break; case 'c': strcpy(&devicename[0],optarg); break; case 'p': portNum = atoi(optarg); break; case ':': fprintf(stderr, "Option -%c requires an operand\n", optopt); break; case '?': fprintf (stderr, "Unknown option `-%c'.\n", optopt); return 1; } } // open the device to be non-blocking (read will return immediately) fd = open (devicename, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { perror (devicename); exit (-1); } if(debug) { printf ("opened: %s\n", devicename); } // save current port setting tcgetattr (fd, &oldtio); // set new port settings for canonical input processing newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD; newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; newtio.c_lflag = 0; //ICANON; newtio.c_cc[VMIN] = 1; newtio.c_cc[VTIME] = 0; tcflush (fd, TCIFLUSH); tcsetattr (fd, TCSANOW, &newtio); server_open(portNum); start_flag = 0; while (1) { server_non_blocking_rx(fd); res = read (fd, &c, 1); if (res > 0) { if (c == START) { mode = slip; } if (mode == slip) { slip_rx (fd); mode = ascii; } if (mode == ascii) { if(debug) printf ("%c", c); } } else { usleep(10000); } } }