00001
00002
00003 #define __GNU_SOURCE
00004 #include <getopt.h>
00005
00006
00007 static char __attribute__((unused)) ident[] = "$Id: ttytest.cc,v 4.33 2001/10/11 13:37:09 wshackle Exp $";
00008
00009
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <unistd.h>
00014 #include "ttyintf.hh"
00015 #include <ctype.h>
00016
00017 rcs_serial_port_setting settings;
00018 char *ttyDevName;
00019 int verbosity=0;
00020 char *endline_str = "\n";
00021
00022 static void
00023 printCmdLineHelp(const char *progname)
00024 {
00025 printf("ttytest: \n");
00026 printf("Usage: [Options] ttydevice \n",progname);
00027 printf("Options:\n");
00028 printf("\t-h \t--help \tPrint this help message.\n");
00029 printf("\t-V \t--version \tPrint version info.\n");
00030 printf("\t-v \t--verbose \tMore verbose output.\n");
00031 printf("\t-b [baudrate] \t--baudrate [baudrate] \tSet baudrate(default=9600)\n");
00032 printf("\t-p (e/o/n) \t--parity (e/o/n) \tSet parity to even odd or none(default=n)\n");
00033 printf("\t-d [dbits] \t--data [dbits] \tSet the number of data bits.\n");
00034 printf("\t-s [sbits] \t--stop [sbits] \tSet the number of stop bits.\n");
00035 printf("\t-e (r/n/rn/nr) \t--endline (r/n/rn/nr) \tSet the string new-line character is replaced with. r = \\r n= \\n \n");
00036 printf("\nReport bugs to shackle@nist.gov\n");
00037 }
00038
00039 static void
00040 parseCmdLineArgs(int argc, char **argv)
00041 {
00042 int opt=0;
00043 int option_index;
00044 static struct option long_options[] =
00045 {
00046 {"verbose", 0, 0, 'v' },
00047 {"version", 0, 0, 'V' },
00048 {"baudrate", 1, 0, 'b' },
00049 {"data", 1, 0, 'd' },
00050 {"stop", 1, 0, 's' },
00051 {"endlline", 1, 0, 'e' },
00052 {"parity", 1, 0, 'p' },
00053 {"help", 0, 0, 'h'},
00054 {0, 0, 0, 0}
00055 };
00056 while (1)
00057 {
00058
00059 opt = getopt_long (argc, argv, "vVb:d:s:e:p:h",
00060 long_options, &option_index);
00061 if (opt == -1)
00062 break;
00063
00064 switch (opt)
00065 {
00066 default:
00067 printf("Invalid option !!!\n");
00068 printCmdLineHelp(argv[0]);
00069 exit(-1);
00070 break;
00071
00072 case 'h':
00073 printCmdLineHelp(argv[0]);
00074 exit(0);
00075 break;
00076
00077 case 'v':
00078 verbosity++;
00079 break;
00080
00081
00082 case 'V':
00083 printf("EMC ECI server %s ("__DATE__") \n", ident);
00084 exit(0);
00085 break;
00086
00087 case 'b':
00088 if(optarg)
00089 {
00090 settings.baud_rate = strtol(optarg,0,0);
00091 }
00092 else
00093 {
00094 printf("Invalid arguments!!");
00095 printCmdLineHelp(argv[0]);
00096 exit(-1);
00097 }
00098 break;
00099
00100
00101 case 's':
00102 if(optarg)
00103 {
00104 settings.stop_bits = strtol(optarg,0,0);
00105 }
00106 else
00107 {
00108 printf("Invalid arguments!!");
00109 printCmdLineHelp(argv[0]);
00110 exit(-1);
00111 }
00112 break;
00113
00114 case 'd':
00115 if(optarg)
00116 {
00117 settings.data_bits = strtol(optarg,0,0);
00118 }
00119 else
00120 {
00121 printf("Invalid arguments!!");
00122 printCmdLineHelp(argv[0]);
00123 exit(-1);
00124 }
00125 break;
00126
00127 case 'e':
00128 if(optarg)
00129 {
00130 if(!strcmp("n",optarg))
00131 {
00132 endline_str="\n";
00133 }
00134 else if(!strcmp("r",optarg))
00135 {
00136 endline_str="\r";
00137 }
00138 else if(!strcmp("rn",optarg))
00139 {
00140 endline_str="\r\n";
00141 }
00142 else if(!strcmp("nr",optarg))
00143 {
00144 endline_str="\n\r";
00145 }
00146 else
00147 {
00148 printf("Invalid arguments!!");
00149 printCmdLineHelp(argv[0]);
00150 exit(-1);
00151 }
00152 }
00153 else
00154 {
00155 printf("Invalid arguments!!");
00156 printCmdLineHelp(argv[0]);
00157 exit(-1);
00158 }
00159 break;
00160
00161 case 'p':
00162 if(optarg)
00163 {
00164 switch(optarg[0])
00165 {
00166 case 'e':
00167 case 'E':
00168 settings.use_parity = 1;
00169 settings.even_parity = 1;
00170 break;
00171
00172 case 'o':
00173 case '0':
00174 settings.use_parity = 1;
00175 settings.even_parity = 0;
00176 break;
00177
00178 case 'n':
00179 case 'N':
00180 settings.use_parity = 0;
00181 settings.even_parity = 0;
00182 break;
00183
00184 default:
00185 printf("Invalid arguments!!");
00186 printCmdLineHelp(argv[0]);
00187 exit(-1);
00188 }
00189 }
00190 else
00191 {
00192 printf("Invalid arguments!!");
00193 printCmdLineHelp(argv[0]);
00194 exit(-1);
00195 }
00196 break;
00197 }
00198 }
00199
00200
00201 if(optind != argc-1)
00202 {
00203 printf("Invalid option !!! ");
00204 for(int i = optind>0?optind:0; i< argc; i++)
00205 {
00206 printf("%s ",argv[i]);
00207 }
00208 printf("\n");
00209 printCmdLineHelp(argv[0]);
00210 exit(-1);
00211 }
00212 ttyDevName =strdup(argv[optind]);
00213 }
00214
00215 static const char *
00216 convertToGraph(const char *in, char *out, int max)
00217 {
00218 if(0 == in || 0 == out)
00219 {
00220 return 0;
00221 }
00222 const char *iptr = in;
00223 char *optr = out;
00224 const char * const i_end = in+max;
00225 const char * const o_end = out+max;
00226 while( iptr < i_end)
00227 {
00228 char ci = *iptr;
00229 if(isgraph(ci))
00230 {
00231 *optr = ci;
00232 optr++;
00233 }
00234 else
00235 {
00236 optr += sprintf(optr,"<0x%X>",ci);
00237 }
00238 iptr++;
00239 }
00240 *optr = 0;
00241 return out;
00242
00243 }
00244
00245 int
00246 main (int argc, char **argv)
00247 {
00248 fd_set readfds;
00249 settings.baud_rate=9600;
00250 settings.data_bits=8;
00251 settings.stop_bits=1;
00252 settings.use_parity=0;
00253 settings.even_parity=0;
00254
00255 parseCmdLineArgs(argc,argv);
00256
00257 if (argc < 2)
00258 {
00259 fprintf (stderr,
00260 "ttytest usage: DeviceName [BaudRate] [DataBits] [StopBits] [Parity(e/o/n)] [Debug(y/n)]\n");
00261 fprintf (stderr, "Example: ttytest /dev/ttyb 9600 8 1 n n\n");
00262 exit (-1);
00263 }
00264 RCS_SERIAL_PORT_HANDLE handle =
00265 open_serial_communications_port (ttyDevName);
00266 if(handle <= 0)
00267 {
00268 exit(-1);
00269 }
00270 if(verbosity >= 1)
00271 {
00272 printf("\nOriginal settings:\n");
00273 print_serial_port_configuration (handle);
00274 printf("\n");
00275 }
00276
00277 set_serial_port_configuration (handle, &settings);
00278 if(verbosity >= 1)
00279 {
00280 printf("\nNew settings:\n");
00281 print_serial_port_configuration (handle);
00282 printf("\n");
00283 }
00284 char buf[256];
00285 char buf2[256];
00286 char answer[20];
00287 memset (buf, 0, sizeof(buf));
00288 int newline_received = 0;
00289 int recieve_first = 0;
00290 char last_last_byte=0;
00291
00292 int max_fileno = STDIN_FILENO>handle?STDIN_FILENO:handle;
00293 max_fileno++;
00294 FD_ZERO(&readfds);
00295 while (1)
00296 {
00297 int select_ret;
00298 FD_SET(STDIN_FILENO,&readfds);
00299 FD_SET(handle, &readfds);
00300 select_ret = select(max_fileno,&readfds,0,0,0);
00301 if(FD_ISSET(handle,&readfds))
00302 {
00303 memset(buf,0,sizeof(buf));
00304 int bytes_read = read_serial_communications_port (handle, buf, sizeof(buf));
00305 if (bytes_read != 0)
00306 {
00307 if (verbosity >= 1)
00308 {
00309 printf ("\nbytes_read = %d\n", bytes_read);
00310 }
00311 for (int i = 0; i < bytes_read; i++)
00312 {
00313 if (buf[i] == '\n' || buf[i] == '\r')
00314 {
00315 newline_received = 1;
00316 }
00317 }
00318
00319 if(verbosity >= 1)
00320 {
00321 printf("\n<recvd>%s</recvd>\n",convertToGraph(buf,buf2,bytes_read));
00322 }
00323 char *cr_ptr = strchr(buf,'\r');
00324 while(cr_ptr >= buf && cr_ptr < buf + sizeof(buf))
00325 {
00326 *cr_ptr = '\n';
00327 cr_ptr = strchr(cr_ptr,'\r');
00328 }
00329 fputs(buf,stdout);
00330 fflush(stdout);
00331 }
00332 }
00333 if (FD_ISSET(STDIN_FILENO, &readfds))
00334 {
00335 memset(buf,0,sizeof(buf));
00336 fgets (buf, sizeof(buf)-2, stdin);
00337 char *endline_ptr = buf +strlen(buf);
00338 if(*(endline_ptr -1) == '\n' ||
00339 *(endline_ptr -1) == '\r')
00340 {
00341 endline_ptr--;
00342 }
00343 strcpy(endline_ptr,endline_str);
00344 int bytes_sent = write_serial_communications_port (handle, buf, strlen (buf));
00345 if(verbosity >= 1)
00346 {
00347 printf("\nbytes_sent=%d\n",bytes_sent);
00348 printf("\n<sent>%s</sent>\n",convertToGraph(buf,buf2,bytes_sent));
00349 }
00350 newline_received = 0;
00351 memset (buf, 0, sizeof(buf));
00352 }
00353 }
00354 }
00355