-
Notifications
You must be signed in to change notification settings - Fork 3
/
unit1.pas
208 lines (184 loc) · 4.57 KB
/
unit1.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Synaser,
StdCtrls, ComCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
BtnSend: TButton;
BtnDump: TButton;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
LineToSend: TEdit;
Label1: TLabel;
endaddr: TEdit;
ProgressBar1: TProgressBar;
SaveDialog1: TSaveDialog;
StartAddr: TEdit;
Memo1: TMemo;
procedure BtnSendClick(Sender: TObject);
procedure BtnDumpClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure StartAddrChange(Sender: TObject);
private
{ private declarations }
fser:TBlockSerial;
function espera(que: string):boolean;
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
fser.free;
end;
procedure TForm1.FormShow(Sender: TObject);
var portname:String;
begin
portname:=Application.GetOptionValue('port');
if portname='' then
portname:='/dev/ttyUSB0';
fser:=TBlockSerial.create;
fser.LinuxLock:=false;
fser.connect(portname);
if fser.lasterror<>0 then
begin
memo1.lines.add('Error opening '+portname+': '+fser.LastErrorDesc);
exit;
end;
fser.Config(115200,8,'N',SB1,false,false);
if fser.lasterror<>0 then
begin
memo1.lines.add('Error configuring '+portname+': '+fser.LastErrorDesc);
exit;
end;
BtnSend.Enabled:=true;
BtnDump.Enabled:=true;
end;
procedure TForm1.StartAddrChange(Sender: TObject);
begin
end;
procedure TForm1.BtnSendClick(Sender: TObject);
var c:byte;
s:string;
begin
fser.SendString(LineToSend.text+chr(13));
s:='';
repeat
c:=fser.RecvByte(100);
if fser.lasterror<>ErrTimeout then s:=s+(chr(c));
until fser.lasterror=ErrTimeout;
memo1.lines.add(s);
end;
function TForm1.espera(que:string):boolean;
var c:byte;
s:string;
begin
s:='';
repeat
c:=fser.RecvByte(100);
if fser.lasterror<>ErrTimeout then
begin
s:=s+(chr(c));
//memo2.lines.add(IntToHex(c,2)+' '+chr(c));
end;
until fser.lasterror=ErrTimeout;
memo1.lines.add(s);
result:=pos(que,s)<>0;
if not result then Memo1.lines.add('****Waiting '+que+' '+inttostr(length(que))+' '+inttostr(length(s)));
end;
procedure TForm1.BtnDumpClick(Sender: TObject);
var c,c2:byte;
s:string;
i,i2:int64;
l:int64;
f:textfile;
m,counter:int64;
espe:string;
ist:integer;
ls:integer;
begin
i:=StrToInt64(startaddr.text);
l:=StrToInt64(endaddr.text)-1;
if l<i then
begin
MessageDlg('Error','End Address must be > Start Address', mtError, [mbOk],0);
exit;
end;
if not SaveDialog1.Execute then exit;
assignfile(f,SaveDialog1.Filename);
rewrite(f);
ProgressBar1.Max:=(l-i+1) div 16;
ProgressBar1.Position:=0;
while i<l do
begin
fser.SendString('r');
fser.RecvTerminated(500,{'Enter the Start Address to Read....0x'} '0x');
fser.SendString(inttohex(i,8)+chr(13));
fser.RecvTerminated(500,{'Data Length is (1) 4 Bytes (2) 2 Bytes (3) 1 Byte...'} '...');
fser.SendString('3');
fser.RecvTerminated(500,{'Enter the Count to Read....(Maximun 10000)'} ')');
m:=l-i+1;
if m>10000 then
m:=10000;
counter:=0;
fser.SendString(IntToStr(m)+chr(13));
i2:=i;
espe:='0x'+IntToHex(i2,8);
repeat
s:=fser.RecvTerminated(200,chr(13));
if copy(s,1,10)=espe then
begin
ist:=12;
ls:=length(s);
while ist<ls do
begin
c:=ord(s[ist])-ord('0');
if c>9 then
c:=c+ord('0')-ord('A')+10;
ist:=ist+1;
c2:=ord(s[ist])-ord('0');
if c2>9 then
c2:=c2+ord('0')-ord('A')+10;
c:=(c shl 4) or c2;
ist:=ist+2;
counter:=counter+1;
write(f,chr(c));
//memo1.lines.add(IntToHex(c,2));
end;
label1.caption:=espe;
i2:=i2+16;
espe:='0x'+IntToHex(i2,8);
progressbar1.StepIt;
Application.ProcessMessages;
end;
until fser.LastError=ErrTimeout;
s:=fser.RecvTerminated(200,':'); //[DANUBE Boot]:
//memo1.lines.add(s);
i:=i+m;
if counter<>m then
begin
label1.caption:=format('error counter %d m %d',[counter,m]);
break;
end else
begin
//application.processmessages;
end;
end;
closefile(f);
ProgressBar1.Position:=0;
label1.caption:=label1.caption+' done';
end;
{$R *.lfm}
end.