HP\李良庭
2025-07-08 868daf94f29ce1ffdd799a68c07bb668cd373bcd
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
{------------------------------------------------------------------------------}
{µ¥ÔªÃû³Æ£ºtestThread.pas                                                      }
{Ä£¿éÃû³Æ£º×Ô¶¯¼ì²âÏ̠߳                                                       }
{Ä£¿é˵Ã÷£ºÏ̴߳´½¨ºó×Ô¶¯Æô¶¯                                                  }
{          ×Ô¶¯¼ì²âÏ̸߳ù¾ÝplcÖ¸Á¿ØÖÆË®·ÖÒÇ×Ô¶¯Íê³Éº¬Ë®Âʼì²â£¬×Ô¶¯Éϱ¨½á¹û }
{½¨Á¢ÈÕÆÚ£º2023-10-27                                                          }
{ÐÞ¸ÄÐ޸ģº2025-05-07                                                          }
{°æÈ¨ËùÓУºÀîÁ¼Í¥ liangtingli@outlook.com                                      }
{------------------------------------------------------------------------------}
//2025-05-07 ×Ô¶¯Ä£Ê½Ï£¬testThread()Ï̸߳ÄΪ×èÈûģʽ£¬Ê¹Óûص÷º¯Êý´¦ÀíËùÓÐÖ¸Áî¡£
 
unit testThread;
 
interface
 
uses
  Windows, Classes, SysUtils, PubUtils, DateUtils, ActuatorLib;
 
type
  TTestThread = class(TThread)
  private
    { Private declarations }
    t_ret : Integer;
  protected
    procedure UpdateError;
    procedure Execute; override;
  public
    constructor Create(param: Boolean=false);
    procedure Terminate;
  end;
 
implementation
 
uses
  uMain, uDM, uInit, uSaveData, Global, log4me;
 
{ TTestThread }
//--------Ï̳߳õʼ»¯----------------------------------------
//Ï̳߳õʼ»¯
constructor TTestThread.Create(param: Boolean=false);
begin
  //Ïß³ÌÍ£Ö¹ºó×Ô¶¯ÊÍ·Å
  inherited Create(param);        //ÉèÖÃÏß³ÌÔËÐÐ, False-×Ô¶¯ÔËÐÐ, True-ÊÖ¶¯Æô¶¯
  FreeOnTerminate := True;        //ÉèÖÃÏß³ÌÍ˳ö×Ô¶¯Ïú
  t_ret := 0;
  gl_log_old := '';
  gl_log_new := '';
  gl_t_cod := 0;
  gl_t_val := 0;
  gl_t_mos := 0;
end;
 
//Ïß³ÌÏú»Ùʼþ
procedure TTestThread.Terminate;
begin
  inherited;
  m_TestThread := 0;             //ÇåÀíÏ߳̾ä±ú
end;
 
//--------CallBackº¯Êý----------------------------------------------------------
//Á¸¿âϵͳͨѶ»Øµ÷º¯Êý
procedure OnMessageCallBack(Data: PWord; Size: Integer); stdcall;
var
  x0,x1,x2,x3,x4,x5 : double;   //У׼ϵÊý
  Words: PWordArray;
