16,472
社区成员
发帖
与我相关
我的任务
分享
static long rxdatacount=0; //该变量用于接收字符计数
LONG CSCOMMDlg::OnCommunication(WPARAM ch, LPARAM port)
{
if (port <= 0 || port > 16)
return -1;
rxdatacount++; //接收的字节计数
CString strTemp;
strTemp.Format("%ld",rxdatacount);
strTemp="RX:"+strTemp;
//byte RecevieData[1000];
// unsigned short int i;
if(m_bStopDispRXData) //如果选择了“停止显示”接收数据,则返回
return -1; //注意,这种情况下,计数仍在继续,只是不显示
//若设置了“自动清空”,则达到50行后,自动清空接收编辑框中显示的数据
if((m_ctrlAutoClear.GetCheck())&&(m_ctrlReceiveData.GetLineCount()>=50))
{
m_ReceiveData.Empty();
UpdateData(FALSE);
}
//如果没有“自动清空”,数据行达到400后,也自动清空
//因为数据过多,影响接收速度,显示是最费CPU时间的操作
if(m_ctrlReceiveData.GetLineCount()>400)
{
m_ReceiveData.Empty();
m_ReceiveData="***The Length of the Text is too long, Emptied Automaticly!!!***\r\n";
UpdateData(FALSE);
}
//如果选择了"十六进制显示",则显示十六进制值
CString str;
if(m_ctrlHexReceieve.GetCheck())
str.Format("%02X ",ch);
else
str.Format("%c",ch);
//以下是将接收的字符加在字符串的最后,这里费时很多
//但考虑到数据需要保存成文件,所以没有用List Control
int nLen=m_ctrlReceiveData.GetWindowTextLength();
m_ctrlReceiveData.SetSel(nLen, nLen);
m_ctrlReceiveData.ReplaceSel(str);
nLen+=str.GetLength();
m_ReceiveData+=str;
return0;
然后我通过OnCommunication,从串口接收到一段16进制数据(数据都存在m_ReceiveData中)