forked from distributed/sers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sers_linux.c
134 lines (107 loc) · 3.72 KB
/
sers_linux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stddef.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
/*
So... custom baud rate support is a very sad topic under linux. Sometimes
there's no asm/termios.h, sometimes is clashes with termios.h, sometimes
termios.h has the wrong definition for NCCS, messing up the layout of a
struct termios2 that is defined here but dependent on NCCS.
The picocom has a whole rant/article about the situation:
https://github.com/Rosonix/picocom/blob/master/termios2.txt
Their copy-pasted implementation lives here:
https://github.com/Rosonix/picocom/blob/238547d7174571b2a463738f03f3dea128e5c676/termbits2.h
And is probably wrong for x86_64. But who knows?!
The pyserial approach can be found at:
https://github.com/pyserial/pyserial/blob/master/serial/serialposix.py
Look at the function _set_special_baudrate. I guess the code is wrong for
pretty much every platform that is not x86_64. Maybe nobody ever noticed?
*/
#if NCCS == 32
// i have not seen a platform with NCCS == 32 in the kernel header files. i saw
// NCCS == 32 On My Machine where it should have been 19, so we're using that.
// Getting this wrong will most likely result in a ioctl that returns an error.
#define NCCS_K 19
#else
#define NCCS_K NCCS
#endif
/* extended termios struct for custom baud rate */
struct termios3 {
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[NCCS_K]; /* control characters */
speed_t c_ispeed; /* input speed */
speed_t c_ospeed; /* output speed */
};
#define TCGETS3 _IOR('T', 0x2A, struct termios3)
#define TCSETS3 _IOW('T', 0x2B, struct termios3)
#include <stdio.h>
#include <errno.h>
#include <string.h>
int ioctl1(int i, unsigned int r, void *d) {
return ioctl(i, r, d);
}
speed_t lookupbaudrate(int br) {
switch (br) {
case 50 : return B50 ;
case 75 : return B75 ;
case 110 : return B110 ;
case 134 : return B134 ;
case 150 : return B150 ;
case 200 : return B200 ;
case 300 : return B300 ;
case 600 : return B600 ;
case 1200 : return B1200 ;
case 1800 : return B1800 ;
case 2400 : return B2400 ;
case 4800 : return B4800 ;
case 9600 : return B9600 ;
case 19200 : return B19200 ;
case 38400 : return B38400 ;
case 57600 : return B57600 ;
case 115200 : return B115200 ;
case 230400 : return B230400 ;
case 460800 : return B460800 ;
case 500000 : return B500000 ;
case 576000 : return B576000 ;
case 921600 : return B921600 ;
case 1000000: return B1000000 ;
case 1152000: return B1152000 ;
case 1500000: return B1500000 ;
case 2000000: return B2000000 ;
case 2500000: return B2500000 ;
case 3000000: return B3000000 ;
case 3500000: return B3500000 ;
case 4000000: return B4000000 ;
}
return 0;
}
int setbaudrate(int fd, int br) {
// we try to set the baud rate via the old school interface first. this
// should work more reliably, if only for certain baud rates.
speed_t baudrateconstant = lookupbaudrate(br);
if (baudrateconstant) {
struct termios tio;
int ret = tcgetattr(fd, &tio);
if (ret == -1) return ret;
ret = cfsetispeed(&tio, baudrateconstant);
if (ret == -1) return ret;
ret = cfsetospeed(&tio, baudrateconstant);
if (ret == -1) return ret;
ret = tcsetattr(fd, TCSANOW, &tio);
return ret;
}
// if we have a custom baud rate, we use our own cobbled together approach.
struct termios3 tio;
int ret;
ret = ioctl(fd, TCGETS3, &tio);
if (ret == -1) return ret;
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= CBAUDEX;
tio.c_ispeed = br;
tio.c_ospeed = br;
return ioctl(fd, TCSETS3, &tio);
}