begin
  //´Ó»Øµ÷º¯Êý¶ÁÈ¡DataÃüÁî
  case Data^ of
    //0x01-Á¸Ê³Æ·ÖÖ¸ÄÁË
    PLC_READY_GRAIN: begin
      frmMain.lblAutoInfo.Caption := 'ÊÕµ½ÐÞ¸ÄÁ¸ÖÖÖ¸Áî.';
      frmMain.lblAutoRet.Caption := '';
      frmMain.lblAutoRet_error.Caption := '';
    end;
    //0x02-·µ»Ø×¼±¸Íê³É
    PLC_READY_FINISH: begin
      frmMain.lblAutoInfo.Caption := 'ÐÞ¸ÄÁ¸ÖÖÍê³É.';
      frmMain.lblAutoRet.Caption := '';
      frmMain.lblAutoRet_error.Caption := '';
    end;
    //0x03-¿ªÊ¼²âÊÔ
    PLC_DETECT_START: begin
      frmMain.lblAutoInfo.Caption := 'Ë®·ÖÒÇÕýÔÚ¼ì²â£¬ÇëÉÔºò......';
      frmMain.lblAutoRet.Caption := '';
      frmMain.lblAutoRet_error.Caption := '';
 
      //Á¸ÖÖ´úÂë-data[1], Ô­Ê¼º¬Ë®ÂÊ-data[3]
      Words := PWordArray(Data);
      gl_t_cod := Words[1];
      gl_t_val := Words[3];
      //ʹÓÃcod²éѯһÌõgrainÊý¾Ý
      Grain := queryGrainSql1(t_Grain, gl_t_cod);
      x0 := StrToFloat(Grain.coef[0]);
      x1 := StrToFloat(Grain.coef[1]);
      x2 := StrToFloat(Grain.coef[2]);
      x3 := StrToFloat(Grain.coef[3]);
      x4 := StrToFloat(Grain.coef[4]);
      x5 := StrToFloat(Grain.Intercept);//ÐÂÔö½Ø¾à
 
      //¼ÆË㺬ˮÂʲ¢»ØÐ´
      if gl_t_val>0 then begin
        gl_t_mos := x0 +
                 x1 * gl_t_val +
                 x2 * gl_t_val * gl_t_val +
                 x3 * gl_t_val * gl_t_val * gl_t_val +
                 x4 * gl_t_val * gl_t_val * gl_t_val * gl_t_val + x5;
        PWordArray(Data)^[3] := Word(Trunc(gl_t_mos * 100));
      end;
      //Êä³öË®·ÖÒÇԭʼֵ
      if gl_t_val<>0 then
        log4Info(Format('×Ô¶¯£º´ÓË®·ÖÒÇData½á¹¹ÖжÁÈ¡Êý¾Ý-[%d].',[gl_t_val]));
    end;
    //0x04-¼ì²âÍê±Ï
    PLC_DETECT_FINISH: begin
      //¹ÊÕÏ´úÂë-data[9]
      Words := PWordArray(Data);
      gl_t_err := Words[9];
      //Ë¢ÐÂÉ豸¹ÊÕÏÂë
      RefashFaultCode(gl_t_err);
 
      frmMain.lblAutoInfo.Caption := '¼ì²âÍê³É.';
      frmMain.lblAutoRet.Caption := Format('Á¸ÖÖ´úÂë: %d, Á¸ÖÖÃû³Æ: %s, º¬Ë®ÂÊ: %.2f',
                                          [gl_t_cod, Grain.Name, Trunc(gl_t_mos*100)/100])+'%';
      frmMain.lblAutoRet_error.Caption := '';
      //¼Ç¼Êý¾Ý¿âºÍÈÕÖ¾
      gl_log_new := Format('×Ô¶¯£º¼ì²â½á¹û-º¬Ë®ÂÊ[%f], ½Ø¾à[%s], Á¸ÖÖ´úÂë[%d], Á¸ÖÖÃû³Æ[%s].',
                          [Trunc(gl_t_mos*100)/100, Grain.Intercept, gl_t_cod, Grain.Name]);
      if gl_log_new<>gl_log_old then begin
        log4Info(gl_log_new);
        gl_log_old := gl_log_new;
      end;
    end;
    //0xFE-Ë®·ÖÒǸ´Î»
    PLC_DEVICE_WR_RESET: begin
      frmMain.lblAutoInfo.Caption := 'ÊÕµ½¸´Î»Ö¸Áî.';
      frmMain.lblAutoRet.Caption := '';
      frmMain.lblAutoRet_error.Caption := '';
      //¼Ç¼¸´Î»
      log4info('×Ô¶¯£ºÊÕµ½¸´Î»Ö¸Áî.');
    end;
    //0xFF-Ë®·ÖÒǹÊÕÏ
    PLC_DEVICE_WR_FAULT: begin
      //¹ÊÕÏ´úÂë-data[9]
      Words := PWordArray(Data);
      gl_t_err := Words[9];
      frmMain.lblAutoInfo.Caption := 'Ë®·ÖÒǹÊÕÏ£¡';
      frmMain.lblAutoRet.Caption := '';
      frmMain.lblAutoRet_error.Caption := Format('Ë®·ÖÒdzöÏÖ [%d]-¹ÊÕÏÂë, Ô­Òò: %s£¡', [gl_t_err, AutoFaultCode(gl_t_err)]);
      //¼Ç¼¹ÊÕÏÐÅÏ¢
      gl_log_new := Format('×Ô¶¯£ºË®·ÖÒdzöÏÖ [%d]-¹ÊÕÏÂë, Ô­Òò: %s£¡', [gl_t_err, AutoFaultCode(gl_t_err)]);
      if gl_log_new<>gl_log_old then begin
        log4Info(gl_log_new);
        gl_log_old := gl_log_new;
      end;
    end;
    //0x00-Õý³£
    else begin
      Words := PWordArray(Data);
      gl_t_err := Words[9];
      RefashFaultCode(gl_t_err);
    end;
  end;
end;
 
//³ö´íÏÔʾ
procedure TTestThread.UpdateError;
begin
  if t_ret>=0 then begin
    frmMain.lblAutoRet.Caption := 'Ǥ¼ñϵͳÁ¬½Ó³É¹¦£¡';
    log4info(Format('×Ô¶¯£ºÇ¤¼ñϵͳÁ¬½Ó³É¹¦£¬·µ»ØÖµ=[%d].',[t_ret]));
  end
  else begin
    frmMain.lblAutoRet_error.Caption := 'Ǥ¼ñϵͳÁ¬½Óʧ°Ü£¡';
    log4error(Format('×Ô¶¯£ºÇ¤¼ñϵͳÁ¬½Óʧ°Ü, ·µ»ØÖµ=[%d].',[t_ret]));
  end;
end;
 
//------- Ïß³ÌÈë¿Ú ----------------------------------------
procedure TTestThread.Execute;
begin
  { Place thread code here }
  try
    //Æô¶¯ÕìÌý·þÎñ¶Ë
    {t_ret := StartTcpSlave(PlcConn.port,
                           PChar(WrConn.SerialPort),
                           WrConn.baud,
                           WrConn.parity,
                           WrConn.data_bit,
                           WrConn.stop_bit,
                           60, 10,
                           @OnMessageCallBack);  }
    t_ret := StartTcpSlaveEx(hWrDev, PlcConn.port, 60, 10, @OnMessageCallBack);
    Synchronize(UpdateError);
    //·ÀÖ¹Ïß³Ì×èÈûCPU
    Sleep(1000);
  finally
    //Ïú»ÙÏß³Ì
    Terminate;
  end;
end;
 
end.