-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial_Port.pas
More file actions
316 lines (255 loc) · 8.86 KB
/
Copy pathSerial_Port.pas
File metadata and controls
316 lines (255 loc) · 8.86 KB
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
unit Serial_Port;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ComObj, ExtCtrls, Buttons, Menus, LazSerial, FileUtil,
StrUtils, EpikTimer;
type
TWMCopyData = packed record
Msg: Cardinal;
From: HWND;
CopyDataStruct: PCopyDataStruct;
Result: Longint;
end;
const
cmRxByte = wm_User+$55;
WM_COPYDATA = wm_User+$3000;
ALLBYTES = 15360;
type
{ TForm1 }
TForm1 = class(TForm)
EpikTimer1: TEpikTimer;
MainMenu1: TMainMenu;
FileMenuItem: TMenuItem;
CreateLogMenuItem: TMenuItem;
OpenDialog1: TOpenDialog;
OpenLogMenuItem: TMenuItem;
SaveDialog1: TSaveDialog;
StatusBar1: TStatusBar;
SpeedBox: TComboBox;
PortBox: TComboBox;
Label1: TLabel;
Button1: TButton;
Label2: TLabel;
DataGroup: TRadioGroup;
StopGroup: TRadioGroup;
ParityBox: TComboBox;
Label3: TLabel;
SpeedButton1: TSpeedButton;
RxMem: TMemo;
SpeedButton3: TSpeedButton;
Timer1: TTimer;
SystemSleepTimer: TTimer;
procedure CreateLogMenuItemClick(Sender: TObject);
procedure OpenLogMenuItemClick(Sender: TObject);
procedure RecivBytes(var Msg : TMessage); message cmRxByte;
procedure PortBoxEnter(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure SystemSleepTimerTimer(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;
procedure SpeedButton3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fmMain: TForm1;
autoscroll:Bool;
isStopped: Bool;
//Probably it is unnecesary here
byteCounter : Long;
wrongBytes : integer;
globalWrongBytes : Long;
LogFileName : String;
firstByte : Bool;
LogFileNameCounter : Integer;
implementation
uses MyComm, SystemSleepUnit;
{$R *.dfm}
//===================================RecivBytes===================================
procedure TForm1.WMCopyData(var Msg: TWMCopyData);
var
sText: array[0..49] of Char;
i:integer;
tmp:String;
s: PChar;
begin
// generate text from parameter
// anzuzeigenden Text aus den Parametern generieren
s := PChar(Msg.CopyDataStruct.lpData) ;
RxMem.Lines.Add(s);
{
StrLCopy(sText, Msg.CopyDataStruct.lpData, Msg.CopyDataStruct.cbData);
// write received text
for i:=0 to 49 do begin
tmp:=tmp+sText[i];
end;
RxMem.Lines.Add(tmp);
end;
}
end;
procedure TForm1.RecivBytes(var Msg: TMessage); //Messages from second thread
var
s:string;
message : PMyMsg;
begin
if firstByte = true then EpikTimer1.Start;
firstByte := false;
message := Pointer(Msg.LParam);
s := message.s;
byteCounter := byteCounter + message.bytesCounter;
wrongBytes := message.wrongBytes;
globalWrongBytes := globalWrongBytes + wrongBytes;
RxMem.Lines.Add(string(s));
RxMem.Lines.Add(IntToStr(byteCounter) + '___' + 'Error:' + FloatToStrF((wrongBytes/message.bytesCounter)*100, ffFixed, 0, 2) + ' %');
RxMem.Lines.Add('');
Timer1.Enabled := False;
Timer1.Enabled := True;
Dispose(message);
end;
procedure TForm1.CreateLogMenuItemClick(Sender: TObject);
var
txtFile : TextFile;
tmp : String;
begin
if LogFileNameCounter = 0 then
begin
SaveDialog1.Execute;
LogFileName := SaveDialog1.FileName;
assignFile(txtFile, LogFileName);
rewrite(txtFile);
closefile(txtFile);
LogFileNameCounter := LogFileNameCounter + 1;
end
else
begin
if Pos('(', LogFileName) <> 0 then
begin
Delete(LogFileName, Pos('(', LogFileName), Pos('.txt', LogFileName) - Pos('(', LogFileName));
end;
tmp := '(' + IntToStr(LogFileNameCounter) + ')';
Insert(tmp, LogFileName, Pos('.txt', LogFileName));
tmp := Copy(LogFileName, LastDelimiter('\', LogFileName) + 1, Pos('.txt', LogFileName) - LastDelimiter('\', LogFileName) - 1);
SaveDialog1.FileName := tmp;
SaveDialog1.Execute;
LogFileName := SaveDialog1.FileName;
assignFile(txtFile, LogFileName);
rewrite(txtFile);
closefile(txtFile);
LogFileNameCounter := LogFileNameCounter + 1;
end;
end;
procedure TForm1.OpenLogMenuItemClick(Sender: TObject);
begin
OpenDialog1.FileName:=LogFileName;
OpenDialog1.Execute;
end;
//===================================RecivBytes===================================
//********************************************************************************
//==================================FillCommList==================================
//Ïðîöåäóðà ïîèñêà ñâîáîäíûõ COM-ïîðòîâ
function FillCommList( List : Tstrings ): integer;
var
ComName: string;
i: integer;
pPath : pchar;
Size : Cardinal;
begin
List.Clear(); //Î÷èñòêà ïðåäûäóùèõ çíà÷åíèé
pPath := Stralloc( 256 ); //Çàäàíèå ðàçìåðà áóôåðà
try //Ïðîáóåì íàéòè ïîðòû
for i := 1 to 99 do
begin
pPath[0] := #0;
ComName := 'COM' + inttostr( i );
QueryDosDevice( pchar( ComName), pPath, Size ); //Ñïåö ôóíêöèÿ
//ïîèñêà ïîäêëþ÷åííûõ
//óñòðîéñòâ
if CompareText( pPath, '' ) <> 0 then //Äîáàâëåíèå â ñïèñîê
List.AddObject( ComName, pointer( i ) );
end;
finally
strdispose( pPath ); //Ïðîöåäóðà îñâîáîæäåíèÿ ñòðîêè
end;
result := List.Count;
end;
//==================================FillCommList==================================
//********************************************************************************
//=================================PortBoxEnter===================================
//Çàïîëíåíèå ÑÎÌ-ïîðòîà ïîñëå íàæàòèÿ íà BOX
procedure TForm1.PortBoxEnter(Sender: TObject);
begin
FillCommList(PortBox.Items);
end;
//=================================PortBoxEnter===================================
//********************************************************************************
//==================================FormCreate====================================
procedure TForm1.FormCreate(Sender: TObject);
begin
DataGroup.ItemIndex := 3;
StopGroup.ItemIndex := 1;
ParityBox.ItemIndex := 0;
//SpeedBox.ItemIndex := 8;
LogFileNameCounter := 0;
SystemSleepTimer.Enabled:=true;
end;
//==================================FormCreate====================================
//********************************************************************************
//=================================ButtonsClick===================================
procedure TForm1.Button1Click(Sender: TObject);
begin
RxMem.Lines.Clear;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
stopbits:integer;
begin
if StartService = true then ShowMessage('Com-Port Started Succesfully');
isStopped:=false;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if isStopped=false then
StopService;
end;
procedure TForm1.SystemSleepTimerTimer(Sender: TObject);
begin
SystemSleepTimer.Enabled := false;
SetThreadExecutionState(ES_CONTINUOUS or ES_SYSTEM_REQUIRED or ES_DISPLAY_REQUIRED);
SystemSleepTimer.Enabled := true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
txtFile : TextFile;
data : String;
tmp : String;
begin
Timer1.Enabled:= false;
if not FileExists(LogFileName) then begin
CreateLogMenuItemClick(Sender);
end;
AssignFile(txtFile, LogFileName);
Append(txtFile);
tmp := EpikTimer1.Elapsed.ToString;
Delete(tmp, 0, 5);
data := 'Bytes received: ' + IntToStr(byteCounter) + '; Bytes lost: ' + IntToStr(ALLBYTES - byteCounter) + '; Wrong bytes: ' + FloatToStrF((globalWrongBytes/byteCounter)*100, ffFixed, 0, 2) + ' %' +'; Timing: ' + tmp;
writeln(txtFile, data);
CloseFile(txtFile);
StatusBar1.SimpleText := data;
byteCounter := 0;
globalWrongBytes := 0;
firstByte := true;
EpikTimer1.Clear();
end;
procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
StopService;
ShowMessage('Com-Port Stopped Succesfully');
isStopped:=true;
end;
end.