void vdg1105::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
int cResult1 = 0;
int iReturn;
char szHex[10];
char buffer[1024];
int iIndex = 0;
BYTE cResult = 0;
int tmp="0";
int i="0" ;
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG k,len;
int recdata="0";
CString m_strRXData,strtemp;
BYTE rxdata[15]; //
BYTE RCda[256];
char tmphv,tmphi;
if(m_vdg1105ctrl.GetCommEvent()==2)//事件值为2表示接收缓冲区内有字符
{
m_vdg1105ctrl.SetInputLen(0); //读取控件中缓冲区中全部内容
variant_inp=m_vdg1105ctrl.GetInput(); //读缓冲区
safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
len = safearray_inp.GetOneDimSize();//得到有效数据长度
for (k = 0; k < len; k++)
{
safearray_inp.GetElement(&k, &rxdata[k]); //转换为BYTE型数组
}
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k); //字符型
strtemp.Format("%c ",bt); //将字符送入临时变量strtemp存放
m_strRXData+=strtemp; //加入接收编辑框对应字符串
if (recall)
{
stateok_two = true;
recall = false;
goto End;
}
if (stateok_two)
{
if (rxdata[k] == 0xf1)
{
stateok_two=false;
goto End;
}
}
if(aaok)
{
goto B;
}
if (lenok)
{
aaok=false;
goto A;
}
if (stateok)
{
RCdaok=true;
goto C;
}
C: if (RCdaok)
{
stateok=false;
if (rxdata[k] == 0xaa)
{
aaok=true;
aaok_two =true;
// goto B;
}
else
{
RCda[comnum++]=rxdata[k];
}
goto End;
}
D: if (rxdata[k] == 0xaa)
{
aaok=true;
// goto B;
}
B: if (rxdata[k] != 0x00 && rxdata[k] == 0x07)
{
if (RCdaok)
{
recall =true;
goto VAL;
}
else
{
if (aaok_two)
{
recall =false;
RCdaok="true";
goto C;
}
else
{
stateok="false";
aaok="false";
lenok="true";
}
// goto A;
}
}
else
{
if (RCdaok)
{
stateok="false";
aaok="false";
lenok="false";
RCdaok="true";
goto End ;
}
}
A: if (rxdata[k] == 0xF1)
{
lenok=false;
//将所有后来接受的数据保存到数组,直到遇到下一次为0xaa 且0xaa后不跟0x00
stateok = true;
}
else
{
}
/* if (rxdata[k] == 0xaa && (rxdata[k+1] != 0x00 ) &&(rxdata[k+1] == 0x07 ) && rxdata[k+2] == 0xF1)
{
tmp = rxdata[k+3]<<8 ;
m_times = tmp+rxdata[k+4];
tmp = rxdata[k+5]<<8;
actwave_HV= (tmp+rxdata[k+6])/10.0;
tmp = rxdata[k+7]<<8;
actwave_HI= (tmp+rxdata[k+8])/100.0;
} */
/* if (actwave_HV >= m_Value_YDHV*1.2 || actwave_HV <= m_Value_YDHV*0.8)
{
actwave_HV = actwave_HVold;
}*/
End: ;
}
}
VAL: if (RCdaok)
{
k="comnum";
for (int i="0";i<k;i++)
{
if (RCda== 0xaa && RCda[i+1]==0x00)
{
for(int j="i"+1;j<k;j++)
RCda[j]=RCda[j+1];
}
}
tmp = RCda[0]<<8 ;
m_times = tmp+RCda[1];
tmp = RCda[2]<<8;
actwave_HV= (tmp+RCda[3])/10.0;
tmp = RCda[4]<<8;
actwave_HI= (tmp+RCda[5])/100.0;
/* if (m_times>m_timesold+1)
{
actwave_HV=actwave_HVold;
actwave_HI=actwave_HIold;
m_times=m_timesold;
}*/
m_timesold=m_times;
actwave_HVold = actwave_HV;
actwave_HIold = actwave_HI;
comnum =0;
aaok=false;
stateok=false;
lenok=false;
RCdaok="false";
}
SetDlgItemInt(IDC_EDIT7, m_times);
SetDlgItemText(IDC_EDIT8, m_strRXData);
UpdateData(false);
if (RealTimes < m_Value_YDTimes)
{
ShowHVHI();//在波形图上显示电压,电流,时间等
}
else
{
}
}
天啦,我调试了两天,一段自己写的协议解析代码,几乎崩溃的边缘!
文章评论(0条评论)
登录后参与讨论