C “串行端口写入”;ReadIntervalTimeout“;绞刑
我正在编写一个代码,从串行端口读取几个字节,然后异步写入同一端口。发送代码在main()中连续运行,我启动一个新线程从中读取。起初,我在从连接到串行端口的硬件设备读取帧时遇到问题。问题是因为两个字节之间的readFile()超时设置为MAXDWORD。所以,我收到了不完整的字节。因此,我将ReadIntervalTimeout=MAXDWORD更改为ReadIntervalTimeout=20。然后,接收完整的帧工作。但是,字节的发送是挂起的。 正如我所知,读取和写入串行端口是完全独立的。还是不是?该文件配置为非重叠(设置为0)。如果需要更多详细信息,请告诉我 1) 这些是通信超时设置C “串行端口写入”;ReadIntervalTimeout“;绞刑,c,file-io,serial-port,C,File Io,Serial Port,我正在编写一个代码,从串行端口读取几个字节,然后异步写入同一端口。发送代码在main()中连续运行,我启动一个新线程从中读取。起初,我在从连接到串行端口的硬件设备读取帧时遇到问题。问题是因为两个字节之间的readFile()超时设置为MAXDWORD。所以,我收到了不完整的字节。因此,我将ReadIntervalTimeout=MAXDWORD更改为ReadIntervalTimeout=20。然后,接收完整的帧工作。但是,字节的发送是挂起的。 正如我所知,读取和写入串行端口是完全独立的。还是不
Cptimeouts.ReadIntervalTimeout = MAXDWORD; //changed to 20 for a gap of 20ms
Cptimeouts.ReadTotalTimeoutMultiplier = 0;
Cptimeouts.ReadTotalTimeoutConstant = 0;
Cptimeouts.WriteTotalTimeoutMultiplier = 0;
Cptimeouts.WriteTotalTimeoutConstant = 0;
2) 这是在main()中运行的send()
3) 这是receive(),在不同的线程中连续运行(轮询)
显示相关代码。这种情况永远不会发生。再说一次,找到一个可靠的USB设备驱动程序来正确模拟串行端口是很困难的。只是不要设置超时,而是更多地关注返回的字节数。继续调用ReadFile(),直到你有足够的资源为止。我现在使用的是桌面上的内置comport COM1,但仍然存在同样的问题
while(1)
{
int RS232_SendBuf(int comport_number, unsigned char *buf, int size)
{
int n;
if(WriteFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL))
{
return(n);
}
return(-1);
}
}
int RS232_PollComport(int comport_number, unsigned char *buf, int size)
{
int n;
if(size>4096) size = 4096;
/* added the void pointer cast, otherwise gcc will complain about */
/* "warning: dereferencing type-punned pointer will break strict aliasing rules" */
ReadFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL);
return(n);
}