-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial_Port.pas
More file actions
199 lines (166 loc) · 5.97 KB
/
Copy pathSerial_Port.pas
File metadata and controls
199 lines (166 loc) · 5.97 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
unit Serial_Port;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ComObj, ExtCtrls, Buttons;
const
cmRxByte = wm_User+$55;
WM_COPYDATA = wm_User+$3000;
type
TForm1 = class(TForm)
TxEdit: TEdit;
StatusBar1: TStatusBar;
SpeedBox: TComboBox;
PortBox: TComboBox;
Label1: TLabel;
Button1: TButton;
CheckBox1: TCheckBox;
Label2: TLabel;
DataGroup: TRadioGroup;
StopGroup: TRadioGroup;
ParityBox: TComboBox;
Label3: TLabel;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
RxMem: TMemo;
SpeedButton3: TSpeedButton;
procedure RecivBytes(var Msg : TMessage); message cmRxByte;
procedure PortBoxEnter(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure WMCopyData(var Msg: TWMCopyData); message WM_COPYDATA;
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fmMain: TForm1;
autoscroll:Bool;
isStopped: Bool;
implementation
uses MyComm;
{$R *.dfm}
//===================================RecivBytes===================================
procedure TForm1.WMCopyData(var Msg: TWMCopyData);
var
sText: array[0..49] of Char;
i:integer;
tmp:String;
begin
// generate text from parameter
// anzuzeigenden Text aus den Parametern generieren
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;
procedure TForm1.RecivBytes(var Msg: TMessage); //Messages from second thread
var
s:PChar;
begin
case Msg.WParam of
//Message with pipette pressure
1: begin
s:=PChar(Pointer(Msg.LParam)^);
RxMem.Lines.Add(string(s));
if autoscroll = false then
SendMessage(RxMem.Handle, WM_VSCROLL, SB_LINEDOWN, 0)
else RxMem.SelStart:=RxMem.Lines.Count +1;
Application.ProcessMessages;
end;
//Connecting protocol
2: begin
Application.ProcessMessages;
end;
end;
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:=0;
end;
//==================================FormCreate====================================
//********************************************************************************
//=================================ButtonsClick===================================
procedure TForm1.Button1Click(Sender: TObject);
begin
RxMem.Lines.Clear;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked = true then autoscroll:= false
else autoscroll:= true;
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.SpeedButton2Click(Sender: TObject);
begin
WriteStrToPort(TxEdit.Text);
end;
procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
StopService;
ShowMessage('Com-Port Stopped Succesfully');
isStopped:=true;
end;
end.