LONG CMainFrame::OnCommunication(WPARAM ch, LPARAM port)
{
if(port == 4)
{
mybuff[i] = char(ch);
i++;
if(i == 1)
{
if(0xDB != mybuff[0])
{
i = 0;
//break;
}
}
if(i == 2)
{
if(0x90 != mybuff[1])
{
i = 0;
//break;
}
}
if(i == 3)
{
if(0xDB != mybuff[2])
{
i = 0;
nGetCtring = "校验错误";
}
}
if(i == 4)
{
if(0x90 != mybuff[3])
{
i = 0;
nGetCtring = "校验错误";
}
}
if(i == 10)
{
unsigned short nGetCrc = CCRC::RunCRC16(mybuff,8,0x1021);
if(nGetCrc == (mybuff[8]<<8|mybuff[9]))
{
CStringChang::Bytes2String(mybuff,10,tempBuf); //字节数据转换为可打印字符串
nGetCtring.Format("%s",tempBuf);
i=0;
}
}
if(i == 16)
{
unsigned short nGetCr = CCRC::RunCRC16(mybuff,14,0x1021);
if(nGetCr == (mybuff[14]<<8|mybuff[15]))
{
CStringChang::Bytes2String(mybuff,16,tempBuf); //字节数据转换为可打印字符串
nGetCtring.Format("%s",tempBuf);
CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
CMDIChildWnd *pChild = (CMDIChildWnd *)pFrame->GetActiveFrame();
CSerialDataView *pView = (CSerialDataView*)pChild->GetActiveView();
pView->PostMessage(WM_GETSTR_FROMFRM,(WPARAM)&nGetCtring,0); //发送消息
}
//接受消息
LRESULT CSerialDataView::OnGetStr(WPARAM wParam, LPARAM lParam)
{
CString* nGetframStr = (CString*)wParam;
unsigned char outbuff[16];
CStringChang::String2Bytes(*nGetframStr,32,outbuff);
if((*nGetframStr)!="")
{
ChangChar(outbuff);
}
return 0L;
}
i=0;
}
}
return 0;
}