下位机定义一个头:0x0a,一个尾:0x0C,中间是AD采集的数据,串口采用控件Cport3.1,直接读到一个字符串中了,本来直接使用这个控件的RxFlag事件了,即接收到0x0a后,就将其后的数据视为有效数据装入一个字符串中,但是不太准,于是我想在程序中验证收到的这个字符串的头和尾是不是0x0a和0x0c。但是编译通不过:
  
  ComPort1.ReadStr(Str, 26);  if ( (Str[1] =0x0a) and (Str[26] =0x0c) )then
  begin
  Ch1 :=StrByteToInt(Str[1]+Str[2]+Str[3]);
  Ch2 :=StrByteToInt(Str[4]+Str[5]+Str[6]);
  end;就是(Str[1] =0x0a) and (Str[26] =0x0c)这里不对,请大侠指点,谢谢

解决方案 »

  1.   

    你这样肯定不行了,字符怎么能和整数比较下,你需要转换下:如下方式
    你这样比较下
    if ( (Str[1] =chr(0x0a)) and (Str[26] =chr(0x0c)) )then 
      begin 
      Ch1 :=StrByteToInt(Str[1]+Str[2]+Str[3]); 
      Ch2 :=StrByteToInt(Str[4]+Str[5]+Str[6]); 
      end; 
      

  2.   

    上面错了,汗16禁止格式也不正确,刚才粗心了,要这样
    if ( (Str[1] =chr($0a)) and (Str[26] =chr($0c)) )then 
      begin 
      Ch1 :=StrByteToInt(Str[1]+Str[2]+Str[3]); 
      Ch2 :=StrByteToInt(Str[4]+Str[5]+Str[6]); 
      end; 
    delphi中$是16进制开始,等于C语言中的0x
      

  3.   

    谢谢 楼上
    但是还是不行啊还是这一句编译提示:
    [Error] Unit1.pas(171): ')' expected but identifier 'a' found  ComPort1.ReadStr(Str, 26);  if ( ( Str[1] =Chr(0x0a) ) and ( Str[26] =Chr(0x0c) ) )then
      begin
      Ch1 :=StrByteToInt(Str[1]+Str[2]+Str[3]);
      Ch2 :=StrByteToInt(Str[4]+Str[5]+Str[6]);
      end;