串口通信程序,发送一个33个字节的数据,用虚拟串口调试时可以收到33个字节,但是用板子向上发送数据时第一次只能收到8个字节的数据,请高手帮忙看看。
#define BUFFER_SIZE 512LRESULT CMFCCommAssistDlg::OnSerialMsg( WPARAM wParam, LPARAM lParam )
{
CSerial::EEvent eEvent = CSerial::EEvent(LOWORD(wParam));
if (eEvent & CSerial::EEventRecv)
{
// Create a clean buffer
DWORD dwRead;
DWORD dwTotalCount = 0;
char szData[BUFFER_SIZE + 1];
const int nBuflen = sizeof(szData)-1;
CString str;
CString strAll;
// Obtain the data from the serial port
do
{
strAll.Empty();
m_port.Read(szData,nBuflen,&dwRead);
szData[dwRead] = '\0';
dwTotalCount += dwRead;
str.Format("%s",szData);
strAll +=str;
} while (dwRead == nBuflen);
#define BUFFER_SIZE 512LRESULT CMFCCommAssistDlg::OnSerialMsg( WPARAM wParam, LPARAM lParam )
{
CSerial::EEvent eEvent = CSerial::EEvent(LOWORD(wParam));
if (eEvent & CSerial::EEventRecv)
{
// Create a clean buffer
DWORD dwRead;
DWORD dwTotalCount = 0;
char szData[BUFFER_SIZE + 1];
const int nBuflen = sizeof(szData)-1;
CString str;
CString strAll;
// Obtain the data from the serial port
do
{
strAll.Empty();
m_port.Read(szData,nBuflen,&dwRead);
szData[dwRead] = '\0';
dwTotalCount += dwRead;
str.Format("%s",szData);
strAll +=str;
} while (dwRead == nBuflen);
解决方案 »
- 在clistctrl子类中动态创建一列按钮,如何响应?
- 关于子对话框的问题
- 我能否在程序员队伍中求一立脚之地?
- 关于COM网络通信的问题,无限感激
- 一大型集团公司,在全世界有N个子公司......帮我!
- 要做指纹门禁系统!高手指点
- 按钮,和别的常用控件为什么在ToolBar基类的对话框中无法使用??高手解决!100分!
- 怎样在编译好的exe中添加菜单、工具栏,并且响音相应功能。
- 我的Visual C++6.0链接后怎么只有一个DEBUG目录,而没有RELEASE目录?
- CWebBrowser2是不是有Bug?我在AX控件中无法使用它,老是在ASSERT中说CWnd不是窗口
- 真心求教关于地址循检
- CryptoAPI 密钥问题
数据位不匹配什么意思?怎么改?
我需要读取串口的数据,然后按照协议格式解析,每一个协议帧的长度为33个字节,在芯片向串口写一帧数据后,读串口时第一次只能收到8个字节,而不是33个字节,以后读的数据大于33字节也不能进入下面的解析协议的while循环,麻烦帮我看一下#define BUFFER_SIZE 512LRESULT CMFCCommAssistDlg::OnSerialMsg( WPARAM wParam, LPARAM lParam )
{
CSerial::EEvent eEvent = CSerial::EEvent(LOWORD(wParam));
if (eEvent & CSerial::EEventRecv)
{
// Create a clean buffer
DWORD dwRead;
DWORD dwTotalCount = 0;
char szData[BUFFER_SIZE + 1];
const int nBuflen = sizeof(szData)-1;
CString str;
CString strAll;
// Obtain the data from the serial port
do
{
strAll.Empty();
m_port.Read(szData,nBuflen,&dwRead);
szData[dwRead] = '\0';
dwTotalCount += dwRead;
str.Format("%s",szData);
strAll +=str;
} while (dwRead == nBuflen);
//收到字节数大于33字节时也不会进入while循环进行解析,这是为什么?是因为事件来得太快吗?
char *pRcvData = strAll.GetBuffer();
char *pAddr,*pCheckAddr;
char Temp[11];
CString strPhoneID;
CString strLongitude;
CString strAtitude;
char xorCheck = 0;
int len = dwTotalCount;
while (len >= 33 && pRcvData != NULL)
{
if(StrChr(pRcvData,'V') && StrChr(pRcvData,'O'))
{
pAddr = StrChr(pRcvData,'V');
//进行异或校验
pCheckAddr = pAddr + 1;
xorCheck = 0;