|
问题是用vc的程序给下位机发送读数据命令,但每次都返回两组数据,我由用vb做了一个是正常的,发送一个指令,就返回一组数据
如果先运行vc的程序,再运行vb的程序第一次会获得两组数据,以后就正常了,那位能帮忙看看
vc的接收代码:
//打开串口
BOOL CMscommDlg::CommOpen(CMSComm &MSCOMIN,int COMPortNOIN)
{
// TODO: Add your control notification handler code here
//如果串口打开则关闭
if(MSCOMIN.GetPortOpen())
{
MSCOMIN.SetPortOpen(FALSE);
}
short i;
i=(short)COMPortNOIN;
try
{
MSCOMIN.SetCommPort(i); //选择com
m_mscomm.SetInputMode(1);//1:表示以二进制方式检取数据
m_mscomm.SetInBufferSize(1024); //接收缓冲区
m_mscomm.SetOutBufferSize(1024);//发送缓冲区
m_mscomm.SetSettings(m_CommSettingStr);
m_mscomm.SetRThreshold(1); //参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
if(!m_mscomm.GetPortOpen())
{
m_mscomm.SetPortOpen(TRUE);//打开串口
}
m_mscomm.SetInputLen(0); //// 每次读取缓冲区中 所有数据
m_mscomm.GetInput();//先预读缓冲区以清除残留数据
}
catch(CException *e)
{
TCHAR errormsg[255];
e->GetErrorMessage (errormsg,255,NULL);
CString msg;
msg.Format("端口COM%i无效,请检查串口是否被其它程序占用或硬件连线错误\r\n\r\n错误描述:",i);
msg+=errormsg;
MessageBox(msg,"无法打开串口",MB_ICONSTOP);
return FALSE;
}
//保存INI
CMyInI myini;
myini.Put("COM",(int)i);
myini.Put("COMSetting",m_CommSettingStr);
//CString title;
//title.Format("智能充电架数据服务器v1.0 COM%i",(int)i);
//this->SetWindowText(title);
return TRUE;
}
//串口接收事件
void CMscommDlg::OnOnCommMscomm1()
{
if( m_mscomm.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{
COleVariant myVar;
COleSafeArray safearray_inp;
LONG len,k;
sleep(10);
myVar.Attach(m_mscomm.GetInput()); //读缓冲区
safearray_inp=myVar;
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
if (len>0)
{
for(k=0;k<len;k++)
{
safearray_inp.GetElement(&k,FrameProcesser.rxdata+k);//转换为BYTE型数组
}
FrameProcesser.StreamInput(FrameProcesser.rxdata,len);
//串口收到数据处理函数
CommRev();
}
}
}
下面是抓的包
//=======================================================用vc的===============================================================
1 [00003611] IRP_MJ_CLOSE Port Closed
2 [00000000] IRP_MJ_CREATE Port Opened - mscomm.exe
3 [00000000] IOCTL_SERIAL_SET_BAUD_RATE Baud Rate: 9600
4 [00000001] IOCTL_SERIAL_SET_LINE_CONTROL StopBits: 1, Parity: No, DataBits: 8
//发送读数据指令
5 [00000021] IRP_MJ_WRITE Length: 0007, Data: 55 AA 03 F2 01 01 F7
//返回数据 这里有问题 返回了两组 55 AA开头的
6 [00000027] IRP_MJ_READ Length: 0004, Data: 55 AA 11 F3
7 [00000028] IRP_MJ_READ Length: 0015, Data: 01 01 58 00 00 00 00 00 00 00 34 00 00 00 00
8 [00000029] IRP_MJ_READ Length: 0002, Data: 00 92
//不应该有这一组
9 [00000030] IRP_MJ_READ Length: 0009, Data: 55 AA 11 F3 01 01 58 00 00
10 [00000031] IRP_MJ_READ Length: 0012, Data: 00 00 00 00 00 34 00 00 00 00 00 92
//发送另一个读数据指令
11 [00000049] IRP_MJ_WRITE Length: 0007, Data: 55 AA 03 F2 01 02 F8
//==========================================================用vb的============================================================
1 [00001216] IRP_MJ_CLOSE Port Closed
2 [00000000] IRP_MJ_CREATE Port Opened - HD_vbserial1.exe
3 [00000000] IOCTL_SERIAL_SET_BAUD_RATE Baud Rate: 9600
4 [00000000] IOCTL_SERIAL_SET_LINE_CONTROL StopBits: 1, Parity: No, DataBits: 8
//发送读数据指令
5 [00001743] IRP_MJ_WRITE Length: 0009, Data: 55 AA 05 F2 01 01 01 01 FB
//返回数据 第一次返回了两组 55 AA开头的
6 [00001749] IRP_MJ_READ Length: 0001, Data: 55
7 [00001749] IRP_MJ_READ Length: 0001, Data: AA
8 [00001749] IRP_MJ_READ Length: 0002, Data: 11 F3
9 [00001749] IRP_MJ_READ Length: 0002, Data: 01 01
10 [00001749] IRP_MJ_READ Length: 0002, Data: 0C 00
11 [00001750] IRP_MJ_READ Length: 0001, Data: 00
12 [00001750] IRP_MJ_READ Length: 0001, Data: 00
13 [00001750] IRP_MJ_READ Length: 0001, Data: 00
14 [00001750] IRP_MJ_READ Length: 0001, Data: 00
15 [00001750] IRP_MJ_READ Length: 0001, Data: 00
16 [00001750] IRP_MJ_READ Length: 0001, Data: 00
17 [00001750] IRP_MJ_READ Length: 0001, Data: 34
18 [00001750] IRP_MJ_READ Length: 0001, Data: 00
19 [00001750] IRP_MJ_READ Length: 0001, Data: 00
20 [00001750] IRP_MJ_READ Length: 0001, Data: 00
21 [00001750] IRP_MJ_READ Length: 0003, Data: 00 00 46
22 [00001752] IRP_MJ_READ Length: 0001, Data: 55
23 [00001752] IRP_MJ_READ Length: 0002, Data: AA 11
24 [00001752] IRP_MJ_READ Length: 0002, Data: F3 01
25 [00001752] IRP_MJ_READ Length: 0001, Data: 01
26 [00001752] IRP_MJ_READ Length: 0002, Data: 0C 00
27 [00001752] IRP_MJ_READ Length: 0002, Data: 00 00
28 [00001752] IRP_MJ_READ Length: 0002, Data: 00 00
29 [00001753] IRP_MJ_READ Length: 0002, Data: 00 00
30 [00001753] IRP_MJ_READ Length: 0002, Data: 34 00
31 [00001753] IRP_MJ_READ Length: 0002, Data: 00 00
32 [00001753] IRP_MJ_READ Length: 0002, Data: 00 00
33 [00001753] IRP_MJ_READ Length: 0001, Data: 46
//发送读数据指令 以后就正确了
34 [00002052] IRP_MJ_WRITE Length: 0009, Data: 55 AA 05 F2 01 01 01 01 FB
//返回正确数据
35 [00002058] IRP_MJ_READ Length: 0001, Data: 55
36 [00002058] IRP_MJ_READ Length: 0003, Data: AA 11 F3
37 [00002058] IRP_MJ_READ Length: 0003, Data: 01 01 20
38 [00002058] IRP_MJ_READ Length: 0004, Data: 00 00 00 00
39 [00002058] IRP_MJ_READ Length: 0004, Data: 00 00 00 34
40 [00002059] IRP_MJ_READ Length: 0004, Data: 00 00 00 00
41 [00002059] IRP_MJ_READ Length: 0002, Data: 00 5A
//发送读数据指令
42 [00002186] IRP_MJ_WRITE Length: 0009, Data: 55 AA 05 F2 01 01 01 01 FB
//返回正确数据
43 [00002192] IRP_MJ_READ Length: 0001, Data: 55
44 [00002192] IRP_MJ_READ Length: 0003, Data: AA 11 F3
45 [00002192] IRP_MJ_READ Length: 0002, Data: 01 01
46 [00002192] IRP_MJ_READ Length: 0004, Data: 20 00 00 00
47 [00002192] IRP_MJ_READ Length: 0005, Data: 00 00 00 00 34
48 [00002193] IRP_MJ_READ Length: 0006, Data: 00 00 00 00 00 5A
//发送读数据指令
49 [00002232] IRP_MJ_WRITE Length: 0009, Data: 55 AA 05 F2 01 01 01 01 FB
//返回正确数据
50 [00002238] IRP_MJ_READ Length: 0001, Data: 55
51 [00002238] IRP_MJ_READ Length: 0003, Data: AA 11 F3
52 [00002238] IRP_MJ_READ Length: 0004, Data: 01 01 20 00
53 [00002239] IRP_MJ_READ Length: 0009, Data: 00 00 00 00 00 00 34 00 00
54 [00002240] IRP_MJ_READ Length: 0004, Data: 00 00 00 5A |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|