{------------------------------------------------------------------------------}
|
{µ¥ÔªÃû³Æ£º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.
|