【发布时间】:2017-12-24 08:58:53
【问题描述】:
我想将 Libc 函数 tcgetattr 返回的 termios 字节映射到 C# 中的类。
在 C 中 termios 定义为:
#define NCCS 12
typedef unsigned cc_t;
typedef unsigned speed_t;
typedef unsigned tcflag_t;
struct termios {
cc_t c_cc[NCCS];
tcflag_t c_cflag;
tcflag_t c_iflag;
tcflag_t c_lflag;
tcflag_t c_oflag;
speed_t c_ispeed;
speed_t c_ospeed;
};
以下是串行端口的 termios 字节。它们之间的唯一区别是 B9600 与 B38400 使用 Libc cfsetspeed 设置的波特率(平台是运行 Raspbian Stretch 的 Raspberry PI):
Byte# B38400 B9600
0 0 0
1 5 5
2 0 0
3 0 0
4 5 5
5 0 0
6 0 0
7 0 0
8 191 189
9 12 12
10 0 0
11 0 0
12 59 59
13 138 138
14 0 0
15 0 0
16 0 0
17 3 3
18 28 28
19 127 127
20 21 21
21 4 4
22 0 0
23 0 1
24 0 0
25 17 17
26 19 19
27 26 26
28 0 0
29 18 18
30 15 15
31 23 23
32 22 22
B9600 和 B38400 之间的唯一区别是索引 = 8 的字节和索引 = 8 的字节的位模式是有意义的,因为 B9600 = 0xd 和 B38400 = 0xf。 189 的最后一个字节是 0xd,而 191 os 的最后一个字节是 0xf,所以看起来是正确的。但是,我不知道如何理解如何将字节映射到 C 结构。当速度改变时,c_ispeed 和 c_ospeed 的字节不应该改变吗?
谁能解释如何将字节索引映射到 C 结构?
【问题讨论】:
标签: c# serial-port termios