gpt4 book ai didi

c - 一根线串口通讯,回显错误

转载 作者:行者123 更新时间:2023-11-30 15:56:58 24 4
gpt4 key购买 nike

我有一个单线串行通信接口(interface),问题是我发送01010101,收到的回声是10次中的8次01010101,但10次中的2次我收到01110101。

代码示例:

void checkVersion(int fd) {
tcflush(fd, TCIFLUSH);
unsigned char checkVersion[] = {0x55, 0x02, 0x00, 0x02};
int n = write(fd, &checkVersion, 4); //Send data
if (n < 0) cout << "BM: WRITE FAILED" << endl;

char read_bytes[10] = {0};
char c;
int aantalBytes = 0;
bool foundU = false;
int res;
while (aantalBytes < 7) {
res = read(fd, &c, 200);
if (res != 0) {
cout << "Byte received: " << bitset < 8 > (c) << endl;
if (c == 'U')foundU = true;
if (foundU)
read_bytes[aantalBytes++] = c;
}
if (aantalBytes > 2 && !foundU) break;
}
if (!foundU) checkVersionSucceeded = false;
if (read_bytes[aantalBytes - 3] == 0x02 && read_bytes[aantalBytes - 2] == 0x04 && read_bytes[aantalBytes - 1] == 0x06)
cout << "BM Version 4" << endl;
}

如何配置我的端口:

int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in

cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);

port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;

tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return (fd);
}

有什么问题吗?回声怎么可能 10 次中有 2 次混合?

最佳答案

也许您应该在配置连接时尝试使用 bzero() 函数。

bzero(&port_settings, sizeof (port_settings));

这会清除新端口设置的结构,这可能有助于阻止您通过串行端口收到的不规则答案。

关于c - 一根线串口通讯,回显错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10795161/

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