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
| unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TByteArr = array of byte;
type
TForm1 = class(TForm)
Panel1: TPanel;
ComboBox1: TComboBox;
Button1: TButton;
Panel2: TPanel;
Memo1: TMemo;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
CheckBox1: TCheckBox;
Timer1: TTimer;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function OpenPort(Open_COM: integer): THandle;
function WritePort(WR_hCOM: THANDLE; Str: string): boolean;
function ReadPort(RD_hCOM: THANDLE): string;
function ByteToString(const Value: Array of Byte): String;
end;
TReadCom = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
var
Form1: TForm1;
COM: integer;
hCOM: THandle;
COMEd: boolean = False;
ReadCom: TReadCom;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if (ComboBox1.ItemIndex>=0) and (COMEd=False) then
begin
COM:=StrToInt(ComboBox1.Items.Strings[ComboBox1.ItemIndex]);
Memo1.Lines.Add('Подключение к: COM'+IntToStr(COM));
hCOM:=OpenPort(COM);
if hCOM<>INVALID_HANDLE_VALUE then
begin
Memo1.Lines.Add('COM'+IntToStr(COM)+' успешно открыт');
COMEd:=True;
ReadCom:=TReadCOM.Create(False);
ReadCom.Priority:=tpNormal;
end;
end else
Memo1.Lines.Add('Выберите порт и убедитесь, что вы уже не подключены к нему');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if COMEd=true then
begin
if ComboBox1.ItemIndex>=0 then Memo1.Lines.Add('Отключение от: COM'+IntToStr(COM));
if CloseHandle(hCOM)=true then
begin
Memo1.Lines.Add('COM'+IntToStr(COM)+' успешно закрыт');
COMEd:=False;
ReadCom.Terminate;
end
else
Memo1.Lines.Add('COM'+IntToStr(COM)+' не закрыт')
end else
Memo1.Lines.Add('Порт уже закрыт');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if COMEd=true then
begin
if Edit1.Text<>'' then
begin
if WritePort(hCOM, Edit1.Text)=true then
Memo1.Lines.Add('Отправлено в COM'+IntToStr(COM)+': '+Edit1.Text)
else
Memo1.Lines.Add('Передать информацию в '+IntToStr(COM)+' не удалось');
end;
end
else
Memo1.Lines.Add('Подключитесь к порту');
Edit1.Text:='';
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
for I := 1 to 255 do
ComboBox1.Items.Add(IntToStr(i));
end;
function TForm1.OpenPort(Open_COM: integer): THandle;
var
Open_hCOM: THandle;
dcbCOM: TDCB;
Open: boolean;
begin
Open:=True;
Open_hCOM:=CreateFile(PWideChar('COM'+IntToStr(Open_COM)), GENERIC_READ or GENERIC_WRITE, 0, nil,OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if Open_hCOM=INVALID_HANDLE_VALUE then
begin
Memo1.Lines.Add('Подключиться к порту COM'+IntToStr(COM)+' не удалось');
Open:=False;
end;
if (not GetCommState(Open_hCOM, dcbCOM)) and (Open=True) then
begin
Memo1.Lines.Add('Запросить настройки с порта COM'+IntToStr(COM)+' не удалось');
Open_hCOM:=INVALID_HANDLE_VALUE;
Open:=False;
end;
dcbCOM.BaudRate := CBR_9600;
dcbCOM.Parity := NOPARITY;
dcbCOM.ByteSize := 8;
dcbCOM.StopBits := ONESTOPBIT;
if (not SetCommState(Open_hCOM,dcbCOM)) and (Open=true) then
begin
Memo1.Lines.Add('Установить настройки для COM'+IntToStr(COM)+' не удалось');
Open_hCOM:=INVALID_HANDLE_VALUE;
Open:=False;
end;
if (not PurgeComm(Open_hCOM, PURGE_TXCLEAR or PURGE_RXCLEAR)) and (Open=true) then
begin
Memo1.Lines.Add('Сбросить COM'+IntToStr(COM)+' не удалось');
Open_hCOM:=INVALID_HANDLE_VALUE;
Open:=False;
end;
if (not SetCommMask(Open_hCOM, EV_RXCHAR)) and (Open=true) then
begin
Memo1.Lines.Add('Перевести в обычный режим COM'+IntToStr(COM)+' не удалось');
Open_hCOM:=INVALID_HANDLE_VALUE;
Open:=False;
end;
OpenPort:=Open_hCOM;
end;
function TForm1.ReadPort(RD_hCOM: THANDLE): string;
var
OverRead: TOverlapped;
read: boolean;
buf: array of Byte;
dwRead: DWORD;
begin
read:=true;
OverRead.hEvent:= CreateEvent(nil, True, False, nil);
if OverRead.hEvent=NULL then read:=False;
if not ReadFile(RD_hCOM,buf, dwRead, dwRead, @Overread) then read:=false;
ReadPort:=ByteToString(buf);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
CheckBox1.Checked:=COMEd;
end;
function TForm1.WritePort(WR_hCOM: THANDLE;Str: string): boolean;
var
dwWrite: DWORD;
OverWrite: TOverlapped;
WriteBytes: array of Byte;
Write: boolean;
i: integer;
begin
Write:=True;
//Строка в массив байт
SetLength(WriteBytes, Length(str));
for I := 0 to Length(str) - 1 do
WriteBytes[I] := ord(str[I + 1]) - 48;
OverWrite.hEvent := CreateEvent(nil, True, False, nil);
if OverWrite.hEvent = Null then write:= false;
if (not WriteFile(WR_hCOM, WriteBytes, SizeOf(WriteBytes),dwWrite, @OverWrite)) and (GetLastError <> ERROR_IO_PENDING) then Write:=False;
WritePort:=Write;
end;
function TForm1.ByteToString(const Value: Array of Byte): String;
var
I: integer;
S : String;
Litera: char;
begin
S := '';
for I := Length(Value)-1 Downto 0 do
begin
Litera := Chr(Value[I] + 48);
S := Litera + S;
end;
Result := S;
end;
{ TReadCom }
procedure TReadCom.Execute;
var
str: string;
begin
while True do
begin
str:=Form1.ReadPort(hCOM);
if str<>'' then Form1.Memo1.Lines.Add('Принято из порта '+IntToStr(COM)+': '+str);
if ReadCom.Terminated=true then break;
end;
end;
end. |