📄 zje_basefuncomm.pas
字号:
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] := $25;
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;
outputdata ;
info_type :=$25;
// 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(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;
half_no := get_command_len_25 div 2 ;
for j:=1 to half_no do
begin
s := '';
if p_no = -1 then
begin
p_no := 0 ;
if (get_data_error[j] and 1) = 1 then s := s + '读剩余电量错误;';
if (get_data_error[j] and 2) = 1 then s := s + '读功率和用电状态错误;';
if (get_data_error[j] and 4) = 1 then s := s + '定时开关错误;';
if (get_data_error[j] and 8) = 1 then s := s + '单用户解过载失败;';
if (get_data_error[j] and 16) = 1 then s := s + '单用户透支用电失败;';
if (get_data_error[j] and 32) = 1 then s := s + '单用户关断失败;';
if (get_data_error[j+half_no] and 1) = 1 then s := s + ' 通道 1 错误;';
if (get_data_error[j+half_no] and 2) = 1 then s := s + ' 通道 2 错误;';
if (get_data_error[j+half_no] and 4) = 1 then s := s + ' 通道 3 错误;';
if (get_data_error[j+half_no] and 8) = 1 then s := s + ' 通道 4 错误;';
if (get_data_error[j+half_no] and 16) = 1 then s := s + ' 通道 5 错误;';
if (get_data_error[j+half_no] and 32) = 1 then s := s + ' 通道 6 错误;';
if (get_data_error[j+half_no] and 64) = 1 then s := s + ' 通道 7 错误;';
if (get_data_error[j+half_no] and 128) = 1 then s := s + ' 通道 8 错误;';
if trim(s) <> '' then
begin
p1[p_no] := s;
p_no := p_no + 1;
end;
end
else
begin
if (get_data_error[j] and 1) = 1 then s := s + '读剩余电量错误;';
if (get_data_error[j] and 2) = 1 then s := s + '读功率和用电状态错误;';
if (get_data_error[j] and 4) = 1 then s := s + '定时开关错误;';
if (get_data_error[j] and 8) = 1 then s := s + '单用户解过载失败;';
if (get_data_error[j] and 16) = 1 then s := s + '单用户透支用电失败;';
if (get_data_error[j] and 32) = 1 then s := s + '单用户关断失败;';
if (get_data_error[j+half_no] and 1) = 1 then s := s + ' 通道 1 错误;';
if (get_data_error[j+half_no] and 2) = 1 then s := s + ' 通道 2 错误;';
if (get_data_error[j+half_no] and 4) = 1 then s := s + ' 通道 3 错误;';
if (get_data_error[j+half_no] and 8) = 1 then s := s + ' 通道 4 错误;';
if (get_data_error[j+half_no] and 16) = 1 then s := s + ' 通道 5 错误;';
if (get_data_error[j+half_no] and 32) = 1 then s := s + ' 通道 6 错误;';
if (get_data_error[j+half_no] and 64) = 1 then s := s + ' 通道 7 错误;';
if (get_data_error[j+half_no] and 128) = 1 then s := s + ' 通道 8 错误;';
if trim(s) <> '' then
begin
p1[p_no] := s;
p_no := p_no + 1;
end;
end;
end;
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_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,angle,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 := 8; //阻性标识
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[1] * 0.05;
room_power := get_data[2] * 100/6;
room_power_limit := get_data[3] * 100/6;
differ := get_data[4];
state := get_data[5];
angle := get_data[6];
// room_type := get_data[7];
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(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,angle,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}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -