📄 umodbusform.pas
字号:
ResultStr := '';
if DeOrganizeMsg(rData,response) then
begin
for I := Low(response) to High(response) do
ResultStr := ResultStr + format('%0.2x',[response[I]]);
end else
ResultStr := '错误响应!';
lbStatus.Items.Add(ResultStr);
end;
end;
procedure TModbusForm.btnClearClick(Sender: TObject);
var
Msg : Pchar;
MsgByte : array of byte;
selregister : String;
registerno : Word;
tmp : byte;
Response : array of byte;
ResultStr : String;
I : integer;
WaitBox : TForm2;
begin
SetLength(MsgByte,6);
if (lbSetBool.ItemIndex=-1) then
begin
lbStatus.Items.Add('请先选择要读的寄存器');
Exit;
end;
selregister := lbSetBool.Items.Strings[lbSetBool.ItemIndex];
selregister := Copy(selregister,1,Pos(':',selregister)-1);
lbStatus.Items.Add('您要清除' + selregister + '号寄存器');
registerno := StrToInt(selregister);
MsgByte[0]:=$1;
MsgByte[1]:=$5;
//寄存器号
tmp := (registerno and $FF00) shr 8;
MsgByte[2]:=tmp;
tmp := registerno and $00FF;
MsgByte[3]:=tmp;
//数据
MsgByte[4]:=$0;
MsgByte[5]:=$0;
Msg := OrganizeMsg(MsgByte, SendLength);
SetLength(MsgByte,0);
lbStatus.Items.Add('发送命令:' + Msg);
com.SendData(Msg,Length(Msg));
hasResponse := False;
WaitForResponseTimer.Enabled := True;
WaitBox := TForm2.Create(nil);
WaitBox.Update;
WaitBox.Show;
Screen.Cursor := crHourGlass;
while WaitForResponseTimer.Enabled and not hasResponse do begin
Application.ProcessMessages;
end;
WaitBox.Close;
WaitBox.Free;
Screen.Cursor := crDefault;
if hasResponse then begin
SetLength(response,(rLen-3) div 2);
ResultStr := '';
if DeOrganizeMsg(rData,response) then
begin
for I := Low(response) to High(response) do
ResultStr := ResultStr + format('%0.2x',[response[I]]);
end else
ResultStr := '错误响应!';
lbStatus.Items.Add(ResultStr);
end;
end;
procedure TModbusForm.btnGetNumericClick(Sender: TObject);
var
Msg : Pchar;
MsgByte : array of byte;
selregister : String;
registerno : Word;
tmp : byte;
Response : array of byte;
ResultStr : String;
I : integer;
WaitBox : TForm2;
begin
SetLength(MsgByte,6);
if (lbGetNumeric.ItemIndex=-1) then
begin
lbStatus.Items.Add('请先选择要读的寄存器');
Exit;
end;
selregister := lbGetNumeric.Items.Strings[lbGetNumeric.ItemIndex];
selregister := Copy(selregister,1,Pos(':',selregister)-1);
lbStatus.Items.Add('您要读取' + selregister + '号寄存器');
registerno := StrToInt(selregister);
MsgByte[0]:=$1;
MsgByte[1]:=$3;
//寄存器号
tmp := (registerno and $FF00) shr 8;
MsgByte[2]:=tmp;
tmp := registerno and $00FF;
MsgByte[3]:=tmp;
//读取的个数
MsgByte[4]:=$0;
MsgByte[5]:=$1;
Msg := OrganizeMsg(MsgByte, SendLength);
SetLength(MsgByte,0);
lbStatus.Items.Add('发送命令:' + Msg);
com.SendData(Msg,Length(Msg));
hasResponse := False;
WaitForResponseTimer.Enabled := True;
WaitBox := TForm2.Create(nil);
WaitBox.Update;
WaitBox.Show;
Screen.Cursor := crHourGlass;
while WaitForResponseTimer.Enabled and not hasResponse do begin
Application.ProcessMessages;
end;
WaitBox.Close;
WaitBox.Free;
Screen.Cursor := crDefault;
if hasResponse then begin
SetLength(response,(rLen-3) div 2);
ResultStr := '';
if DeOrganizeMsg(rData,response) then
begin
for I := Low(response) to High(response) do
ResultStr := ResultStr + format('%0.2x',[response[I]]);
end else
ResultStr := '错误响应!';
lbStatus.Items.Add(ResultStr);
end;
end;
procedure TModbusForm.btnSetNumericClick(Sender: TObject);
var
Msg : Pchar;
MsgByte : array of byte;
selregister : String;
registerno : Word;
tmp : byte;
tmpWord : Word;
Response : array of byte;
ResultStr : String;
I : integer;
WaitBox : TForm2;
begin
SetLength(MsgByte,6);
if (lbSetNumeric.ItemIndex=-1) then
begin
lbStatus.Items.Add('请先选择要预置的寄存器');
Exit;
end;
selregister := lbSetNumeric.Items.Strings[lbSetNumeric.ItemIndex];
selregister := Copy(selregister,1,Pos(':',selregister)-1);
lbStatus.Items.Add('您要预置' + selregister + '号寄存器');
registerno := StrToInt(selregister);
MsgByte[0]:=$1;
MsgByte[1]:=$6;
//寄存器号
tmp := (registerno and $FF00) shr 8;
MsgByte[2]:=tmp;
tmp := registerno and $00FF;
MsgByte[3]:=tmp;
//数据
try
I := StrToInt(trim(edtSetValue.text));
except
lbStatus.Items.Add('请输入合法的整数');
SetLength(MsgByte,0);
Exit;
end;
tmpWord := I;
tmp := (tmpWord and $FF00) shr 8;
MsgByte[4]:=tmp;
tmp := tmpWord and $00FF;
MsgByte[5]:= tmp;
Msg := OrganizeMsg(MsgByte, SendLength);
SetLength(MsgByte,0);
lbStatus.Items.Add('发送命令:' + Msg);
com.SendData(Msg,Length(Msg));
hasResponse := False;
WaitForResponseTimer.Enabled := True;
WaitBox := TForm2.Create(nil);
WaitBox.Update;
WaitBox.Show;
Screen.Cursor := crHourGlass;
while WaitForResponseTimer.Enabled and not hasResponse do begin
Application.ProcessMessages;
end;
WaitBox.Close;
WaitBox.Free;
Screen.Cursor := crDefault;
if hasResponse then begin
SetLength(response,(rLen-3) div 2);
ResultStr := '';
if DeOrganizeMsg(rData,response) then
begin
for I := Low(response) to High(response) do
ResultStr := ResultStr + format('%0.2x',[response[I]]);
end else
ResultStr := '错误响应!';
lbStatus.Items.Add(ResultStr);
end;
end;
procedure TModbusForm.btnReadLogsClick(Sender: TObject);
var
Msg : Pchar;
MsgByte : array of byte;
//selregister : String;
registerno : Word;
tmp : byte;
Response : array of byte;
ResultStr : String;
I : integer;
WaitBox : TForm2;
begin
SetLength(MsgByte,6);
if (lbLogs.ItemIndex=-1) then
begin
lbStatus.Items.Add('请先选择要读的日志');
Exit;
end;
registerno := lbLogs.ItemIndex;
case registerno of
0 : registerno := 707;
1 : registerno := 708;
2 : registerno := 709;
3 : registerno := 710;
4 : registerno := 32;
end;
lbStatus.Items.Add('您要读日志' + lbLogs.Items.Strings[lbLogs.ItemIndex]);
MsgByte[0]:=$1;
MsgByte[1]:=$3;
//寄存器号
tmp := (registerno and $FF00) shr 8;
MsgByte[2]:=tmp;
tmp := registerno and $00FF;
MsgByte[3]:=tmp;
//读取的个数
MsgByte[4]:=$0;
MsgByte[5]:=$1;
Msg := OrganizeMsg(MsgByte, SendLength);
SetLength(MsgByte,0);
lbStatus.Items.Add('发送命令:' + Msg);
com.SendData(Msg,Length(Msg));
hasResponse := False;
WaitForResponseTimer.Enabled := True;
WaitBox := TForm2.Create(nil);
WaitBox.Update;
WaitBox.Show;
Screen.Cursor := crHourGlass;
while WaitForResponseTimer.Enabled and not hasResponse do begin
Application.ProcessMessages;
end;
WaitBox.Close;
WaitBox.Free;
Screen.Cursor := crDefault;
if hasResponse then begin
SetLength(response,(rLen-3) div 2);
ResultStr := '';
if DeOrganizeMsg(rData,response) then
begin
for I := Low(response) to High(response) do
ResultStr := ResultStr + format('%0.2x',[response[I]]);
end else
ResultStr := '错误响应!';
lbStatus.Items.Add(ResultStr);
end;
end;
procedure TModbusForm.comLineErrorEvent(Sender: TObject);
begin
lbStatus.Items.Add('Line Error');
end;
procedure TModbusForm.Button2Click(Sender: TObject);
var
ij : single;
byte1,byte2,byte3,byte4 : BYTE;
begin
ShowMessage(IEEEFLOATTODATETIMESTRING($47,$4F,$0D,$00,$48,$27,$CD,$80));
ij := IEEEFLOATTOSINGLE($41,$3C,$A1,$CB);
showMessage(floattostr(ij));
SINGLETOFOURBYTE(ij,byte1,byte2,byte3,byte4);
ShowMessage(format('%.2x %.2x %.2x %.2x',[byte1,byte2,byte3,byte4]));
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -