我试图通过串口读取一系列数据。以下是我的测试场景:
我在Windows 10上有一个发送方(POS终端)和一个接收方(dll)。两者都通过RS232电缆(9针)连接。
发件人一次写入16个字节的数据。接收器能够读取数据,每次读取1个字节,循环16次。
发件人一次写入114个字节的数据。接收器能够通过循环114次来读取每次读取1个字节的数据。
发件人一次写入115个字节的数据。接收器能够通过循环115次来读取每次读取1个字节的数据。但最后一个角色丢失了。换句话说,最后一个字符似乎已损坏。
E.g。编写的数据: ABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNO
我能够得到" ABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMN "正确。但最后一个字符' O' 已损坏。而是代替最后一个字符读取的数据是(ASCII值)205,255,234等。在多次读取尝试期间数据不一致。
任何人都可以对此有所了解吗?如果我遗失某些东西,有人可以指导我吗?
我在本项目中提到了这篇文章:Serial Port Communication
Below is the code snippet:
//Port opening.
HANDLE hPortDailUp = CreateFile(portfinal,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
0,
NULL);
//Setting COM timeouts.
SetCommMask(hPortDailUp, 0);
SetupComm(hPortDailUp, 2048, 2048);
GetCommTimeouts (hPortDailUp, &commTimeOuts);
commTimeOuts.ReadIntervalTimeout = 100;
commTimeOuts.ReadTotalTimeoutMultiplier = 70;
commTimeOuts.ReadTotalTimeoutConstant = 1000; //1000
commTimeOuts.WriteTotalTimeoutMultiplier = 0;
commTimeOuts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hPortDailUp, &commTimeOuts);
//Setting port configuration.
DCB PortDCB;
PortDCB.DCBlength = sizeof(DCB);
PortDCB.BaudRate = BaudRate;
PortDCB.ByteSize = 8;
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONESTOPBIT;
PortDCB.fOutxCtsFlow = RTS_CONTROL_DISABLE;
PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
SetCommState(hPortDailUp, &PortDCB)
//Reading data from serial port.
unsigned char tempRecvBuf[3001] = {'\0'};
ReadFile(hPortDailUp, tempRecvBuf + nBytesRead, 1, &TempLen, NULL);
//PortDCB Value at runtime.
PortDCB {DCBlength=28 BaudRate=115200 fBinary=1 ...} _DCB
DCBlength 28 unsigned long
BaudRate 115200 unsigned long
fBinary 1 unsigned long
fParity 0 unsigned long
fOutxCtsFlow 0 unsigned long
fOutxDsrFlow 0 unsigned long
fDtrControl 1 unsigned long
fDsrSensitivity 0 unsigned long
fTXContinueOnXoff 0 unsigned long
fOutX 0 unsigned long
fInX 0 unsigned long
fErrorChar 0 unsigned long
fNull 0 unsigned long
fRtsControl 0 unsigned long
fAbortOnError 0 unsigned long
fDummy2 0 unsigned long
wReserved 0 unsigned short
XonLim 2048 unsigned short
XoffLim 512 unsigned short
ByteSize 8 '\b' unsigned char
Parity 0 '\0' unsigned char
StopBits 0 '\0' unsigned char
XonChar 17 '\x11' char
XoffChar 19 '\x13' char
ErrorChar 0 '\0' char
EofChar 0 '\0' char
EvtChar 0 '\0' char
wReserved1 0 unsigned short
答案 0 :(得分:0)
你的循环可能没有足够快地读出字符,所以到了第115次,一个新角色已经覆盖了最后一个被太晚读出的角色。 要对此进行测试,您可以在发送器端的字符之间添加非常短的延迟,然后查看接收器的行为是否不同。