4767bfd8462638a189fb0f6165e038fd4cecc3ba
24 #define START_STR_SIZE 100
25 #define SERIAL_BUF_SIZE 1
27 int GetNumberOption (int *argNr
, int argCount
, char *argVect
[], int *ret
, char *option
);
28 int GetStringOption (int *argNr
, int argCount
, char *argVect
[], char *ret
, char *option
);
29 int WaitForRead (int s
, int usecs
);
30 int WaitForWrite (int s
, int usecs
);
32 int SetInputMode (FILE *fd
, int value
, int onoff
);
33 int SetControlMode (FILE *fd
, int value
, int onoff
);
34 int SetLocalMode (FILE *fd
, int value
, int onoff
);
35 void ParseArgs (int argc
, char *argv
[]);
38 char dev
[100] = "/dev/ttyS0";
39 char filename
[100] = "";
40 char serial_buf
[SERIAL_BUF_SIZE
];
41 int baudRates
[] = {0, 50, 75, 110, 134, 150, 200, 300, 600,
42 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
50 int set_baudrate
= 9600;
57 main(int argc
, char *argv
[])
64 ParseArgs(argc
, argv
);
67 if (strcmp(filename
, "") != 0) {
70 if ((fd
= fopen(filename
, "r")) == NULL
) {
71 perror("Can't open file\n");
75 if (fstat(fileno(fd
), &st
) != 0) {
76 perror("stat failed\n");
80 buf
= malloc(st
.st_size
);
81 if (fread(buf
, 1, st
.st_size
, fd
) != st
.st_size
) {
82 printf("fread failed\n");
87 printf("# sendserial transmitting %d bytes at baudrate %d.\n", (int)st
.st_size
, set_baudrate
);
88 printf("# transmitting...\n");
96 // while (tr_len != st.st_size) {
98 if (WaitForRead(fileno(modemFd
), 0)) {
99 if((rec_len
= fread(rec_buf
, 1, 1, modemFd
)) > 0) {
101 printf("# connected!\n");
103 fwrite(rec_buf
, rec_len
, 1, stdout
);
106 if (tr_len
< st
.st_size
) {
107 while (!(WaitForWrite(fileno(modemFd
), 1000000))) { }
108 fwrite(&buf
[tr_len
], 1, 1, modemFd
);
111 else if (tr_len
++ == st
.st_size
) {
112 printf("# ...transmission done.\n");
120 ret
= fwrite(buf
, 1, st
.st_size
, modemFd
);
121 printf("Wrote %d bytes of %d.\n", ret
, (int)st
.st_size
);
130 if (WaitForRead(fileno(modemFd
), 1000000)) {
131 if((rec_len
= fread(rec_buf
, 1, 1, modemFd
)) > 0) {
132 fwrite(rec_buf
, rec_len
, 1, stdout
);
141 /****************************************************************************
143 *# FUNCTION NAME: ParseArgs
145 *# PARAMETERS: argc,argv
149 *#---------------------------------------------------------------------------
154 *# 960930 ronny Initial version
156 *#***************************************************************************/
159 ParseArgs (int argc
, char *argv
[])
163 for (argi
=1; argi
< argc
; argi
++) {
165 if (strncmp(argv
[argi
], "--xonxoff", 9) == 0) {
170 else if (strncmp(argv
[argi
], "--crtcts", 8) == 0) {
175 else if (strncmp(argv
[argi
], "--loop", 6) == 0) {
180 else if (strncmp(argv
[argi
], "--time", 6) == 0) {
185 else if (strncmp(argv
[argi
], "--check", 6) == 0) {
189 else if (strncmp(argv
[argi
], "--noprint", 9) == 0) {
193 else if (strncmp(argv
[argi
], "--device", 8) == 0) {
194 if (GetStringOption (&argi
, argc
, argv
, dev
, "--device") == 0) {
195 printf("--device name\n");
200 else if (strncmp(argv
[argi
], "--file", 6) == 0) {
201 if (GetStringOption (&argi
, argc
, argv
, filename
, "--file") == 0) {
202 printf("--file name\n");
207 else if (strncmp(argv
[argi
], "--baudrate", 10) == 0) {
208 if (GetNumberOption (&argi
, argc
, argv
, &set_baudrate
, "--baudrate") == 0) {
209 printf("--baudrate baudrate\n");
214 printf("'%s' unknown command\n", argv
[argi
]);
215 printf("\nsendserial --file fname [--device dname] [--baudrate baudrate] [--xonxoff] [--crtcts]\n\n");
216 printf("%dbps on %s default. No handshake.\n", set_baudrate
, dev
);
222 /****************************************************************************
224 *# FUNCTION NAME: GetNumberOption
230 *#---------------------------------------------------------------------------
235 *# 960930 ronny Initial version
237 *#***************************************************************************/
240 GetNumberOption(int *argNr
, int argCount
, char *argVect
[], int *ret
, char *option
)
242 int startChar
= strlen(option
);
244 if (strlen(argVect
[*argNr
]) <= (unsigned int) startChar
) {
249 if (*argNr
> argCount
) {
250 printf("ERROR! The option '%s' needs a number argument.\n", option
);
254 *ret
= atoi(&argVect
[*argNr
][startChar
]);
259 /****************************************************************************
261 *# FUNCTION NAME: GetStringOption
263 *# PARAMETERS: int *argNr : Returns next argc here.
264 *# int argCount : Index of last argument.
265 *# char *argVect[] : argv.
266 *# char *ret : Copy string here.
267 *# char *option : Name of the option.
269 *# DESCRIPTION: Extracts a string option from argv, and updates argnr.
270 *# Returns TRUE/FALSE and string in *ret.
272 *#---------------------------------------------------------------------------
277 *# 960930 ronny Initial version
278 *# 961203 ronny Handles filenames with spaces within 'file name'.
280 *#***************************************************************************/
283 GetStringOption(int *argNr
, int argCount
, char *argVect
[], char *ret
, char *option
)
285 int startChar
= strlen(option
);
290 /* Are there any more chars after option? If not skip to next argv. */
291 if (strlen(argVect
[*argNr
]) <= (unsigned int)startChar
) {
297 if (*argNr
>= argCount
) {
298 printf("ERROR! The option '%s' needs a string argument.\n", option
);
302 strcpy(ret
, &argVect
[*argNr
][startChar
]);
304 if (ret
[0] == '\'' || ret
[0] == '\"') {
305 if (db3
) printf("Inside string\n");
306 strcpy(tmp
, &ret
[1]);
308 if (tmp
[strlen(tmp
) - 1 ] == '\'' || tmp
[strlen(tmp
) - 1] == '\"') {
309 tmp
[strlen(tmp
) - 1] = '\0';
315 if (*argNr
>= argCount
) {
316 printf("ERROR! Mismatched ' or \" in options.\n");
320 strcat(tmp
, &argVect
[*argNr
][0]);
321 if (db3
) printf("tmp %s\n", tmp
);
326 if (db4
) printf("<<< GetStringOption %s\n", ret
);
331 /****************************************************************************
333 *# FUNCTION NAME: SetupSerial
339 *#---------------------------------------------------------------------------
344 *# 970325 ronny Initial version
346 *#***************************************************************************/
355 if ((modemFd
= fopen(dev
, "r+")) == NULL
) {
356 perror("Can't open modem\n");
360 /* Important. Remember this!!!!!!! */
361 /* setvbuf(modemFd, NULL, _IONBF, 0); */
363 setvbuf(modemFd
, NULL
, _IONBF
, 0);
364 setvbuf(stdout
, NULL
, _IONBF
, 0);
366 if (fcntl(fileno(modemFd
), F_SETFL
, O_NONBLOCK
) == -1) {
371 tcflush(fileno(modemFd
), TCIOFLUSH
);
373 if (tcgetattr (fileno(modemFd
), &ti
)) {
378 if (db4
) printf("VTIME:%d\n", ti
.c_cc
[VTIME
]);
379 if (db4
) printf("VMIN:%d\n", ti
.c_cc
[VMIN
]);
383 ti
.c_cc
[VSTART
] = XON
;
384 ti
.c_cc
[VSTOP
] = XOFF
;
386 if (tcsetattr (fileno(modemFd
), TCSANOW
, &ti
)) {
391 //if (db3) printf("outspeed: %d, inspeed: %d\n", baudRates[cfgetospeed(&ti)], baudRates[cfgetispeed(&ti)]);
393 int new_baudrate
= 0;
395 if (set_baudrate
<= 50) {
399 else if (set_baudrate
<= 75) {
403 else if (set_baudrate
<= 110) {
407 else if (set_baudrate
<= 134) {
411 else if (set_baudrate
<= 150) {
415 else if (set_baudrate
<= 200) {
419 else if (set_baudrate
<= 300) {
423 else if (set_baudrate
<= 600) {
427 else if (set_baudrate
<= 1200) {
431 else if (set_baudrate
<= 1800) {
435 else if (set_baudrate
<= 2400) {
439 else if (set_baudrate
<= 4800) {
443 else if (set_baudrate
<= 9600) {
447 else if (set_baudrate
<= 19200) {
449 new_baudrate
= 19200;
451 else if (set_baudrate
<= 38400)
453 #if defined (B57600) /* POSIX only defines >= 38400 */
454 else if (set_baudrate
<= 57600) {
456 new_baudrate
= 57600;
459 #if defined (B115200)
460 else if (set_baudrate
<= 115200) {
462 new_baudrate
= 115200;
465 #if defined (B230400)
466 else if (set_baudrate
<= 230400) {
468 new_baudrate
= 230400;
475 cfsetispeed(&ti
, B9600
);
476 cfsetospeed(&ti
, baudrate
);
477 tcsetattr(fileno(modemFd
), TCSANOW
, &ti
);
479 // printf("* baudrate set to %d\n", new_baudrate);
480 set_baudrate
= new_baudrate
;
482 if (db3
) printf("outspeed: %d, inspeed: %d\n", baudRates
[cfgetospeed(&ti
)], baudRates
[cfgetispeed(&ti
)]);
487 SetInputMode(modemFd
, IXON
, FALSE
);
488 SetInputMode(modemFd
, IXOFF
, xonxoff
); /* input */
489 SetInputMode(modemFd
, IXANY
, FALSE
);
490 SetInputMode(modemFd
, ICRNL
, FALSE
);
491 SetInputMode(modemFd
, IGNPAR
, TRUE
);
492 SetInputMode(modemFd
, IGNBRK
, TRUE
);
493 SetInputMode(modemFd
, IGNPAR
, TRUE
);
494 SetInputMode(modemFd
, INPCK
, FALSE
);
496 SetLocalMode(modemFd
, ECHO
, FALSE
);
497 SetLocalMode(modemFd
, ISIG
, FALSE
);
499 SetLocalMode(modemFd
, ICANON
, FALSE
);
501 SetControlMode(modemFd
, PARENB
, FALSE
);
502 SetControlMode(modemFd
, CLOCAL
, TRUE
);
504 /* Disable hardware flow control. */
507 SetControlMode(modemFd
, CRTSCTS
, crtcts
);
508 #if defined(CRTSXOFF)
509 SetControlMode(modemFd
, CRTSXOFF
, crtcts
);
512 #elif defined(CCTS_OFLOW)
514 SetControlMode(modemFd
, CCTS_OFLOW
, crtcts
);
515 SetControlMode(modemFd
, CRTS_IFLOW
, crtcts
);
517 #elif defined(CNEW_RTSCTS)
519 SetControlMode(modemFd
, CNEW_RTSCTS
, crtcts
);
522 printf("Cannot set hardware flow control. Set it manually with a terminal program.\n");
529 /****************************************************************************
531 *# FUNCTION NAME: SetInputMode
537 *#---------------------------------------------------------------------------
542 *# 970325 ronny Initial version
544 *#***************************************************************************/
547 SetInputMode (FILE *fd
, int value
, int onoff
)
549 struct termios settings
;
552 result
= tcgetattr(fileno(fd
), &settings
);
555 perror ("error in tcgetattr");
560 settings
.c_iflag
|= value
;
562 settings
.c_iflag
&= ~value
;
564 result
= tcsetattr(fileno(fd
), TCSANOW
, &settings
);
567 perror ("error in tcgetattr");
574 /****************************************************************************
576 *# FUNCTION NAME: SetControlMode
582 *#---------------------------------------------------------------------------
587 *# 970325 ronny Initial version
589 *#***************************************************************************/
592 SetControlMode (FILE *fd
, int value
, int onoff
)
597 struct termios settings
;
600 result
= tcgetattr (fileno(fd
), &settings
);
603 perror ("error in tcgetattr");
608 settings
.c_cflag
|= value
;
610 settings
.c_cflag
&= ~value
;
612 result
= tcsetattr (fileno(fd
), TCSANOW
, &settings
);
615 perror ("error in tcgetattr");
623 /****************************************************************************
625 *# FUNCTION NAME: SetLocalMode
631 *#---------------------------------------------------------------------------
636 *# 970325 ronny Initial version
638 *#***************************************************************************/
641 SetLocalMode (FILE *fd
, int value
, int onoff
)
646 struct termios settings
;
649 result
= tcgetattr (fileno(fd
), &settings
);
652 perror ("error in tcgetattr");
657 settings
.c_lflag
|= value
;
659 settings
.c_lflag
&= ~value
;
661 result
= tcsetattr (fileno(fd
), TCSANOW
, &settings
);
664 perror ("error in tcgetattr");
673 /****************************************************************************
675 *# FUNCTION NAME: WaitForRead
681 *#---------------------------------------------------------------------------
686 *# 961001 ronny Initial version
687 *# 971204 ronny usecs was a only one tenth of what it should be.
689 *#***************************************************************************/
692 WaitForRead(int s
, int usecs
)
698 tm
.tv_sec
= usecs
/1000000;
699 tm
.tv_usec
= usecs
%1000000;
704 if ((ret
= select(FD_SETSIZE
, &mask
, NULL
, NULL
, &tm
)) == -1) {
705 printf("<<< select failed with return: %d.\n", ret
);
709 if (db3
) printf("* select returned OK:%d\n", ret
);
712 if (FD_ISSET(s
, &mask
) == 0) {
713 if (db2
) printf("<<< WaitForRead. No data to read.\n");
717 if (db3
) printf("<<< WaitForRead OK. Returned:%d.\n", ret
);
722 /****************************************************************************
724 *# FUNCTION NAME: WaitForWrite
730 *#---------------------------------------------------------------------------
735 *# 2000-11-20 ronny Initial version
737 *#***************************************************************************/
740 WaitForWrite(int s
, int usecs
)
746 tm
.tv_sec
= usecs
/1000000;
747 tm
.tv_usec
= usecs
%1000000;
752 if ((ret
= select(FD_SETSIZE
, NULL
, &mask
, NULL
, &tm
)) == -1) {
753 printf("<<< select failed with return: %d.\n", ret
);
757 if (db3
) printf("* select returned OK:%d\n", ret
);
760 if (FD_ISSET(s
, &mask
) == 0) {
761 if (db2
) printf("<<< WaitForWrite. No data to read.\n");
765 if (db3
) printf("<<< WaitForWrite OK. Returned:%d.\n", ret
);
This page took 0.079843 seconds and 3 git commands to generate.