首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

救命呀!各位,帮小弟我用C++ builder写一段接收程序,小弟我以前用str.SubString()但有时候不是很方便。小弟我按书下的方法没一次成功的,救救

2013-01-01 
救命呀!各位,帮我用C++ builder写一段接收程序,我以前用str.SubString()但有时候不是很方便。我按书上的方

救命呀!各位,帮我用C++ builder写一段接收程序,我以前用str.SubString()但有时候不是很方便。我按书上的方法没一次成功的,救救我吧!
我想用MSComm控件实现接收DSP发送采集的实时数据,每次采集19个数,这19个数据都是按16进制发送过来的,小数点后最多两位,小数点前最多三位,也就是说最大的数是999.99。这19个数据得一次性接收进来,然后转换成double形式放在temp1至temp19中存放。亲们,一定要可以实现的程序,帮帮我吧,我真是搞这个东西已经精疲力尽了!如下是我自己唯一可以运行的接收程序,但不能达到我的预期目标。(因为我的程序不能实现小数的精确显示。)
double temp1=0,temp2=0,temp3=0,temp4=0,temp5=0,temp6=0,temp7=0,temp8=0,temp9=0,temp10=0,temp11=0,temp12=0,temp13=0,temp14=0,temp15=0,temp16=0,temp17=0,temp18=0,temp19=0;
    OleVariant s;//声明一个用于接收数据的OleVariant变量。
   if(MSComm1->CommEvent==comEvReceive)//   接收缓冲区中是否收到Rthreshold个字符。
     while(MSComm1->InBufferCount) //是否有字符驻留在接收缓冲区等待被取出
    {

    s=MSComm1->Input;//接收数据
    str=s.AsType(varString);//把接收到的OleVariant变量转换成AnsiString类型
    str0=str.SubString(1,1).c_str();
    str1=str.SubString(2,2).c_str();
    str2=str.SubString(4,2).c_str();
    str3=str.SubString(6,2).c_str();
    str4=str.SubString(8,2).c_str();
    str5=str.SubString(10,2).c_str();
    str6=str.SubString(12,2).c_str();
    str7=str.SubString(14,2).c_str();
    str8=str.SubString(16,2).c_str();
    str9=str.SubString(18,2).c_str();
    str10=str.SubString(20,2).c_str();
    str11=str.SubString(22,2).c_str();
    str12=str.SubString(24,2).c_str();
    str13=str.SubString(26,2).c_str();
    str14=str.SubString(28,2).c_str();
    str15=str.SubString(30,2).c_str();
    str16=str.SubString(32,2).c_str();
    str17=str.SubString(34,2).c_str();
    str18=str.SubString(36,2).c_str();
    str19=str.SubString(38,2).c_str();
 if(str0=="A")
    {
    str1="0x"+str1;
    str2="0x"+str2;
    str3="0x"+str3;
    str4="0x"+str4;
    str5="0x"+str5;
    str6="0x"+str6;
    str7="0x"+str7;
    str8="0x"+str8;
    str9="0x"+str9;
    str10="0x"+str10;
    str11="0x"+str11;
    str12="0x"+str12;
    str13="0x"+str13;
    str14="0x"+str14;
    str15="0x"+str15;
    str16="0x"+str16;
    str17="0x"+str17;
    str18="0x"+str18;
    str19="0x"+str19;


    temp1=StrToInt(str1);
    temp2=StrToInt(str2);
    temp3=StrToInt(str3);
    temp4=StrToInt(str4);
    temp5=StrToInt(str5);
    temp6=StrToInt(str6);
    temp7=StrToInt(str7);
    temp8=StrToInt(str8);
    temp9=StrToInt(str9);
    temp10=StrToInt(str10);
    temp11=StrToInt(str11);
    temp12=StrToInt(str12);
    temp13=StrToInt(str13);
    temp14=StrToInt(str14);
    temp15=StrToInt(str15);
    temp16=StrToInt(str16);
    temp17=StrToInt(str17);


    temp18=StrToInt(str18);
    temp19=StrToInt(str19);
  }
 }

[解决办法]
str这个长度不是固定的吧,19位?
[解决办法]
数据收不上来?
你这样  你每四个做一组,然后用buff[3]<<24
[解决办法]
buff[2]<<16
[解决办法]
buff[1]<<8
[解决办法]
buff[0]

热点排行