00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <fcntl.h>
00037 #include <unistd.h>
00038 #include <termios.h>
00039 #include <sys/ioctl.h>
00040 #include <string.h>
00041
00042 #define COM_PORT "/dev/ttyS0";
00043
00044
00045 int debug = 0;
00046
00047 int open_port(char * com_port) {
00048 int fd;
00049 int err;
00050 struct termios options;
00051
00052 if ( debug >= 1 ) {
00053 printf("Entering function open_port()\n");
00054 }
00055 if ( debug >= 2 ) {
00056 printf(" Opening port...\n");
00057 }
00058
00059 fd = open(com_port, O_WRONLY | O_NOCTTY | O_NDELAY);
00060 if ( fd == -1 ) {
00061 perror(" open: Unable to open com port - ");
00062 exit(EXIT_FAILURE);
00063 }
00064
00065 if ( debug >= 2 ) {
00066 printf(" Opening port succeeded!\n");
00067 printf(" Setting file options...\n");
00068 }
00069
00070 err = fcntl(fd, F_SETFL, 0);
00071 if ( err != 0 ) {
00072 perror(" fcntl: Setting file options failed - ");
00073 exit(EXIT_FAILURE);
00074 }
00075
00076 if ( debug >= 2 ) {
00077 printf(" Setting file options suceeded!\n");
00078 printf(" Getting current options for the port...\n");
00079 }
00080
00081 err = tcgetattr(fd, &options);
00082 if ( err != 0 ) {
00083 perror(" tcgetattr: Getting file options failed - ");
00084 exit(EXIT_FAILURE);
00085 }
00086
00087 if ( debug >= 2 ) {
00088 printf(" Getting current options for the port succeeded!\n");
00089 printf(" Setting baud rates for the port...\n");
00090 }
00091
00092 err = cfsetispeed(&options, B9600);
00093 if ( err != 0 ) {
00094 perror(" cfsetispeed: Setting input baud rate failed - ");
00095 exit(EXIT_FAILURE);
00096 }
00097
00098 err = cfsetospeed(&options, B9600);
00099 if ( err != 0 ) {
00100 perror(" cfsetospeed: Setting output baud rate failed - ");
00101 exit(EXIT_FAILURE);
00102 }
00103
00104 if ( debug >= 2 ) {
00105 printf(" Setting baud rates for the port succeeded!\n");
00106 printf(" Setting local mode...\n");
00107 }
00108
00109
00110 options.c_cflag |= (CLOCAL );
00111 options.c_cflag &= ~PARENB;
00112 options.c_cflag &= ~CSTOPB;
00113 options.c_cflag &= ~CSIZE;
00114 options.c_cflag |= CS8;
00115 options.c_cflag &= ~CREAD;
00116 options.c_cflag &= ~CRTSCTS;
00117 options.c_iflag &= ~(IXON | IXOFF | IXANY);
00118
00119 err = tcsetattr(fd, TCSANOW, &options);
00120 if ( err != 0 ) {
00121 perror(" tcsetattr: Setting local mode failed - ");
00122 exit(EXIT_FAILURE);
00123 }
00124
00125 if ( debug >= 2 ) {
00126 printf(" Setting local mode suceeded!\n");
00127 }
00128 if ( debug >= 1 ) {
00129 printf("Leaving function open_port()\n");
00130 }
00131
00132 return(fd);
00133 }
00134
00135 void set_rts(int fd) {
00136
00137 int bitset = TIOCM_DTR;
00138 int status;
00139 int err;
00140
00141 if ( debug >= 1 ) {
00142 printf("Entering function set_rts()\n");
00143 }
00144 if ( debug >= 2 ) {
00145 printf(" Checking current state of RTS line...\n");
00146 }
00147
00148 err = ioctl(fd, TIOCMGET, &status);
00149 if ( err != 0 ) {
00150 perror(" ioctl: Checking RTS line failed - ");
00151 exit(EXIT_FAILURE);
00152 }
00153
00154 if ( debug >= 2 ) {
00155 if ( status & bitset ) {
00156 printf(" RTS bit is set \n");
00157 } else {
00158 printf(" RTS bit is unset\n");
00159 }
00160 printf(" Checking current state of RTS line suceeded\n");
00161 printf(" Setting RTS line...\n");
00162 }
00163
00164 status |= bitset;
00165 err = ioctl(fd, TIOCMSET, &status);
00166 if ( err != 0 ) {
00167 perror(" ioctl: Setting RTS line failed - ");
00168 exit(EXIT_FAILURE);
00169 }
00170
00171 if ( debug >= 2 ) {
00172 printf(" Setting RTS line succeeded\n");
00173 printf(" Checking new state of RTS line...\n");
00174 }
00175
00176 err = ioctl(fd, TIOCMGET, &status);
00177 if ( err != 0 ) {
00178 perror(" ioctl: Checking RTS line failed - ");
00179 exit(EXIT_FAILURE);
00180 }
00181
00182 if ( debug >= 2 ) {
00183 if ( status & bitset ) {
00184 printf(" RTS bit was set correctly \n");
00185 } else {
00186 printf(" FAILED to set RTS bit\n");
00187 exit(EXIT_FAILURE);
00188 }
00189 printf(" Checking new state of RTS line suceeded\n");
00190 }
00191 if ( debug >= 1 ) {
00192 printf("Leaving function set_rts()\n");
00193 }
00194 }
00195
00196 int main (int argc, char **argv) {
00197 int fd;
00198 char *com_port = COM_PORT;
00199 char *data;
00200 char *message;
00201 int wait_time;
00202 int n;
00203
00204 debug = 0;
00205
00206
00207 switch (argc) {
00208 case 5: {
00209 com_port = argv[1];
00210 data = argv[2];
00211 wait_time = atoi(argv[3]);
00212 debug = atoi(argv[4]);
00213 break;
00214 };
00215 case 4: {
00216 com_port = argv[1];
00217 data = argv[2];
00218 wait_time = atoi(argv[3]);
00219 break;
00220 };
00221 case 3: {
00222 com_port = COM_PORT;
00223 data = argv[1];
00224 debug = atoi(argv[2]);
00225 wait_time = 1;
00226 break;
00227 };
00228 case 2: {
00229 com_port = COM_PORT;
00230 data = argv[1];
00231 wait_time = 1;
00232 break;
00233 };
00234 default: {
00235 fputs("usage: red_eye deviceComPort sendString waitSecs [debug]\n",stderr);
00236 fputs(" -or- red_eye sendString [debug]\n",stderr);
00237 fputs("\n",stderr);
00238 fputs(" where deviceComPort is e.g. /dev/ttyS0\n",stderr);
00239 fputs(" sendString is your desired command to the RedEye Serial\n",stderr);
00240 fputs(" waitSecs is pause time after command (in seconds 0-9)\n",stderr);
00241 fputs(" debug is debug level (0=off, 9=max)\n",stderr);
00242 exit(EXIT_FAILURE);
00243 };
00244 }
00245
00246 if ( 0 > wait_time ) wait_time = 0;
00247 if ( 9 < wait_time ) wait_time = 9;
00248
00249 if ( 0 > debug ) debug = 0;
00250 if ( 9 < debug ) debug = 9;
00251
00252 if ( debug >= 1 ) {
00253 printf("Opening port for %s\n", com_port);
00254 }
00255
00256 fd = open_port(com_port) ;
00257
00258 if ( debug >= 1 ) {
00259 printf("Setting RTS for %s\n", com_port);
00260 }
00261
00262 set_rts(fd);
00263
00264 if ( debug >= 1 ) {
00265 printf("Pausing for 2 seconds to allow IR system to power up\n");
00266 }
00267
00268 sleep(2);
00269
00270 if ( debug >= 1 ) {
00271 printf("Writing data (\"%s\") now\n",data);
00272 }
00273
00274 n = write(fd, strcat(data,"O"), strlen(data)+1);
00275 if (n < 0) {
00276 perror(" write: Writing data failed - ");
00277 exit(EXIT_FAILURE);
00278 }
00279 if ( debug >= 1 ) {
00280 printf("Writing data suceeded\n");
00281 }
00282
00283 tcdrain(fd);
00284
00285 if ( debug >= 1 ) {
00286 printf("Sleeping for %d seconds ....\n",wait_time);
00287 }
00288
00289 sleep(wait_time);
00290
00291 if ( debug >= 1 ) {
00292 printf("Start close... ");
00293 }
00294
00295 close(fd);
00296
00297 if ( debug >= 1 ) {
00298 printf("finished\n");
00299 }
00300
00301 exit(EXIT_SUCCESS);
00302 }