gpt4 book ai didi

c - 从串行端口读取原始字节

转载 作者:IT王子 更新时间:2023-10-28 23:59:35 24 4
gpt4 key购买 nike

我正在尝试从 IEC 870-5-101 win32 协议(protocol)模拟器发送的串行端口读取原始字节,并使用在 Linux 32 位上运行的用 C 编写的程序。

对于像 0x00 - 0x7F 这样的字节值,它工作正常。但是对于从 0x80 到 0xAF 的值,高位是错误的,例如:

0x7F -> 0x7F //correct
0x18 -> 0x18 //correct
0x79 -> 0x79 //correct
0x80 -> 0x00 //wrong
0xAF -> 0x2F //wrong
0xFF -> 0x7F //wrong

经过两天的挖掘,我不知道是什么原因造成的。

这是我的串口配置:

    cfsetispeed(&config, B9600);
cfsetospeed(&config, B9600);

config.c_cflag |= (CLOCAL | CREAD);

config.c_cflag &= ~CSIZE; /* Mask the character size bits */
config.c_cflag |= (PARENB | CS8); /* Parity bit Select 8 data bits */

config.c_cflag &= ~(PARODD | CSTOPB); /* even parity, 1 stop bit */


config.c_cflag |= CRTSCTS; /*enable RTS/CTS flow control - linux only supports rts/cts*/


config.c_iflag &= ~(IXON | IXOFF | IXANY); /*disable software flow control*/

config.c_oflag &= ~OPOST; /* enable raw output */
config.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* enable raw input */

config.c_iflag &= ~(INPCK | PARMRK); /* DANGEROUS no parity check*/
config.c_iflag |= ISTRIP; /* strip parity bits */
config.c_iflag |= IGNPAR; /* DANGEROUS ignore parity errors*/

config.c_cc[VTIME] = 1; /*timeout to read a character in tenth of a second*/

我正在通过串口读取数据:

*bytesread = read((int) fd, in_buf, BytesToRead);

在此操作之后“in_buf”包含错误的字节,所以我猜我的配置有问题,它是来自 win32 DCB 结构的端口。

感谢任何想法!

最佳答案

根据您的示例,只有第 8 位(高位)是错误的,并且始终为 0 是错误的。您在 Linux 端的线路规程中设置了 ISTRIP,这将导致此问题。正如 C 代码中的注释所声称的那样,ISTRIP 不会剥离奇偶校验位。它剥离第 8 个数据位。

If ISTRIP is set, valid input bytes shall first be stripped to seven bits; otherwise, all eight bits shall be processed. IEEE Std 1003.1, 2004 Edition, chapter 11, General Terminal Interface

关于c - 从串行端口读取原始字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8606291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com