![]() |
#2
ljbyan2013-12-31 11:17
void CUart12Dlg::OnButton1()
{ // TODO: Add your control notification handler code here com_open_flag=m_contrlcom.GetPortOpen(); if(com_open_flag==0) { m_contrlcom.SetCommPort(2); //选择com2 if(m_contrlcom.GetPortOpen()) m_contrlcom.SetPortOpen(FALSE); if(!m_contrlcom.GetPortOpen()) { m_contrlcom.SetPortOpen(TRUE);//打开串口 } else { AfxMessageBox("无法打开串口!");//不管怎么都跳不到这一句啊 } m_contrlcom.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位 m_contrlcom.SetInputMode(1); m_contrlcom.SetRThreshold(1); m_contrlcom.SetInputLen(0); m_contrlcom.GetInput(); /*SetupComm(hCom,1000,1000); COMMTIMEOUTS TimeOuts; TimeOuts.ReadIntervalTimeout=1000; TimeOuts.ReadTotalTimeoutConstant=5000; TimeOuts.ReadTotalTimeoutMultiplier=500; TimeOuts.WriteTotalTimeoutConstant=2000; TimeOuts.WriteTotalTimeoutMultiplier=500; SetCommTimeouts(hCom,&TimeOuts); DCB dcb; GetCommState(hCom,&dcb); dcb.BaudRate=9600; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=ONESTOPBIT; SetCommState(hCom,&dcb); PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);*/ GetDlgItem(IDC_BUTTON1)->SetWindowText("关闭串口"); } else { m_contrlcom.SetPortOpen(FALSE); GetDlgItem(IDC_BUTTON1)->SetWindowText("打开串口"); } } void CUart12Dlg::OnOnCommMscomm1() { // TODO: Add your control notification handler code here VARIANT variant_inp; COleSafeArray safearray_inp; LONG len,k; static long len_edit=0; BYTE rxdata[2048]; CString strtemp,strtempline; if(m_contrlcom.GetCommEvent()==2) { variant_inp=m_contrlcom.GetInput(); safearray_inp=variant_inp; len=safearray_inp.GetOneDimSize(); for(k=0;k<len;k++) safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组 for(k=0;k<len;k++) { BYTE bt=*(char*)(rxdata+k); if(m_ctrlHexDisplay.GetCheck()) strtemp.Format("%02X ",bt);//将字符以十六进制方式送入临时变量strtemp存放,注意这里加入一个空隔 else strtemp.Format("%c",bt);//将字符送入临时变量strtemp存放 len_edit+=strtemp.GetLength(); if(len_edit>=40) { len_edit=0; m_strRxdata+="\r\n"; } m_strRxdata+=strtemp; } } UpdateData(FALSE); } void CUart12Dlg::OnButton3() { // TODO: Add your control notification handler code here UpdateData(TRUE); //读取编辑框内容 CString rr="hello"; if(m_ctrlHexSend.GetCheck()) { CByteArray hexdata; int len=String2Hex(rr,hexdata); //此处返回的len可以用于计算发送了多少个十六进制数 m_contrlcom.SetOutput(COleVariant(hexdata)); //发送十六进制数据* } else m_contrlcom.SetOutput(COleVariant(rr));//发送ASCII字符数据 } 大侠你好,帮我看看上面这串口程序怎么了好不?我用电脑两个USB转串口,一个用在我的程序上,另一个用串口助手,为什么我这个函数发送出去的数据每次都不一样, 详细测试后发现有时候是对的,接收部分从来没出过问题, |
从“人体生理参数监测平台的开发”这篇帖子的总览可知,该监测平台与下位机的通信是通过串口实现的。关于串口的资料网上已经满天飞,已不再是什么难题,故而这里神略掉那些陈词难调的东西,直接贴出了串口主要的程序。

m_MsComm.SetCommPort(((CComboBox*)GetDlgItem(IDC_COMMPORT))->GetCurSel()+1);//设置串口号,COM1,COM3,COM3等等
m_MsComm.SetInBufferSize(1024);//设置接收缓冲区大小
m_MsComm.SetOutBufferSize(1024); //设置发送缓冲区大小
CString tmpStr,itemStr;
int index=((CComboBox*)GetDlgItem(IDC_BAUD))->GetCurSel();
((CComboBox*)GetDlgItem(IDC_BAUD))->GetLBText(index,tmpStr);
switch(((CComboBox*)GetDlgItem(IDC_CHECKBIT))->GetCurSel())
{
case 0:tmpStr=tmpStr+","+"n";break;
case 1:tmpStr=tmpStr+","+"o";break;
case 2:tmpStr=tmpStr+","+"e";break;
default:tmpStr=tmpStr+","+"n";break;
}
index=((CComboBox*)GetDlgItem(IDC_DATABIT))->GetCurSel();
((CComboBox*)GetDlgItem(IDC_DATABIT))->GetLBText(index,itemStr);
tmpStr=tmpStr+","+itemStr;
index=((CComboBox*)GetDlgItem(IDC_STOPBIT))->GetCurSel();
((CComboBox*)GetDlgItem(IDC_STOPBIT))->GetLBText(index,itemStr);
tmpStr=tmpStr+","+itemStr;
m_MsComm.SetSettings(tmpStr);//设置串口参数,如波特率,停止位,数据位宽等
m_MsComm.SetPortOpen(TRUE);//打开串口
我的监测平台软件除了一个监测主平台之外,自己还开发了一个用于仿真下位机的软件,该软件模拟下位机给监测平台发送生理数据。该仿真软件也使用了串口,这里先看看界面:
只有本站会员才能查看附件,请 登录
再上源码,有需要的下载。
只有本站会员才能查看附件,请 登录