-
Notifications
You must be signed in to change notification settings - Fork 6
/
lazserialsetup.pas
249 lines (215 loc) · 6.03 KB
/
lazserialsetup.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
(******************************************************
* lazSerialSetup *
* *
* written by Jurassic Pork O3/2013 *
* based on TComport TcomSetupFrm *
*****************************************************)
unit lazserialsetup;
{$mode objfpc}{$H+}
interface
uses
LCLIntf, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Buttons, LResources,lazSerial;
type
// TLazSerial setup dialog
{ TComSetupFrm }
TComSetupFrm = class(TForm)
Button1: TButton;
Button2: TButton;
ComComboBox1: TComboBox;
ComComboBox2: TComboBox;
ComComboBox3: TComboBox;
ComComboBox4: TComboBox;
ComComboBox5: TComboBox;
ComComboBox6: TComboBox;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure EditComPort(ComPort: TlazSerial);
// conversion functions
function StrToBaudRate(Str: string): TBaudRate;
function StrToStopBits(Str: string): TStopBits;
function StrToDataBits(Str: string): TDataBits;
function StrToParity(Str: string): TParity;
function StrToFlowControl(Str: string): TFlowControl;
function BaudRateToStr(BaudRate: TBaudRate): string;
function StopBitsToStr(StopBits: TStopBits): string;
function DataBitsToStr(DataBits: TDataBits): string;
function ParityToStr(Parity: TParity): string;
function FlowControlToStr(FlowControl: TFlowControl): string;
implementation
uses synaser;
const
BaudRateStrings: array[TBaudRate] of string = ('110', '300', '600',
'1200', '2400', '4800', '9600', '14400', '19200', '38400', '56000', '57600',
'115200', '128000', '230400', '256000','460800', '921600');
StopBitsStrings: array[TStopBits] of string = ('1', '1.5', '2');
DataBitsStrings: array[TDataBits] of string = ('8', '7', '6', '5');
ParityBitsStrings: array[TParity] of string = ('None', 'Odd', 'Even',
'Mark', 'Space');
FlowControlStrings: array[TFlowControl] of string = ('None',
'Software', 'HardWare');
procedure StringArrayToList(AList: TStrings; const AStrings: array of string);
var
Cpt: Integer;
begin
for Cpt := Low(AStrings) to High(AStrings) do
AList.Add(AStrings[Cpt]);
end;
// string to baud rate
function StrToBaudRate(Str: string): TBaudRate;
var
I: TBaudRate;
begin
I := Low(TBaudRate);
while (I <= High(TBaudRate)) do
begin
if UpperCase(Str) = UpperCase(BaudRateToStr(TBaudRate(I))) then
Break;
I := Succ(I);
end;
if I > High(TBaudRate) then
Result := br__9600
else
Result := I;
end;
// string to stop bits
function StrToStopBits(Str: string): TStopBits;
var
I: TStopBits;
begin
I := Low(TStopBits);
while (I <= High(TStopBits)) do
begin
if UpperCase(Str) = UpperCase(StopBitsToStr(TStopBits(I))) then
Break;
I := Succ(I);
end;
if I > High(TStopBits) then
Result := sbOne
else
Result := I;
end;
// string to data bits
function StrToDataBits(Str: string): TDataBits;
var
I: TDataBits;
begin
I := Low(TDataBits);
while (I <= High(TDataBits)) do
begin
if UpperCase(Str) = UpperCase(DataBitsToStr(I)) then
Break;
I := Succ(I);
end;
if I > High(TDataBits) then
Result := db8bits
else
Result := I;
end;
// string to parity
function StrToParity(Str: string): TParity;
var
I: TParity;
begin
I := Low(TParity);
while (I <= High(TParity)) do
begin
if UpperCase(Str) = UpperCase(ParityToStr(I)) then
Break;
I := Succ(I);
end;
if I > High(TParity) then
Result := pNone
else
Result := I;
end;
// string to flow control
function StrToFlowControl(Str: string): TFlowControl;
var
I: TFlowControl;
begin
I := Low(TFlowControl);
while (I <= High(TFlowControl)) do
begin
if UpperCase(Str) = UpperCase(FlowControlToStr(I)) then
Break;
I := Succ(I);
end;
if I > High(TFlowControl) then
Result := fcNone
else
Result := I;
end;
// baud rate to string
function BaudRateToStr(BaudRate: TBaudRate): string;
begin
Result := BaudRateStrings[BaudRate];
end;
// stop bits to string
function StopBitsToStr(StopBits: TStopBits): string;
begin
Result := StopBitsStrings[StopBits];
end;
// data bits to string
function DataBitsToStr(DataBits: TDataBits): string;
begin
Result := DataBitsStrings[DataBits];
end;
// parity to string
function ParityToStr(Parity: TParity): string;
begin
Result := ParityBitsStrings[Parity];
end;
// flow control to string
function FlowControlToStr(FlowControl: TFlowControl): string;
begin
Result := FlowControlStrings[FlowControl];
end;
procedure EditComPort(ComPort: TLazSerial);
begin
with TComSetupFrm.Create(nil) do
begin
ComComboBox1.Text := ComPort.Device;
ComComboBox2.Text := BaudRateToStr(ComPort.BaudRate);
ComComboBox3.Text := DataBitsToStr(ComPort.DataBits);
ComComBoBox4.Text := StopBitsToStr(ComPort.StopBits);
ComComBoBox5.Text := ParityToStr(ComPort.Parity);
ComComBoBox6.Text := FlowControlToStr(ComPort.FlowControl);
if ShowModal = mrOK then
begin
ComPort.Close;
ComPort.Device := ComComboBox1.Text;
ComPort.BaudRate := StrToBaudRate(ComComboBox2.Text);
ComPort.DataBits := StrToDataBits(ComComboBox3.Text);
ComPort.StopBits := StrToStopBits(ComComboBox4.Text);
ComPort.Parity := StrToParity(ComComboBox5.Text);
ComPort.FlowCOntrol := StrToFlowControl(ComComboBox6.Text);
// ComPort.Open;
end;
Free;
end;
end;
{ TComSetupFrm }
procedure TComSetupFrm.FormCreate(Sender: TObject);
begin
ComComboBox1.Items.CommaText := GetSerialPortNames();
StringArrayToList(ComComboBox2.Items,BaudRateStrings) ;
StringArrayToList(ComComboBox3.Items,DataBitsStrings) ;
StringArrayToList(ComComboBox4.Items,StopBitsStrings) ;
StringArrayToList(ComComboBox5.Items,ParityBitsStrings) ;
StringArrayToList(ComComboBox6.Items,FlowControlStrings) ;
end;
initialization
{$i lazSerialSetup.lrs}
end.