-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunit2.pas
187 lines (165 loc) · 5.2 KB
/
unit2.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(******************************************************************************)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* This file is part of Modbus Client *)
(* *)
(* See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(******************************************************************************)
Unit Unit2;
{$MODE objfpc}{$H+}
Interface
Uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Grids, ExtCtrls;
Type
{ TForm2 }
TForm2 = Class(TForm)
Button1: TButton;
Button2: TButton;
CheckBox1: TCheckBox;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
StringGrid1: TStringGrid;
Timer1: TTimer;
Procedure Button1Click(Sender: TObject);
Procedure Button2Click(Sender: TObject);
Procedure CheckBox1Change(Sender: TObject);
Procedure Edit1Change(Sender: TObject);
Procedure FormCreate(Sender: TObject);
Procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
Const Value: String);
Procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
End;
Var
Form2: TForm2;
Implementation
Uses unit1, math;
{$R *.lfm}
Function hextointdef(HexString: String; DefaultValue: integer): integer;
Const
Chars = '0123456789ABCDEF';
Var
j, i: Integer;
Begin
Try
result := 0;
HexString := UpperCase(HexString);
For i := 1 To length(HexString) Do Begin
j := pos(HexString[i], Chars);
If j = 0 Then Begin
result := DefaultValue;
exit;
End;
result := result * 16;
result := result + (j - 1);
End;
Except
result := DefaultValue;
End;
End;
{ TForm2 }
Procedure TForm2.FormCreate(Sender: TObject);
Begin
caption := 'Register overview';
Edit1.text := '0';
Edit2.text := '10';
StringGrid1.cells[0, 0] := 'Register Nr';
StringGrid1.cells[1, 0] := 'Hex';
StringGrid1.cells[2, 0] := 'Decimal';
StringGrid1.cells[3, 0] := 'Char';
StringGrid1.ColWidths[0] := 100;
End;
Procedure TForm2.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
Const Value: String);
Var
val, i: integer;
Begin
Case acol Of
1: Begin // Edit als Hex Zahl
val := hextointdef(value, 0);
val := val And $FFFF;
Registers[strtointdef(edit1.text, 0) + arow - 1] := val;
End;
2: Begin // Edit als dezimal Zahl
val := StrToIntDef(value, 0);
If val < 0 Then val := 65536 + val;
val := val And $FFFF;
Registers[strtointdef(edit1.text, 0) + arow - 1] := val;
End;
3: Begin // Edit als Char
// Das ist noch nicht Optimal, aber geht schon mal rudimentär
val := 0;
For i := 1 To min(length(Value), 2) Do Begin
val := val * 256;
val := val + ord(Value[i]);
End;
Registers[strtointdef(edit1.text, 0) + arow - 1] := val;
End;
End;
Button2Click(Nil);
End;
Procedure TForm2.Timer1Timer(Sender: TObject);
Begin
Button2.Click;
End;
Procedure TForm2.CheckBox1Change(Sender: TObject);
Begin
timer1.enabled := CheckBox1.Checked;
End;
Procedure TForm2.Edit1Change(Sender: TObject);
Begin
Button2.Click;
End;
Procedure TForm2.Button2Click(Sender: TObject);
Var
firstReg, Count, j: integer;
s: String;
b1, b2: byte;
Begin
firstReg := StrToIntDef(Edit1.Text, 0);
Count := StrToIntDef(Edit2.Text, 10);
If (count <= 0) Or (firstReg < 0) Or (firstReg > 65535) Then exit;
If firstReg + Count > 65535 Then Begin
count := 65535 - firstReg;
End;
StringGrid1.RowCount := count + 1;
For j := firstReg To firstReg + Count - 1 Do Begin
StringGrid1.Cells[0, j - firstReg + 1] := 'Register: ' + IntToStr(j);
StringGrid1.Cells[1, j - firstReg + 1] := format('%0.4X', [Registers[j]]);
StringGrid1.Cells[2, j - firstReg + 1] := format('%d', [Registers[j]]);
b1 := (Registers[j] Shr 8) And $FF;
b2 := Registers[j] And $FF;
s := '';
If b1 In [32..255] Then Begin
s := chr(b1);
End
Else Begin
s := '';
End;
If b2 In [32..255] Then Begin
s := s + chr(b2);
End
Else Begin
s := s + '';
End;
StringGrid1.Cells[3, j - firstReg + 1] := s;
End;
End;
Procedure TForm2.Button1Click(Sender: TObject);
Begin
Close;
End;
End.