3,245
社区成员
发帖
与我相关
我的任务
分享
void CMyQQDlg::OnBtnStartGprs()
{
// TODO: Add your control notification handler code here
if(m_msGPRS.GetPortOpen())
m_msGPRS.SetPortOpen(FALSE);
m_msGPRS.SetCommPort(5); //选择com5
m_msGPRS.SetInBufferSize(1024);
m_msGPRS.SetOutBufferSize(512);
if( !m_msGPRS.GetPortOpen())
m_msGPRS.SetPortOpen(TRUE);//打开串口
else
AfxMessageBox("cannot open serial port");
m_msGPRS.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位
m_msGPRS.SetInputMode(1); //1:表示以二进制方式检取数据
m_msGPRS.SetRThreshold(1);
//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_msGPRS.SetInputLen(0); //设置当前接收区数据长度为0
m_msGPRS.GetInput(); //先预读缓冲区以清除残留数据
m_btnStartGPRS.EnableWindow(FALSE);
m_btnStopGPRS.EnableWindow(TRUE);
}
void CMyQQDlg::OnBtnTest()
{
// TODO: Add your control notification handler code here
CString strMsg="test";
m_msGPRS.SetOutput(COleVariant(strMsg)); //发送数据
}
BEGIN_EVENTSINK_MAP(CMyQQDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMyQQDlg)
ON_EVENT(CMyQQDlg, IDC_MSCOMM_GPRS, 1 /* OnComm */, OnOnCommMscommGprs, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
int flag =0;
void CMyQQDlg::OnOnCommMscommGprs()
{
// TODO: Add your control notification handler code here
DWORD InBufferCount;
InBufferCount=m_msGPRS.GetInBufferCount();
if(TRUE)
{
VARIANT m_input1;
COleSafeArray m_input2;
long length,i;
BYTE data[1024];
CString str ="";
CString strTemp;
if(m_msGPRS.GetCommEvent()==2)//接收缓冲区内有字符
{
m_input1=m_msGPRS.GetInput();//读取缓冲区内的数据
m_input2=m_input1;//将VARIANT型变量转换为ColeSafeArray型变量
length=m_input2.GetOneDimSize();//确定数据长度
for(i=0;i<length;i++)
m_input2.GetElement(&i,data+i); //将数据转换为BYTE型数组
for(i=0;i<length;i++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(data+i); //字符型
strTemp.Format("%c",bt); //将字符送入临时变量strtemp存放
m_strReceiveMsg +=strTemp; //加入接收编辑框对应字符串,在这儿,编辑框不是必须的,可做相应处理
char ch=(char)bt;
if(ch=='#')
{
//在此处设置一个可以接收数据的全局标志,说明接收到数据前的‘#’标志了,下一步可以读数据了,同时将m_strReceiveMsg清空
flag=1;
m_strReceiveMsg.Empty(); //下一次接收的便为有用的数据
}
if(ch=='$')
{
flag=0; //下面可以提取数据了
//先获取窗口句柄
HWND hWnd = ::FindWindow(NULL, "RSD有机溶剂在线监测仪远程控制软件");
CString strPara = _T("m_strReceiveMsg");
::SendMessage(hWnd,WM_GPRS_ADD_POINT ,0,(LPARAM)(LPCTSTR)strPara);
}
if(flag==1) //标志为1,
{
//提取数据
if (ch!='#')
{
m_strReceiveMsg +=strTemp;
}
}
}
}
UpdateData(FALSE); //-----更新编辑框内容
}
else
{
MessageBox("串口事件!");
}
}
BEGIN_EVENTSINK_MAP(CMyQQDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMyQQDlg)
ON_EVENT(CMyQQDlg, IDC_MSCOMM_GPRS, 1 /* OnComm */, OnOnCommMscommGprs, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMyQQDlg::OnOnCommMscommGprs()
{
// TODO: Add your control notification handler code here
断点==>DWORD InBufferCount;
DWORD InBufferCount;
InBufferCount=m_msGPRS.GetInBufferCount();
CString str;
str.Format("%d",InBufferCount);
AfxMessageBox(str);