串口有时候收不到数据,是怎么回事?
我写了一个串口收发数据的程序,可是有时候发现明明有数据发向串口,我的程序却接收不到,这时候我用别人的程序打开关闭一下该串口,再用我的程序打开该串口就可以正常的收到数据了,请问这是怎么回事?
一下是我打开串口的代码,不知道哪里有问题,请大家指教
BOOL CComPort::Create(int port, int baud_rate)
{
DWORD dwError, dwThreadID;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
//HANDLE hThread;
CString strPort;
strPort.Format("\\\\.\\COM%d", port);
m_nComportNum = port;
// Open the serial port.
m_hPort = CreateFile (strPort, // Pointer to the name of the port
GENERIC_READ|GENERIC_WRITE,
// Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, // Port attributes
NULL); // Handle to port with attribute
// to copy
// If it fails to open the port, return FALSE.
if ( m_hPort == INVALID_HANDLE_VALUE )
{
// Could not open the port.
dwError = GetLastError ();
return FALSE;
}
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
if (! GetCommState (m_hPort, &PortDCB))
return FALSE;
// Change the DCB structure settings.
PortDCB.BaudRate = baud_rate; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = FALSE; // Enable parity checking.
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement.
PortDCB.fNull = FALSE; // Disable null stripping.
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error.
PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (m_hPort, &PortDCB))
{
AfxMessageBox ("Unable to configure the serial port");
dwError = GetLastError ();
return FALSE;
}
// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (m_hPort, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 2;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
// Set the time-out parameters for all read and write operations
// on the port.
if (!SetCommTimeouts (m_hPort, &CommTimeouts))
{
// Could not create the read thread.
AfxMessageBox ("Unable to set the time-out parameters");
dwError = GetLastError ();
return FALSE;
}
// Direct the port to perform extended functions SETDTR and SETRTS.
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
EscapeCommFunction (m_hPort, SETDTR);
EscapeCommFunction (m_hPort, SETRTS);
m_hSendEvent = CreateEvent(NULL,FALSE,FALSE,NULL);//创建发送缓存读取事件量,初值为//FALSE
m_hRecvEvent = CreateEvent(NULL,FALSE,TRUE,NULL);//创建发送缓存写入事件量,初值为//TRUE
// Create a read thread for reading data from the communication port.
if (m_hReadThread = CreateThread (NULL, 0, PortReadThread, this, 0,
&dwThreadID))
{
CloseHandle (m_hReadThread);
}
else
{
// Could not create the read thread.
dwError = GetLastError ();
return FALSE;
}
if (m_hWriteThread = CreateThread (NULL, 0, PortWriteThread, this, 0,
&dwThreadID))
{
CloseHandle (m_hWriteThread);
}
else
{
// Could not create the read thread.
dwError = GetLastError ();
return FALSE;
}
m_bClose = FALSE;
return TRUE;
}
一下是我打开串口的代码,不知道哪里有问题,请大家指教
BOOL CComPort::Create(int port, int baud_rate)
{
DWORD dwError, dwThreadID;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
//HANDLE hThread;
CString strPort;
strPort.Format("\\\\.\\COM%d", port);
m_nComportNum = port;
// Open the serial port.
m_hPort = CreateFile (strPort, // Pointer to the name of the port
GENERIC_READ|GENERIC_WRITE,
// Access (read/write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, // Port attributes
NULL); // Handle to port with attribute
// to copy
// If it fails to open the port, return FALSE.
if ( m_hPort == INVALID_HANDLE_VALUE )
{
// Could not open the port.
dwError = GetLastError ();
return FALSE;
}
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
if (! GetCommState (m_hPort, &PortDCB))
return FALSE;
// Change the DCB structure settings.
PortDCB.BaudRate = baud_rate; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = FALSE; // Enable parity checking.
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement.
PortDCB.fNull = FALSE; // Disable null stripping.
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error.
PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (m_hPort, &PortDCB))
{
AfxMessageBox ("Unable to configure the serial port");
dwError = GetLastError ();
return FALSE;
}
// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (m_hPort, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 2;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
// Set the time-out parameters for all read and write operations
// on the port.
if (!SetCommTimeouts (m_hPort, &CommTimeouts))
{
// Could not create the read thread.
AfxMessageBox ("Unable to set the time-out parameters");
dwError = GetLastError ();
return FALSE;
}
// Direct the port to perform extended functions SETDTR and SETRTS.
// SETDTR: Sends the DTR (data-terminal-ready) signal.
// SETRTS: Sends the RTS (request-to-send) signal.
EscapeCommFunction (m_hPort, SETDTR);
EscapeCommFunction (m_hPort, SETRTS);
m_hSendEvent = CreateEvent(NULL,FALSE,FALSE,NULL);//创建发送缓存读取事件量,初值为//FALSE
m_hRecvEvent = CreateEvent(NULL,FALSE,TRUE,NULL);//创建发送缓存写入事件量,初值为//TRUE
// Create a read thread for reading data from the communication port.
if (m_hReadThread = CreateThread (NULL, 0, PortReadThread, this, 0,
&dwThreadID))
{
CloseHandle (m_hReadThread);
}
else
{
// Could not create the read thread.
dwError = GetLastError ();
return FALSE;
}
if (m_hWriteThread = CreateThread (NULL, 0, PortWriteThread, this, 0,
&dwThreadID))
{
CloseHandle (m_hWriteThread);
}
else
{
// Could not create the read thread.
dwError = GetLastError ();
return FALSE;
}
m_bClose = FALSE;
return TRUE;
}
...全文
相关推荐
C# 串口收发异常,偶尔有某个收不到,断点计数都断不到,原因分析(1)
“C# 串口收发异常,偶尔有某个收不到,断点计数都断不到,原因分析(1)。问题主要涉及C#的消息机制、串口通信、多线程以及Timer组件的使用。”在C#编程中,串口通信是一种常见的硬件交互方式,用
接收串口数据并将数据保存
本项目"接收串口数据并将数据保存"是针对数据采集卡的一种实践应用,其目标是实时接收并记录从串口接收到的数据。1.
串口通信 实时解析或发送串口的数据包
- 方法:`Open()`打开串口;`Close()`关闭串口;`Write()`向串口发送数据;`Read()`读取串口接收到的数据。
串口转发工具配合虚拟串口即可实现一串口数据转发到另外几个串口
串口转发工具是一种高效实用的通信技术,它允许用户通过一个物理串口接收数据,并将这些数据实时转发到其他多个串口,以便于在不同设备或应用之间共享数据。
使用Python串口实时显示数据并绘图的例子
#### 注意事项和错误解决在使用`pip`进行库安装的时候,有时候会出现一些错误,比如升级`pip`后可能出现的"‘E:\Anaconda3\Scripts\pip-script.py' is not


硬件/系统
