串口有时候收不到数据,是怎么回事?

yeekai yeekai 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写了一个串口收发数据的程序,可是有时候发现明明有数据发向串口,我的程序却接收不到,这时候我用别人的程序打开关闭一下该串口,再用我的程序打开该串口就可以正常的收到数据了,请问这是怎么回事?
一下是我打开串口的代码,不知道哪里有问题,请大家指教
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;
}
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
yeekai
硬件/系统 2,644 成员 17,229 内容