-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_thread.pas
201 lines (123 loc) · 3.58 KB
/
main_thread.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
unit main_thread;
interface
uses
windows,classes,syncobjs,sysutils, com_32m,
variables, forms , dialogs,
osdp_client ;
type CMain_Thread = class (TThread)
protected
procedure Execute();override;// íåïîñðåäñòâåííî çàïðîñû - îòâåòû
private
m_terminated:boolean; // êàïèòóëÿöèÿ
m_sect:TCriticalSection;// ñèíõðîíèçàöèÿ íà ïåðåìåííûå
m_interval:DWORD; // ïðèáëèçèòåëüíûé èíòåðâàë îïðîñà
//m_tx,m_rx:DWORD; // ñòàòèñòèêà
public
m_osdp:COSDPClient;
Disable_RX:boolean;// Äëÿ îòñûëêè - ñòàâèì â true - îïðîñ ïðåêðàòèòñÿ, çàòåì îáÿçàòåëüíî ñòàâèì â false
constructor Create();
destructor Destroy();override;
///function mf_StartService(Port1Settings:TComSettings):boolean;
function mf_StartService(ps:TComSettings):boolean;
procedure mf_StopService();
function Started():boolean;
function mf_GetPercent():extended;// ïîëó÷èòü ñòàòèñòèêó íåïðîõîæäåíèÿ
function FLAGSneed():boolean;
end;
implementation
constructor CMain_Thread.Create();
var iii:integer;
begin
inherited Create(false);
Disable_RX:=false;
m_osdp:=nil;
self.FreeOnTerminate:=false;
m_sect:=TCriticalSection.Create;
m_terminated:=true;
m_interval:=100;
self.Priority := tpLowest;
end;
destructor CMain_Thread.Destroy();
var iii:integer;
begin
mf_StopService();
m_sect.Free();
inherited Destroy;
end;
function CMain_Thread.FLAGSneed():boolean;
begin
result:= false;//self.need_Set_T_water or
//self.need_Set_T_water_back or
//self.need_Set_Start_Stop;
end;
function CMain_Thread.mf_StartService(ps:TComSettings):boolean;
//function CModbus_Thread.mf_StartService(portname:string):boolean;
begin
result:=false;
Disable_RX:=false;
m_osdp:=COSDPClient.Create();
if m_osdp=nil then exit;
///m_modbus.mf_StartService(PS);
m_osdp.mf_StartService(ps);
if (not m_osdp.Started())
then begin
m_osdp.Free;
m_osdp:=nil;
exit;
end;
m_terminated:=false;
self.Resume();
result:=true;
end;
function CMain_Thread.Started():boolean;
begin
result:=m_osdp.Started();
end;
procedure CMain_Thread.mf_StopService();
begin
if m_terminated then exit;
m_terminated:=true;
Sleep(200);
TerminateThread(self.Handle,0);
m_osdp.mf_StopService();
m_osdp.Free();
m_osdp:=nil;
end;
procedure CMain_Thread.Execute();
var dw:DWORD;
iii:integer;
good_rx:boolean;
begin
dw:=GetTickCount();
while (1=1) do
begin
while ((GetTickCount()-dw)<m_interval) and (not m_terminated) do sleep(1);
dw:=GetTickCount();
while (m_terminated) do Sleep(1);//
begin
if (Disable_RX) then continue;
end;
// ïîêà âíåøíèé ïîòîê íå ïîñêèäûâàåò ôëàãè
while (not terminated) and
(
FLAGSneed()
// *****
)
do sleep(1);
end;
end;
function CMain_Thread.mf_GetPercent():extended;// ïîëó÷èòü ñòàòèñòèêó íåïðîõîæäåíèÿ
var rez:extended;
begin
{
rez:=100;
try
self.m_sect.Enter;
if m_osdp<>nil then m_modbus.GetTXTR(m_tx,m_rx);
if (m_tx=0) then rez:=100 else rez:=(m_tx-m_rx)/0.01/m_tx;
self.m_sect.Leave;
except end;
result:=rez;
}
end;
end.