无法从串行端口读取。 WaitCommEvent() 从不 returns
Unable to read from serial port. WaitCommEvent() never returns
我必须使用 win32 从串口读取字节 api 但还没有连接任何设备
到端口进行测试,所以我在端口中写入一个字节并尝试读取,但 WaitCommEvent()
从来没有
returns 并且程序仍在等待 state.When 我检查是否写入完成,我看到已经完成但问题出在 WaitCommEvent()
。
HANDLE hPort;
TCHAR *pcCommPort = TEXT("COM1");
hPort = CreateFile( pcCommPort,GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,NULL);
if (hPort == INVALID_HANDLE_VALUE)
MessageBox( hwnd , L"Error in opening serial port" , L"error" , MB_OK );
else
MessageBox( hwnd , L"Port opened" , L"successful" , MB_OK ); //This is displayed
//Configuration
DCB conf={0};
conf.DCBlength = sizeof(conf);
if(GetCommState(hPort, &conf))
{
conf.ByteSize = 8;
conf.Parity = NOPARITY;
conf.StopBits = ONESTOPBIT;
conf.fBinary = TRUE;
conf.fParity = TRUE;
}
else
MessageBox( hwnd , L"Cannot get comm state" , L"Oops" , MB_OK );
if(!SetCommState(hPort, &conf))
{
MessageBox( hwnd , L"cannot set comm state" , L"Oops" , MB_OK );
}
//Timeout
COMMTIMEOUTS commTimeout;
if(GetCommTimeouts(hPort, &commTimeout))
{
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
}
else
MessageBox( hwnd , L"cannot get timeout" , L"Oops" , MB_OK );
if(!SetCommTimeouts(hPort, &commTimeout))
MessageBox( hwnd , L"cannot set timeout" , L"Oops" , MB_OK );
//Writing
char lpBuffer[] = "a";
DWORD dNoOFBytestoWrite;
DWORD dNoOfBytesWritten = 0;
dNoOFBytestoWrite = sizeof(lpBuffer);
WriteFile(hPort,lpBuffer,dNoOFBytestoWrite,&dNoOfBytesWritten,NULL);
if(dNoOfBytesWritten == 1){
MessageBox(NULL , L"Writing happened" , L"Attention" , MB_OK); //This is displayed
}
//Reading
DWORD dwEventMask;
SetCommMask(hPort, EV_RXCHAR);
WaitCommEvent(hPort, &dwEventMask, NULL);
char TempChar;
DWORD NoBytesRead;
ReadFile( hPort,&TempChar,sizeof(TempChar),&NoBytesRead, NULL);
CloseHandle(hPort);//Closing the Serial Port
怎么了?为什么我看不懂?
谢谢
I [...] have not connected any device to the port
来自连接设备的串行端口 "reads" 字节。您没有连接的设备。因此,永远不会出现角色。
如果有环回连接(使您的计算机成为自己的连接设备),您只能读回您自己写入的字节。一些串口会支持软件配置环回,但是C#没有提供任何方法来控制这个1。否则你将需要一个hardware loopback connection(如果你禁用硬件握手,它可以像单线一样简单)
最后一个选项是虚拟串行端口驱动程序,它连接到另一个应用程序而不涉及任何硬件。
1 Win32 API 可以启用和禁用内部环回,但不幸的是它仍然既不是标准的也不是普遍支持的。参见 IOCTL_SERIAL_SET_MODEM_CONTROL
。
我必须使用 win32 从串口读取字节 api 但还没有连接任何设备
到端口进行测试,所以我在端口中写入一个字节并尝试读取,但 WaitCommEvent()
从来没有
returns 并且程序仍在等待 state.When 我检查是否写入完成,我看到已经完成但问题出在 WaitCommEvent()
。
HANDLE hPort;
TCHAR *pcCommPort = TEXT("COM1");
hPort = CreateFile( pcCommPort,GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,NULL);
if (hPort == INVALID_HANDLE_VALUE)
MessageBox( hwnd , L"Error in opening serial port" , L"error" , MB_OK );
else
MessageBox( hwnd , L"Port opened" , L"successful" , MB_OK ); //This is displayed
//Configuration
DCB conf={0};
conf.DCBlength = sizeof(conf);
if(GetCommState(hPort, &conf))
{
conf.ByteSize = 8;
conf.Parity = NOPARITY;
conf.StopBits = ONESTOPBIT;
conf.fBinary = TRUE;
conf.fParity = TRUE;
}
else
MessageBox( hwnd , L"Cannot get comm state" , L"Oops" , MB_OK );
if(!SetCommState(hPort, &conf))
{
MessageBox( hwnd , L"cannot set comm state" , L"Oops" , MB_OK );
}
//Timeout
COMMTIMEOUTS commTimeout;
if(GetCommTimeouts(hPort, &commTimeout))
{
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
}
else
MessageBox( hwnd , L"cannot get timeout" , L"Oops" , MB_OK );
if(!SetCommTimeouts(hPort, &commTimeout))
MessageBox( hwnd , L"cannot set timeout" , L"Oops" , MB_OK );
//Writing
char lpBuffer[] = "a";
DWORD dNoOFBytestoWrite;
DWORD dNoOfBytesWritten = 0;
dNoOFBytestoWrite = sizeof(lpBuffer);
WriteFile(hPort,lpBuffer,dNoOFBytestoWrite,&dNoOfBytesWritten,NULL);
if(dNoOfBytesWritten == 1){
MessageBox(NULL , L"Writing happened" , L"Attention" , MB_OK); //This is displayed
}
//Reading
DWORD dwEventMask;
SetCommMask(hPort, EV_RXCHAR);
WaitCommEvent(hPort, &dwEventMask, NULL);
char TempChar;
DWORD NoBytesRead;
ReadFile( hPort,&TempChar,sizeof(TempChar),&NoBytesRead, NULL);
CloseHandle(hPort);//Closing the Serial Port
怎么了?为什么我看不懂?
谢谢
I [...] have not connected any device to the port
来自连接设备的串行端口 "reads" 字节。您没有连接的设备。因此,永远不会出现角色。
如果有环回连接(使您的计算机成为自己的连接设备),您只能读回您自己写入的字节。一些串口会支持软件配置环回,但是C#没有提供任何方法来控制这个1。否则你将需要一个hardware loopback connection(如果你禁用硬件握手,它可以像单线一样简单)
最后一个选项是虚拟串行端口驱动程序,它连接到另一个应用程序而不涉及任何硬件。
1 Win32 API 可以启用和禁用内部环回,但不幸的是它仍然既不是标准的也不是普遍支持的。参见 IOCTL_SERIAL_SET_MODEM_CONTROL
。