procedure
Tmain
.
Button2Click(Sender: TObject);
var
...
begin
...
if
(send_str(main,str) <>
0
)
then
begin
Memo2
.
Text:= Memo2
.
Text + edit1
.
Text;
Memo2
.
Lines
.
Add(
''
);
end
;
end
;
end
;
procedure
class_monitor
.
Execute;
var
ee:dword;
i:
integer
;
begin
while
True
do
begin
sleep(
100
);
cs3
.
Enter;
event := tevent
.
create(
nil
,
false
,
false
,
''
);
if
WaitCommEvent(hcom, ee, @o) = NULL
then
cs3
.
Leave;
begin
i := (Read_Device(hCOM, o, Length(Input_Buffer), Input_Buffer));
if
(i <>
0
)
then
begin
Application
.
MessageBox(
'Ошибка чтения порта'
,
'Ошибка'
, MB_OK);
end
;
cs1
.
Enter;
main
.
memo1
.
Text := main
.
memo1
.
Text+decode_str();
main
.
memo1
.
Lines
.
Add(
''
);
cs1
.
Leave;
event
.
setevent;
end
;
end
;
end
;
function
send_str(Sender: TObject;str:
ansistring
):
integer
;
var
i,j:
integer
;
s,ss:
ansistring
;
begin
ss:=dopolnenie(str);
ss:=coder_mes(main,ss);
event
.
waitfor(
12
);
result := Write_Device(hCOM, o, Output_Buffer, Length(Output_Buffer));
if
(result <>
0
)
then
begin
Application
.
MessageBox(
'Ошибка записи в порт'
,
'Ошибка'
, MB_OK);
...
end
;
...
end
;
function
Write_Device(handle:
cardinal
; o: TOverlapped; Buffer: TIO_Buffer;
Byte_Number:
integer
):
integer
;
var
Byte_Count2:
cardinal
;
fSuccess:
boolean
;
Event: DWORD;
begin
fSuccess := SetCommMask(handle, (EV_BREAK
or
EV_ERR
or
EV_TXEMPTY));
if
fSuccess =
false
then
begin
Write_Device := -
1
;
Exit;
end
;
fSuccess := WriteFile(handle, Buffer, Byte_Number, Byte_Count2,
nil
);
if
fSuccess =
false
then
begin
Write_Device := -
2
;
Exit;
end
;
fSuccess := WaitCommEvent(handle, Event, @o);
if
(fSuccess =
true
)
and
((Event
and
EV_BREAK) = EV_BREAK)
then
begin
Write_Device :=
0
;
exit;
end
;
if
(fSuccess =
true
)
and
((Event
and
EV_ERR) = EV_ERR)
then
begin
Write_Device := -
4
;
exit;
end
;
if
(fSuccess =
true
)
and
((Event
and
EV_TXEMPTY) = EV_TXEMPTY)
then
begin
Write_Device :=
0
;
end
else
Write_Device := -
5
;
end
;