Improve baudrate handling
This commit is contained in:
parent
b2bd5ed662
commit
93608baed4
@ -1,5 +1,7 @@
|
|||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
#include "tools-utils.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
@ -13,6 +15,8 @@
|
|||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
#define BAUDRATE B115200
|
#define BAUDRATE B115200
|
||||||
#define BAUDRATE_S "115200"
|
#define BAUDRATE_S "115200"
|
||||||
|
|
||||||
|
speed_t b_rate = BAUDRATE;
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
#ifdef linux
|
#ifdef linux
|
||||||
#define MODEMDEVICE "/dev/ttyS0"
|
#define MODEMDEVICE "/dev/ttyS0"
|
||||||
@ -105,8 +109,7 @@ main(int argc, char **argv)
|
|||||||
struct termios options;
|
struct termios options;
|
||||||
fd_set mask, smask;
|
fd_set mask, smask;
|
||||||
int fd;
|
int fd;
|
||||||
speed_t speed = BAUDRATE;
|
int baudrate = BUNKNOWN;
|
||||||
char *speedname = BAUDRATE_S;
|
|
||||||
char *device = MODEMDEVICE;
|
char *device = MODEMDEVICE;
|
||||||
char *timeformat = NULL;
|
char *timeformat = NULL;
|
||||||
unsigned char buf[BUFSIZE];
|
unsigned char buf[BUFSIZE];
|
||||||
@ -120,23 +123,7 @@ main(int argc, char **argv)
|
|||||||
if(argv[index][0] == '-') {
|
if(argv[index][0] == '-') {
|
||||||
switch(argv[index][1]) {
|
switch(argv[index][1]) {
|
||||||
case 'b':
|
case 'b':
|
||||||
/* set speed */
|
baudrate = atoi(&argv[index][2]);
|
||||||
if(strcmp(&argv[index][2], "38400") == 0) {
|
|
||||||
speed = B38400;
|
|
||||||
speedname = "38400";
|
|
||||||
} else if(strcmp(&argv[index][2], "19200") == 0) {
|
|
||||||
speed = B19200;
|
|
||||||
speedname = "19200";
|
|
||||||
} else if(strcmp(&argv[index][2], "57600") == 0) {
|
|
||||||
speed = B57600;
|
|
||||||
speedname = "57600";
|
|
||||||
} else if(strcmp(&argv[index][2], "115200") == 0) {
|
|
||||||
speed = B115200;
|
|
||||||
speedname = "115200";
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "unsupported speed: %s\n", &argv[index][2]);
|
|
||||||
return usage(1);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
mode = MODE_HEX;
|
mode = MODE_HEX;
|
||||||
@ -181,6 +168,12 @@ main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(baudrate != BUNKNOWN) {
|
||||||
|
b_rate = select_baudrate(baudrate);
|
||||||
|
if(b_rate == 0) {
|
||||||
|
fprintf(stderr, "unknown baudrate %d\n", baudrate);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "connecting to %s", device);
|
fprintf(stderr, "connecting to %s", device);
|
||||||
@ -203,9 +196,10 @@ main(int argc, char **argv)
|
|||||||
perror("could not get options");
|
perror("could not get options");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
/* fprintf(stderr, "serial options set\n"); */
|
|
||||||
cfsetispeed(&options, speed);
|
cfsetispeed(&options, b_rate);
|
||||||
cfsetospeed(&options, speed);
|
cfsetospeed(&options, b_rate);
|
||||||
|
|
||||||
/* Enable the receiver and set local mode */
|
/* Enable the receiver and set local mode */
|
||||||
options.c_cflag |= (CLOCAL | CREAD);
|
options.c_cflag |= (CLOCAL | CREAD);
|
||||||
/* Mask the character size bits and turn off (odd) parity */
|
/* Mask the character size bits and turn off (odd) parity */
|
||||||
|
Loading…
Reference in New Issue
Block a user