gpt4 book ai didi

c++ - 在 Windows 中通过串行写入二进制数据

转载 作者:行者123 更新时间:2023-11-27 23:12:31 26 4
gpt4 key购买 nike

我需要通过串行端口发送二进制数据,而不会在传输过程中将任何字节重新解释为控制字符。我目前正在按如下方式设置我的串口:

#include <windows.h>

// open serial port
HANDLE hSerial;
hSerial = CreateFile ("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

// get serial parameters
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength = sizeof (dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
cout << "error getting state\n";
exit(0);
}

// set serial params
dcbSerialParams.BaudRate = CBR_115200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState (hSerial, &dcbSerialParams)) {
cout << "error setting parameters\n";
exit(0);
}

// set time outs
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 10;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 10;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts (hSerial, &timeouts)) {
cout << "problem setting timeout values\n";
exit(0);
} else cout << "timeouts set\n";

当我发出 ReadFile 命令时,我可以毫无问题地获取和显示从 0 到 255 的字节。但我对 WriteFile 没有这样的好运。有没有办法显式设置二进制写入模式?

编辑

好的,这里有更多信息。我有一个windows机器和一个linux单板机通过串口连接,上面windows端的代码后面是:

unsigned char temp = 0;

bool keepReading = true;
while (keepReading) {
DWORD dwBytesRead = 0;
ReadFile (hSerial, &temp, 1, &dwBytesRead, NULL);
if (1 == dwBytesRead) cout << (unsigned int) temp << " ";
if (255 == temp) keepReading = false;
}
cout << endl;

bool keepWriting = true;
char send = 0;
while (keepWriting) {
DWORD dwBytesWritten = 0;
WriteFile (hSerial, &send, 1, &dwBytesWritten, NULL);
send++;
if (256 == send) keepWriting = false;
}

我在 linux 端的代码如下所示:

int fd = open("/dev/ttymxc0", O_RDWR | O_NOCTTY);
struct termios options;
bzero (options, sizeof(options));
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
options.c_iflat = IGNPAR;
options.c_oflag = 0;
options.c_lflag = ICANON;
options.c_cc[VMIN] = 1;
options.c_CC[VTIME] = 0;
tcflush (fd, TCIFLUSH);
tcsetattr (fd, ICSANOW, &options);

bool keepWriting = true;
char send = 0;
while (keepWriting) {
write (fd, &send, 1);
send++;
if (256 == send) keepWriting = false;
}

bool keepReading = true;
while (keepReading) {
char temp = 0;
int n = read (fd, &temp, 1);
if (-1 == n) {
perror ("Read error");
keepReading = false;
} else if (1 == n) {
cout << temp << " ";
}
if (256 == temp) keepReading = false;

}
cout << endl;

close(fd);

我在两台机器上启动代码,第一组 while 循环运行良好。 Windows 端的终端显示 0 到 255。然后它就在那里。如果我为第二组 while 循环输出在 linux 端读取的字节数,它会不断给我 0 个字节。这通常表示端口已关闭,但我只是通过它发送了一堆信息,那怎么可能呢?

最佳答案

正如 Jonathan Potter 提到的,您很可能没有关闭 XON/XOFF 流量控制。在调用 SetCommState 之前添加这些行:

dcbSerialParams.fOutX = 0;
dcbSerialParams.fInX = 0;

您可能需要设置的其他一些字段:

dcbSerialParams.fNull = 0;
dcbSerialParams.fDtrControl = DTR_CONTROL_DISABLE;
dcbSerialParams.fRtsControl = RTS_CONTROL_DISABLE;

关于c++ - 在 Windows 中通过串行写入二进制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19166583/

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