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

📄 zje_basefuncomm.pas

📁 串口数据传输,用Tcomm或MSComm控件
💻 PAS
📖 第 1 页 / 共 5 页
字号:
        for i := 1 to 8 do
        begin
          k := i*2+3;
          count_h := inttohex(receivebuf_first[k],2);
          count_l := inttohex(receivebuf_first[k+1],2);
          count_s:= '$'+ count_h + count_l;
          count_variant := count_s;
          count := strtoint(count_s);
          get_data[i] := count;
        end;
        reacho_i := 1;
      end;

      if command_type = 16    then                //非阻性标识
      begin
        for i := 1 to 16 do
        begin
          k := i*2;
          count_h := inttohex(receivebuf_first[k],2);
          count_l := inttohex(receivebuf_first[k+1],2);
          count_s:= '$'+ count_h + count_l;
          count_variant := count_s;
          count := strtoint(count_s);
          get_data_16[i] := count;
        end;
        reacho_i := 1;
      end;
    end ;


    if (info_type = $21) then
    begin
      if command_type = 8    then                //阻性标识
      begin
        for i := 1 to 8 do
        begin
          get_data[i+8] := receivebuf_first[i+4] ;
          get_data[i] := receivebuf_first[i+12] ;
        end;
        reacho_i := 1;
      end;

      if command_type = 16    then                //非阻性标识
      begin
        for i := 1 to 16 do
        begin
          get_data_16[i+16] := receivebuf_first[i+1] ;
          get_data_16[i] := receivebuf_first[i+17] ;
        end;
        reacho_i := 1;
      end;
    end;

    if (info_type = $22) then
    begin
      if command_type = 8    then                //阻性标识
      begin
        for i := 1 to 8 do
        begin
          get_data[i] := receivebuf_first[i+4] ;
          get_data[i+8] := receivebuf_first[i+14] ;
        end;
        get_data[17] := receivebuf_first[14] ;
        get_data[18] := receivebuf_first[13] ;
        reacho_i := 1;
      end;

      if command_type = 16    then                //非阻性标识
      begin
        for i := 1 to 16 do
        begin
          get_data_16[i] := receivebuf_first[i+1] ;
          get_data_16[i+16] := receivebuf_first[i+17] ;
        end;
        reacho_i := 1;
      end;
    end;

    if (info_type = $23) then     //本命令字只有阻性没有非阻性,所以不用 command_type
    begin
      get_data[1] := receivebuf_first[5];
      get_data[2] := receivebuf_first[6];
      get_data[3] := receivebuf_first[7];
      get_data[4] := receivebuf_first[8];
      get_data[5] := receivebuf_first[9];
      get_data[6] := receivebuf_first[10];
      get_data[7] := receivebuf_first[11];
      get_data[8] := receivebuf_first[12];
      reacho_i := 1;
    end;


    if (info_type = $24) then
    begin
      if command_type = 8    then                //阻性标识
      begin
        count_h := inttostr(receivebuf_first[6]);
        count_l := inttostr(receivebuf_first[7]);
        if length(count_h) = 1 then count_h := '0' + count_h ;
        if length(count_l) = 1 then count_l := '0' + count_l ;
        count_s:=  count_h + count_l;
        get_room_name :=  copy(count_s,1,1) + '-' + copy(count_s,2,3) ;

        count_h := inttohex(receivebuf_first[8],2);
        count_l := inttohex(receivebuf_first[9],2);
        count_s:= '$'+ count_h + count_l;
        count := strtoint(count_s);
        get_data[1] := count;  //电量

        get_data[2] := receivebuf_first[10];//功率
        get_data[3] := receivebuf_first[11];//功限
        get_data[4] := receivebuf_first[12];//电量差
        get_data[5] := receivebuf_first[13];//状态
        get_data[6] := receivebuf_first[14];//角度
      //  get_data[6] := receivebuf_first[15];//性质
        reacho_i := 1;
      end;

      if command_type = 16    then                //非阻性标识
      begin
        count_h := inttostr(receivebuf_first[6]);
        count_l := inttostr(receivebuf_first[7]);
        if length(count_h) = 1 then count_h := '0' + count_h ;
        if length(count_l) = 1 then count_l := '0' + count_l ;
        count_s:=  count_h + count_l;
        get_room_name :=  copy(count_s,1,1) + '-' + copy(count_s,2,3) ;

        count_h := inttohex(receivebuf_first[8],2);
        count_l := inttohex(receivebuf_first[9],2);
        count_s:= '$'+ count_h + count_l;
        count := strtoint(count_s);
        get_data[1] := count;  //电量

        get_data[2] := receivebuf_first[10];//功率
        get_data[3] := receivebuf_first[11];//功限
        get_data[4] := receivebuf_first[12];//电量差
        get_data[5] := receivebuf_first[13];//状态
        get_data[6] := receivebuf_first[14];//性质
        reacho_i := 1;
      end;
    end;


end;

procedure TCommonFunction.Delay(msecs:integer);
var
  FirstTickCount_1:longint;
begin
  FirstTickCount_1:=GetTickCount;
  repeat
  Application.ProcessMessages;
  until ((GetTickCount-FirstTickCount_1) >= Longint(msecs));
end;

procedure TCommonFunction.checksum;
var
  i:integer;
  n:integer;//除数
  sum:integer;//求和变量
  s : string;
  len : integer;
begin
  sum:=0;
  for i:=0 to 4 do
   sum:=sum+sendbuf[i];
  if sum <= 255 then  sendbuf[5]:= sum
  else if sum > 255 then
  begin
    s := inttohex(sum,2);
    len := length(s);
    s := '$' + copy(s,len-1,2);
    sendbuf[5] := strtoint(s);
  end;
end;

procedure TCommonFunction.outputdata;
begin
    sendbuf1[0] := sendbuf[0];
    comm1.OutputByte(sendbuf1);
    delay(5);

    sendbuf1[0] := sendbuf[1];
    comm1.OutputByte(sendbuf1);
    delay(5);

    sendbuf1[0] := sendbuf[2];
    comm1.OutputByte(sendbuf1);
    delay(5);

    sendbuf1[0] := sendbuf[3];
    comm1.OutputByte(sendbuf1);
    delay(5);

    sendbuf1[0] := sendbuf[4];
    comm1.OutputByte(sendbuf1);
    delay(5);

    sendbuf1[0] := sendbuf[5] ;
    comm1.OutputByte(sendbuf1);
    delay(5);

  //  sendbuf1[0] := sendbuf[6] ;
  //  comm1.OutputByte(sendbuf1);
end;


procedure TCommonFunction.purgecomm_all;
begin
  purgecomm(comm1.Handle,purge_txclear);
  purgecomm(comm1.Handle,purge_rxclear);
end;

{ *----------------------------------读涵数begin--------------------------------* }
function  TCommonFunction.ReadCommunicate(Icode_Build,Ichannel_count:integer;Scname:string;var aFquantity: array of Real;var SCommandInfo,SErrorInfo:string):Boolean;    //1读全通道剩余电量
           ////1读全通道(按通信板)剩余电量(栋号,房间数,板地址;var 每房间的剩余电量;var 主机发命令,从机发回的错误回执)
var subpv_I:integer;
begin
//sun [1..60]
  Result:=false;
  SCommandInfo:='xxx';    //主机发命令
  SErrorInfo:='yyy';      //从机发回的错误回执
//sun  aFquantity:=0;         //剩余电量初始化
  for subpv_I:=1 to Ichannel_count do
  begin
    aFquantity[subpv_I]:=99.6;     //剩余电量
  end;
  Result:=True;
end;

function  TCommonFunction.ReadSingleRoom(SRoomCname,SMachine:string;var fQuantity,fRoom_power,FLimit_Power_Read,fUseQuantity,fRoom_angel:Real;var Icode_state:integer;var SCommandInfo,SErrorInfo:string):Boolean;    //读单户数据
           //5读单户数据(用户房号,机器号;var 剩余电量,当前功率,当前功限,用电量/电量差,当前角度;var 电用状态;var 主机发命令,从机发回的错误回执)
begin
  Result:=false;
  SCommandInfo:='xxx';    //主机发命令
  SErrorInfo:='yyy';      //从机发回的错误回执
  fQuantity:=99.6;             //剩余电量
  fRoom_power:=1000;          //当前功率
  FLimit_Power_Read:=1500;   //当前功限
  fUseQuantity:=2.5;          //用电量/电量差
  fRoom_angel:=50.5;        //当前角度
  Icode_state:=1;          //电用状态
  Result:=True;
end;
{ *------------------------------读涵数end----------------------------------* }

{ *------------------------------写涵数begin--------------------------------* }
function  TCommonFunction.WriteSingleRoom(SRoomCname,SMachine:string;fFillQuantity:Real;var SCommandInfo,SErrorInfo:string):Boolean;    //7设置单户电量(加电量)
          //7设置单户电量(用户房号,机器号;用补电量;var 主机发命令,从机发回的错误回执)
begin
  Result:=false;
  SCommandInfo:='xxx';    //主机发命令
  SErrorInfo:='yyy';      //从机发回的错误回执
  Result:=True;
end;
{ *------------------------------写涵数end----------------------------------* }
function TCommonFunction.get_single_room_data_16_for_test(comm_port : string; building_id : integer; board_adress : integer;
                                  channel_no : integer;
                                  var room_name : string;
                                  var room_quantity,room_power, room_power_limit: real;
                                  var differ : real;
                                  var state,room_type :integer;
                                  var SCommandInfo,SErrorInfo:string ) : boolean;
var flag : boolean;
    hNewCommFile:   THandle;
    s : string;


    board_adress_id  : integer;



    room_id : string;
    FirstTickCount_2:longint;
    i : integer;

    count_s : string;
    count : integer;
    hei : string;
    low : string;
begin
  clear; //清空接收数据字节的数组

  if comm1.PortOpen = true then
  begin
    if comm1.CommTimer.Enabled = true then
       comm1.CommTimer.Enabled := false;
    comm1.PortOpen := false;
  end;

  comport := comm_port;
  flag := true;
  if comport='com1' then
    comm1.CommPort:=pnCOM1;

  if comport='com2' then
    comm1.CommPort:=pnCOM2;

  if comport='com3' then
    comm1.CommPort:=pnCOM3;

  if comport='com4' then
    comm1.CommPort:=pnCOM4;

  if comport='com5' then
    comm1.CommPort:=pnCOM5;

  if (comport<>'com1') and (comport<>'com2') and (comport<>'com3')
     and (comport<>'com4') and (comport<>'com5')
  then flag := false;

  if flag = false then
  begin
    SErrorInfo := '串口设定错误,串口必须设为 com1~com5 之间!!';
    result := false;
    exit;
  end;
  hNewCommFile := CreateFile( PChar(comport),
                             GENERIC_READ or GENERIC_WRITE,
                             0, {not shared}
                             nil, {no security ??}
                             OPEN_EXISTING,
                             0,{No Overlapped}
                             0 {template} );

  if hNewCommFile = INVALID_HANDLE_VALUE then
  begin
    SErrorInfo := '此端口不存在或正在为其他程序占用!!';
    result := false;
    exit;
  end
  else
  begin
    PurgeComm( hNewCommFile, PURGE_TXABORT or PURGE_RXABORT or  PURGE_TXCLEAR or PURGE_RXCLEAR ) ;    //  by hs
    CloseHandle( hNewCommFile );
    hNewCommFile := 0 ;
  end;


   for i:=1 to 3 do
   begin
     sendbuf[0] := building_id;
     sendbuf[1] := board_adress;
     sendbuf[2] := $24;
     sendbuf[3] := channel_no;
     sendbuf[4] := $00;
     sendbuf[5] := $00;
     checksum;

     SCommandInfo := get_command(sendbuf);

     reacho_i := -1;         //回响是正确回响还是错误回响的标志
     receive_flag := false;  //接收到数据的标志
     FirstTickCount:=GetTickCount; //串口开启时间

     comm1.PortOpen := true;
     purgecomm_all;
     comm1.CommTimer.Enabled := true;

     input_len := 0;
     outputdata ;
     info_type :=$24;
     command_type := 16; //非阻性标识

     watch_port_timer.Enabled := true;

     //等待两秒,看是否有回响
     FirstTickCount_2:=GetTickCount;
     repeat
       if receive_flag = true then break;
       Application.ProcessMessages;
     until ((GetTickCount-FirstTickCount_2) >= Longint(2000));

     comm1.CommTimer.Enabled := false;
     comm1.PortOpen := false;

     if receive_flag = false then continue;
     if reacho_i = -1 then continue;
     if (receive_flag = true) and (reacho_i = 0) then continue;

⌨️ 快捷键说明

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