我电路上用了芯片,把波特率可提高到921600,现用CSerialPort类编串口,发现它一次只读一个数据,这样似乎效率不行,我接上来的数据老出错,请问有哪位高手用过的这个类的,如果我要把它修改成每次读30个字节,要怎么修改?我的接收事件响应程序要怎么编?请指教....帮上忙就给分答谢...
解决方案 »
- mfc 中链表问题求助!
- 如何在dll下用异步,,分没有,,不知道还没有人来回答,,
- 急切:關於利用PCOMM傳輸文件的問題(真的很急得說,幫幫忙!)
- 欢迎高手指点
- 关于MSN插件的开发!
- 怎么把GetLastError()返回的类型转换成CString类型?
- 如何实现渐隐渐现的窗口!非常着急,希望高人帮忙,感激!
- 如何才能实现WORD那样从网页中直接把文字和图片复制到文档中?
- 怎样在进程之间传递消息!急,在线等!!
- 关于CDC的SetPixel,调试没错误,但没有输出
- vc 和c++到底有什么联系和区别 我一直迷糊中
- 一个程序每隔10分钟向服务器验证一个,我怎样通过别的程序把这个时间回避掉,记它永不验证?
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
// unsigned char RXBuff; int nlength;
unsigned char *RXBuff=new unsigned char[30]; for (;;)
{
...........
...........
if (comstat.cbInQue == 0)
{
// break out when all bytes have been read
break;
}
EnterCriticalSection(&port->m_csCommunicationSync); if (bRead)
{
nlength=min(30,comstat.cbInQue);
bResult = ReadFile(port->m_hComm, // Handle to COMM port
RXBuff, // RX Buffer Pointer
nlength, // Read one byte
&BytesRead, // Stores number of bytes read
&port->m_ov); // pointer to the m_ov structure
// deal with the error code
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) nlength);
} // end forever loop}
接收响应函数:LONG CNIRDlg::OnCommunication(WPARAM *ch, LPARAM length)
{
for(int i=0;i<length;i++)
{
m_ReceiveData+=ch[i];
} delete ch;
return 0;
}以上不行,不知道问题出在了哪里,请哪位高手指教啊,急啊!!!!
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
// unsigned char RXBuff; int nlength;
unsigned char *RXBuff=new unsigned char[30]; for (;;)
{
...........
........... if (comstat.cbInQue == 0)
{
// break out when all bytes have been read
break;
} EnterCriticalSection(&port->m_csCommunicationSync); if (bRead)
{
nlength=min(30,comstat.cbInQue);
bResult = ReadFile(port->m_hComm, // Handle to COMM port
RXBuff, // RX Buffer Pointer
nlength, // Read one byte
&BytesRead, // Stores number of bytes read
&port->m_ov); // pointer to the m_ov structure
// deal with the error code ...........
........... ::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) nlength);
} // end forever loop }
接收响应函数: LONG CNIRDlg::OnCommunication(WPARAM *ch, LPARAM length)
{
for(int i=0;i <length;i++)
{
m_ReceiveData+=ch[i];
} delete ch;
return 0;
} 以上不行,不知道问题出在了哪里,请哪位高手指教啊,急啊!!!!
{
nlength=min(30,comstat.cbInQue);
bResult = ReadFile(port->m_hComm, // Handle to COMM port
RXBuff, // RX Buffer Pointer
nlength, // Read one byte
&BytesRead, // Stores number of bytes read
&port->m_ov); // pointer to the m_ov structure
// deal with the error code
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) nlength);
} // end forever loop 这种写法当然会出错了,在串口接收数据时,从开始,只要他有数据就开始读,nlength=min(30,comstat.cbInQue);
不是30的时候,可能是20时 ,你也读,也发送消息,这样过去后数据不完整,肯定会出错了,在这里加一个判断当实际读有30个字节是否在去读和发送消息:
if (bRead)
{
nlength=min(30,comstat.cbInQue);
if(30==nlength)
{
bResult = ReadFile(port->m_hComm, // Handle to COMM port
RXBuff, // RX Buffer Pointer
nlength, // Read one byte
&BytesRead, // Stores number of bytes read
&port->m_ov); // pointer to the m_ov structure
// deal with the error code
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff, (LPARAM) nlength);
}
} // end forever loop 建议你定的读取字节最好是你数据大小的整倍数,这样有利于你OnCommunication(WPARAM *ch, LPARAM length) 分解数据,比如你数据传上来的格式是double型:1.24,2,21,3.56, 那么你定的每次读取的大小就是8+1的倍数