⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zje_basefuncomm.pas

📁 串口数据传输,用Tcomm或MSComm控件
💻 PAS
📖 第 1 页 / 共 5 页
字号:
    end;
    data_count := comm1.DataCount;
    if comm1.DataCount >= 0 then
    begin
      x := comm1.ReadInputByte(ft);
      for i := 0 to x do
      begin
        receivebuf_first_1[i+input_len] := ft^;
        inc(ft);
      end ;
      if receivebuf_first_1[3] = 2 then
      begin
        reacho_i := 0 ;
        error_s := '接收到 6 个字节的错误回响!!' ;
        exit;
      end;
      input_len := input_len + data_count ;
      command_len_25 := receivebuf_first_1[3];
    end;

    if input_len < command_len_25 + 4 then exit;
    receive_flag := true;
    sum := 0;

    PurgeComm(comm1.Handle, PURGE_RXCLEAR);  // 清除COM 数据

    if (receivebuf_first_1[0]<>0)  then
    begin
      reacho_i := 0;
      error_s := '第一个字节不为零!!';
      exit;
    end;

    if  (receivebuf_first_1[1]<>0) then
    begin
      reacho_i := 0;
      error_s := '第二个字节不为零!!';
      exit;
    end;
    for i := 4 to command_len_25 + 4 - 2 do
    sum := sum +  receivebuf_first_1[i];

    if sum <= 255 then  sum:= sum
    else if sum > 255 then
    begin
      s := inttohex(sum,2);
      len := length(s);
      s := '$' + copy(s,len-1,2);
      sum  := strtoint(s);
    end;

    if sum <> receivebuf_first_1[command_len_25 + 4 - 1] then
    begin
      reacho_i := 0;
      error_s := '校验和不对!!';
      exit;
    end;
    reacho_i := 1;
    get_command_len_25 := command_len_25 - 1 ;
    
    for i:=4 to command_len_25 + 4 - 2 do
    begin
      get_data_error[i-3] := receivebuf_first_1[i];
    end;
    reacho_i := 1;
  end;
 //===================================================================
  if info_type = $33 then
  begin
    //第一次一定要等到 6 个字节!!
    if (input_len = 0) and (comm1.DataCount < 6) and (comm1.DataCount > 0) then
    begin
      //receive_flag := true;
      reacho_i := 0 ;
      error_s := '回响数据长度不够!!' ;
      exit;
    end;
    data_count := comm1.DataCount;
    if comm1.DataCount >= 0 then
    begin
      x := comm1.ReadInputByte(ft);
      for i := 0 to x do
      begin
        receivebuf_first[i+input_len] := ft^;
        inc(ft);
      end ;
      if receivebuf_first[3] = 2 then
      begin
        receive_flag := true;
        reacho_i := 0 ;
        error_s := '接收到 6 个字节的错误回响!!' ;
        exit;
      end;
      input_len := input_len + data_count ;
    end;

    if input_len < 13 then
    begin
      receive_flag := false;
      reacho_i := 0 ;
      error_s := '只收到 ' + inttostr(input_len) + ' 个字节,数据长度不够!!' ;
      exit;
    end;

    receive_flag := true;
    sum := 0;

    if (receivebuf_first[0]<>0)  then
    begin
      reacho_i := 0;
      error_s := '第一个字节不为零!!';
      exit;
    end;

    if  (receivebuf_first[1]<>0) then
    begin
      reacho_i := 0;
      error_s := '第二个字节不为零!!';
      exit;
    end;
    for i := 4 to input_len-2 do
    sum := sum +  receivebuf_first[i];

    if sum <= 255 then  sum:= sum
    else if sum > 255 then
    begin
      s := inttohex(sum,2);
      len := length(s);
      s := '$' + copy(s,len-1,2);
      sum  := strtoint(s);
    end;


    if sum <> receivebuf_first[input_len-1] then
    begin
      reacho_i := 0;
      error_s := '校验和不对!!';
      exit;
    end;

    reacho_i := 1;
    get_data[1] := receivebuf_first[5] ;

    count_h := inttohex(receivebuf_first[6],2);
    count_l := inttohex(receivebuf_first[7],2);
    count_s:= '$'+ count_h + count_l;
    count_variant := count_s;
    count := strtoint(count_s);
    get_data[2] := count;

    count_h := inttohex(receivebuf_first[8],2);
    count_l := inttohex(receivebuf_first[9],2);
    count_s:= '$'+ count_h + count_l;
    count_variant := count_s;
    count := strtoint(count_s);
    get_data[3] := count;

    count_h := inttohex(receivebuf_first[10],2);
    count_l := inttohex(receivebuf_first[11],2);
    count_s:= '$'+ count_h + count_l;
    count_variant := count_s;
    count := strtoint(count_s);
    get_data[4] := count;

    exit;
  end;
  }
  //=======================================================================

    if (info_type = $26) or (info_type = $27 ) or (info_type = $28 ) or (info_type = $29 ) or (info_type = $2A)
       or (info_type = $32)  or (info_type = $33) or (info_type = $34) or (info_type = $35) then
    begin
      //因为回响一定是 3 个字节,所以死等 3 个字节!!
      if (comm1.DataCount < 3) and (comm1.DataCount > 0) then
      begin
       // receive_flag := true;
        reacho_i := 0 ;
        error_s := '回响数据长度不够!!' ;
        exit;
      end;
    end;

    data_count := comm1.DataCount;



    if (info_type = $20 ) or (info_type = $21 ) or (info_type = $22 ) or (info_type = $23 )
       or (info_type = $24 ) then
    begin
      //第一次一定要等到 3 个字节!!
      if (input_len = 0) and (comm1.DataCount < 3) and (comm1.DataCount > 0) then
      begin
       // receive_flag := true;
        reacho_i := 0 ;
        error_s := '回响数据长度不够!!' ;
        exit;
      end;
      if comm1.DataCount >= 0 then
      begin
        x := comm1.ReadInputByte(ft);
        for i := 0 to x do
        begin
          receivebuf_first[i+input_len] := ft^;
          get_s := get_s  + ' ' + inttohex(ft^,2);
          inc(ft);
        end ;
        if receivebuf_first[0] = 2 then
        begin
          reacho_i := 0 ;
          error_s := '接收到 3 个字节的错误回响!!' ;
          exit;
        end;
        input_len := input_len + data_count ;
      end;
    end;

    if (info_type = $20 )  then
    begin
      if command_type = 8  then
        if input_len < 22 then exit;

      if command_type = 16  then
         if input_len < 35 then exit;
    end;

    if (info_type = $21 )  then
    begin
      if command_type = 8  then
        if input_len < 22 then exit;

      if command_type = 16  then
         if input_len < 35 then exit;
    end;

    if (info_type = $22 )  then
    begin
      if command_type = 8  then
        if input_len < 24 then exit;

      if command_type = 16  then
         if input_len < 35 then exit;
    end;

    if (info_type = $23 )  then
    begin
      if input_len < 14 then exit;
    end;

    if (info_type = $24 )  then
    begin
      if command_type = 8  then
        if input_len < 16 then exit;

      if command_type = 16  then
        if input_len < 16 then exit;
    end;

    receive_flag := true;
    sum := 0;

    if (info_type = $26) or (info_type = $27 ) or (info_type = $28 ) or (info_type = $29 ) or (info_type = $2A)
       or (info_type = $32)  or (info_type = $33) or (info_type = $34) or (info_type = $35) then
    begin
      if data_count >= 0 then
      begin
        x := comm1.ReadInputByte(ft);
        for i := 0 to x do
        begin
          receivebuf_first[i] := ft^;
          get_s := get_s  + ' ' + inttohex(ft^,2);
          inc(ft);
        end ;
        
        //这些命令回响没有和校验
        {
        for i := 1 to x-2 do
        sum := sum +  receivebuf_first[i];

        if sum <= 255 then  sum:= sum
        else if sum > 255 then
        begin
          s := inttohex(sum,2);
          len := length(s);
          s := '$' + copy(s,len-1,2);
          sum  := strtoint(s);
        end;
        }
      end;
    end;

    if (info_type = $20 ) or (info_type = $21 ) or (info_type = $22 ) or (info_type = $23 )
       or (info_type = $24 )  then
    begin
      for i := 1 to input_len-2 do
      sum := sum +  receivebuf_first[i];

      if sum <= 255 then  sum:= sum
      else if sum > 255 then
      begin
        s := inttohex(sum,2);
        len := length(s);
        s := '$' + copy(s,len-1,2);
        sum  := strtoint(s);
      end;
    end;
    
    PurgeComm(comm1.Handle, PURGE_RXCLEAR);  // 清除COM 数据
    {
    if (receivebuf_first[0]<>0)  then
    begin
      reacho_i := 0;
      error_s := '第一个字节不为零!!';
      exit;
    end;

    if  (receivebuf_first[1]<>0) then
    begin
      reacho_i := 0;
      error_s := '第二个字节不为零!!';
      exit;
    end;
    }

    //这些命令回响没有和校验
    {
    if (info_type = $26) or (info_type = $27 ) or (info_type = $28 ) or (info_type = $29 ) or (info_type = $2E )
       or (info_type = $31) or (info_type = $32) or (info_type = $2A)
       or (info_type = $2B) then
    begin
      if sum <> receivebuf_first[x-1]  then
      begin
        reacho_i := 0;
        error_s := '校验和不对!!';
        exit;
      end;
    end;
    }
    if (info_type = $20 ) or (info_type = $21 ) or (info_type = $22 ) or (info_type = $23 )
       or (info_type = $24 ) or   (info_type = $25 ) then
    begin
      if sum <> receivebuf_first[input_len-1]  then
      begin
        reacho_i := 0;
        error_s := '校验和不对!!';
        exit;
      end;
    end;

    {
    if (info_type = $20) or (info_type = $21)  or (info_type = $22) or (info_type = $23)
        or (info_type = $24)  or (info_type = $25)  then
    begin
      if sum <> receivebuf_first[input_len-1]  then
      begin
        reacho_i := 0;
        error_s := '校验和不对!!';
        exit;
      end;
    end;
    }

    if (info_type = $26) or (info_type = $27 ) or (info_type = $28 ) or (info_type = $29 ) or (info_type = $2A)
       or (info_type = $32)  or (info_type = $33) or (info_type = $34) or (info_type = $35) then
    begin
      if receivebuf_first[2] = $55 then reacho_i := 1
      else if  receivebuf_first[2]  = $AA  then reacho_i := 0
      else  if ((receivebuf_first[2] <> $55) and (receivebuf_first[2] <> $AA))  then
      begin
        reacho_i := 0;
        error_s := '标志位既不是 55, 也不是 AA !!';
        exit;
      end;

    end;

    if (info_type = $20) then
    begin
      if command_type = 8    then                //阻性标识
      begin

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -