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

📄 zje_basefuncomm.pas

📁 串口数据传输,用Tcomm或MSComm控件
💻 PAS
📖 第 1 页 / 共 5 页
字号:
     if (receive_flag = true) and (reacho_i = 1) then
     begin
       result := true;

       room_name          := get_room_name ;
       room_quantity      := get_data_16[1] * 0.05;
       room_power         := get_data_16[2] * 100/6;
       room_power_limit   := get_data_16[3] * 100/6;
       differ             := get_data_16[4];

       state              := get_data_16[5];
       room_type          := get_data_16[6];

       exit;
     end;
   end;

   if (receive_flag = true) and (reacho_i = 0) then
   begin
     result := false;
     SErrorInfo := error_s ;
     exit;
   end;

   if receive_flag = false then
   begin
     SErrorInfo := '已下发三次失败,请重新读取非阻性单用户数据!!';
     result := false;
     exit;
   end;

end;


function TCommonFunction.get_single_room_data_16(FaddQ,FSQ:real;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;
     if (receive_flag = true) and (reacho_i = 1) then
     begin
       result := true;

       room_name          := get_room_name ;
       room_quantity      := get_data_16[1] * 0.05;
       room_power         := get_data_16[2] * 100/6;
       room_power_limit   := get_data_16[3] * 100/6;
      // differ             := get_data_16[4];
       differ             := FaddQ + FSQ - room_quantity;
       state              := get_data_16[5];
       room_type          := get_data_16[6];

       exit;
     end;
   end;

   if (receive_flag = true) and (reacho_i = 0) then
   begin
     result := false;
     SErrorInfo := error_s ;
     exit;
   end;

   if receive_flag = false then
   begin
     SErrorInfo := '已下发三次失败,请重新读取非阻性单用户数据!!';
     result := false;
     exit;
   end;

end;

function TCommonFunction.get_building_info(comm_port : string; building_id : integer;
                               var board_count ,key_password: integer;
                               var count_1,count_2 : real;
                               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] := $00;
     sendbuf[2] := $33;
     sendbuf[3] := $00;
     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;
     info_type :=$33;
     outputdata ;

    // command_type := 8; //阻性标识

     watch_port_timer.Enabled := true;

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

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

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

       board_count := get_data[1];
       key_password := get_data[2];
       count_1 := get_data[3] * 0.05;
       count_2 := get_data[4] * 0.05;;
       exit;
     end;
   end;

   if (receive_flag = true) and (reacho_i = 0) then
   begin
     result := false;
     SErrorInfo := error_s ;
     exit;
   end;
   
   if receive_flag = false then
   begin
     SErrorInfo := '已下发三次失败,请重新读取公共初始化数据!!';
     result := false;
     exit;
   end;

   result := false;
   SErrorInfo := error_s ;
end;


function TCommonFunction.get_building_error(comm_port : string; building_id : integer; var p1 : array of string;
                                var SCommandInfo,SErrorInfo:string) : boolean;
var flag : boolean;
    hNewCommFile:   THandle;
    s : string;
    board_adress_id  : integer;
    room_id : string;
    FirstTickCount_2:longint;
    i,j : integer;

    count_s : string;
    count : integer;
    half_no : integer;
    p_no :integer;
begin
  clear; //清空接收数据字节的数组
  p_no := -1;
  for i:=low(p1) to High(p1) do
  p1[i] := '';

  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

⌨️ 快捷键说明

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