-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserialtest.pas
318 lines (245 loc) · 6.54 KB
/
serialtest.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
unit serialtest;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, GlobalConst, Platform, retromalina, mwindows, threads, simpleaudio, blcksock, winsock2;
type TSerialThread=class (TThread)
private
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
type TSerialThread2=class (TThread)
private
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
var sw:TWindow=nil;
sw2:TWindow=nil;
close:boolean=false;
type TServerThread=class (TThread)
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
type TClientThread=class (TThread)
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
implementation
var
Count:LongWord;
ch:byte;
Character:Char;
Characters:String;
i:integer;
Winsock2TCPClient:TWinsock2TCPClient;
constructor TSerialThread.create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
constructor TSerialThread2.create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TSerialThread.execute;
var testbuf:array[0..1023] of byte;
const i:integer=0;
begin
ThreadSetpriority(ThreadGetCurrent,6);
threadsleep(1);
sw:=TWindow.create(1152,800,'Serial receive');
sw.move(200,200,800,600,0,0);
sw.tc:=200;
sw.bg:=0;
sw.cls(0);
sleep(200);
initI2SMaster;
sleep(200);
//initI2Smasterreceiver;
sleep(200);
//i:=SerialOpen(1000000,SERIAL_DATA_8BIT,SERIAL_STOP_1BIT,SERIAL_PARITY_NONE,SERIAL_FLOW_NONE,0,0);
//sw.println(inttostr(i));
//for i:=1 to 6 do begin serialread(@testbuf[0],1024,count); sw.println(inttostr(i)); end;
repeat
repeat threadsleep(1) until i2stransmitted;
i2stransmitted:=false; i:=(i+1) and $7F;
// for i:=0 to 127 do
sw.print(inttohex(outbuf[i],8)+' ');
// sw.println(' ');
if keypressed then begin readkey; repeat threadsleep(100) until keypressed; readkey; end;
until sw.needclose;
sw.destroy;
sw:=nil;
end;
procedure TSerialThread2.execute;
var testbuf:array[0..1023] of byte;
t:int64;
begin
ThreadSetpriority(ThreadGetCurrent,6);
threadsleep(1);
for i:=0 to 1022 do testbuf[i]:=i mod 100;
testbuf[1023]:=141;
sw2:=TWindow.create(800,600,'Serial transmit');
sw2.move(1100,200,800,600,0,0);
sw2.tc:=24;
sw2.bg:=0;
sw2.cls(0);
i:=SerialOpen(460800,SERIAL_DATA_8BIT,SERIAL_STOP_1BIT,SERIAL_PARITY_NONE,SERIAL_FLOW_NONE,0,0);
sw2.println('waiting 2 seconds');
threadsleep(2000) ;
sw2.println('transmission start');
repeat
threadsleep(1)
// repeat threadsleep(0); until keypressed;
// ch:=readkey;
// serialwrite(@ch,1,count);
// if ch=141 then sw2.println('') else sw2.print(chr(ch));
until sw2.needclose;
sw2.destroy;
sw2:=nil;
end;
constructor TClientThread.create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
constructor TServerThread.create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TServerThread.execute;
label p999;
var sock:TUDPBlockSocket;
buf:string;
IPAddress:string;
i:integer;
// Winsock2TCPClient:TWinsock2TCPClient;
begin
sw:=TWindow.create(800,600,'Serial receive');
sw.move(200,200,800,600,0,0);
sw.tc:=200;
sw.bg:=0;
sw.cls(0);
sleep(200);
sw.println('Test');
i:=0;
REPEAT i+=1; IPAddress:=Winsock2TCPClient.LocalAddress; sw.println('Waiting for ip '+ inttostr(i)); threadsleep(500) until ((ipaddress<>'') or (i>19));
if i>19 then
begin
sw.println('No IP address available, closing.');
threadsleep(2000);
goto p999;
end;
sw.println('IP Address: '+ipaddress);
sock:=TUDPBlockSocket.create;
//sock.createsocket;
sock.bind(ipaddress,'12346');
while (not sw.needclose) and (not close) do
begin
buf:=sock.RecvPacket(1000);
if buf<>'' then sw.println(buf);
sleep(1);
end;
close:=true;
p999:
sock.destroy;
sock:=nil;
sw.destroy;
sw:=nil;
end;
procedure TClientThread.execute;
label p999;
var sock:TUDPBlockSocket;
buf:string;
IPAddress:string;
// Winsock2TCPClient:TWinsock2TCPClient;
const i:integer=0;
begin
Winsock2TCPClient:=TWinsock2TCPClient.Create;
sw2:=TWindow.create(800,600,'Network transmit');
sw2.move(1100,200,800,600,0,0);
sw2.tc:=26;
sw2.bg:=0;
sw2.cls(0);
i:=0;
REPEAT i+=1; IPAddress:=Winsock2TCPClient.LocalAddress; sw2.println('Waiting for ip '+ inttostr(i)); threadsleep(500) until ((ipaddress<>'') or (i>19));
if i>19 then
begin
sw2.println('No IP address available, closing.');
threadsleep(2000);
goto p999;
end;
sw2.println('IP Address: '+ipaddress);
i:=0;
sock:=TUDPBlockSocket.create;
sock.connect('192.168.2.2','12345');
repeat
i:=i+1;
buf:='Test string '+inttostr(i);
sw2.println(buf);
sock.sendstring(buf);
waitvbl;
until close or sw2.needclose;
close:=true;
p999:
sock.destroy;
sock:=nil;
sw2.destroy;
sw2:=nil;
end;
//uses
// GlobalTypes,
// Serial; {Include the Serial unit so we can open, read and write to the device}
{We'll need a window handle plus a couple of others.}
procedure serialtest;
var testbuf:array[0..1023] of byte;
begin
if SerialOpen(9600,SERIAL_DATA_8BIT,SERIAL_STOP_1BIT,SERIAL_PARITY_NONE,SERIAL_FLOW_NONE,0,0) = ERROR_SUCCESS then
begin
for i:=0 to 1000 do
begin
SerialRead(@Character,SizeOf(Character),Count);
if Character = #13 then
begin
// ConsoleWindowWriteLn(WindowHandle,'Received a line: ' + Characters);
if Uppercase(Characters) = 'QUIT' then
begin
{If received then say goodbye and exit our loop}
Characters:='Goodbye!' + Chr(13) + Chr(10);
SerialWrite(PChar(Characters),Length(Characters),Count);
Sleep(1000);
Break;
end;
Characters:=Characters + Chr(13) + Chr(10);
{And echo them back to the serial device using SerialWrite}
SerialWrite(PChar(Characters),Length(Characters),Count);
{Now clear the characters and wait for more}
Characters:='';
end
else
begin
{Add the character to what we have already recevied}
Characters:=Characters + Character;
end;
{No need to sleep on each loop, SerialRead will wait until data is received}
end;
{Close the serial device using SerialClose}
SerialClose;
end
else
begin
end;
end;
initialization
//SetIPAddress('Network0','192.168.2.10','255.255.255.0','192.168.2.1');
end.