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

yeekai 2007-04-11 04:43:13
我写了一个串口收发数据的程序,可是有时候发现明明有数据发向串口,我的程序却接收不到,这时候我用别人的程序打开关闭一下该串口,再用我的程序打开该串口就可以正常的收到数据了,请问这是怎么回事?
一下是我打开串口的代码,不知道哪里有问题,请大家指教
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;
}
...全文
1789 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
双杯献酒 2007-04-15
  • 打赏
  • 举报
回复
应该
BuildCommDCB(_T("9600,8,1,n"),&dcb);
双杯献酒 2007-04-15
  • 打赏
  • 举报
回复
其实设置DCB没有那么复杂,
一般用下面就可以:
DCB dcb = {sizeof(dcb)};
BuildCommDCB(&dcb,_T("9600,8,1,n"));
双杯献酒 2007-04-15
  • 打赏
  • 举报
回复
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
PortDCB.fTXContinueOnXoff = TRUE;
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;

这三个参数为什么是TRUE ?
现在一般都用FALSE吧.
大选 2007-04-13
  • 打赏
  • 举报
回复
mochen5460 2007-04-13
  • 打赏
  • 举报
回复
在读数据前加一个延时试试看,我以前也遇到过这样的问题,加个延时就好了,比如:Sleep(50)
一条晚起的虫 2007-04-13
  • 打赏
  • 举报
回复
mark,慢慢看
yeekai 2007-04-12
  • 打赏
  • 举报
回复
我加了ClearCommError()还是没用
现在是发数据没问题,但是收数据有时能收到,有时收不到,郁闷阿
corn8888 2007-04-11
  • 打赏
  • 举报
回复
up
yeekai 2007-04-11
  • 打赏
  • 举报
回复
会不会是pcb有哪项该设的我没设呢
yeekai 2007-04-11
  • 打赏
  • 举报
回复
lfchen,能说的详细写吗?我不是很明白
一条晚起的虫 2007-04-11
  • 打赏
  • 举报
回复
ClearCommError()

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