admin 管理员组

文章数量: 887031


2024年1月18日发(作者:文件上传不了怎么办)

主窗体:

unit barcode;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls,bar_thread;

type

TForm1 = class(TForm)

Memo_scan1_bar: TMemo;

Button1: TButton;

Button2: TButton;

Edit1: TEdit;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

Barcodethread : Array[1..1] of TMXbcscaleThread ;

procedure proc_InitThreads;

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure 1Click(Sender: TObject);

begin

proc_InitThreads;

end;

procedure 2Click(Sender: TObject);

var i:integer;

begin

for i:=1 to 1 do

Barcodethread[i].Terminate;

close;

end;

procedure _InitThreads;

var i : integer;

begin

Barcodethread[1]:=(true,strtoint(),1,Memo_scan1_bar);

for i:=1 to 1 do

Barcodethread[i].Resume;

end;

end.

条码线程:

unit bar_thread;

interface

uses

SysUtils,Classes,Windows,stdctrls,constdef,Comctrls,PComm;

type

TMXbcscaleThread = class(TThread)

private

{ Private declarations 这些变量对每一个吊车的线程而言,应该都是独立的}

pw_bcscalethr_fComPort : Word;

pi_bcscalethr_fscale : Integer;

pr_wt:real;

ptmemo_scalethr_mess_response : TMemo;

pTLabel_scalethr_wt: TLabel;

ps_wt_str,ps_scalethr_mess_response : String;

procedure proc_Read_scale_Data(var s_ReadStr:String;li_len:Longint);

procedure proc_Write_scale_Data(const cs_WriteStr : String;li_len : LongInt);

procedure proc_scaleTerminate(sender : Tobject);

procedure proc_get_scale_response_MSG;

procedure proc_Update_ptmemo_scalethr_mess_response;

protected

procedure Execute; override;

public

constructor Create(b_CreateSuspended:

Boolean;i_bcscalethr_fComPort:integer;i_bcscalethr_fscale:Integer;tmemo_scalethr_mess_response:TMemo);

destructor Destroy; override;

end;

implementation

constructor (b_CreateSuspended:

Boolean;i_bcscalethr_fComPort:integer;i_bcscalethr_fscale:Integer;tmemo_scalethr_mess_response:TMemo);

begin

//Baud Rate: 2400 ;Parity : Even; Stop bit :1 bit; Data bit :7 bit

if (i_bcscalethr_fComPort<0)or(i_bcscalethr_fComPort>16) then Terminate;

pw_bcscalethr_fComPort := i_bcscalethr_fComPort;

pi_bcscalethr_fscale := i_bcscalethr_fscale;

if sio_open(pw_bcscalethr_fComPort)<0 then

Terminate;

//测试时注释

{

if SIOFT_BADPORT=-1 then

begin

ps_plcthr_mess_runstatus := 'Can not open SCALE port';

Terminate;

end;

}

if sio_ioctl(pw_bcscalethr_fComPort,B9600,bit_7 or stop_1 or P_EVEN)<0 then Terminate;

//pTLabel_scalethr_wt:=TLabel_scalethr_wt;

ptmemo_scalethr_mess_response := tmemo_scalethr_mess_response;

// Priority := tpHigher;

OnTerminate := proc_scaleTerminate;

inherited Create(True);

end;

procedure _scaleTerminate(sender : Tobject);

begin

suspend;

sio_flush(pw_bcscalethr_fComPort,2);

sio_Close(pw_bcscalethr_fComPort);

end;

destructor y;

begin

inherited Destroy;

end;

procedure _Read_scale_Data(var s_ReadStr:String;li_len:Longint);

var arc_Buf : i_scale_MAXBUFSIZE] of Char;

begin

FillChar(arc_Buf,SizeOf(arc_Buf),0);

sio_Read(pw_bcscalethr_fComPort,@arc_Buf,1024);

s_ReadStr := StrPas(pansichar(@arc_Buf));

end;

procedure _Write_scale_Data(const cs_WriteStr : String;li_len :

LongInt);

var s_tempStr : String;

begin

s_tempStr := cs_WriteStr;

sio_Write(pw_bcscalethr_fComPort,PChar(s_tempStr),li_len);

end;

procedure _Update_ptmemo_scalethr_mess_response;

begin

pTLabel_scalethr_n:='[ '+ps_scalethr_mess_response+' ]';

// if ptmemo_scalethr_mess_response=nil then Exit;

if ptmemo_scalethr_mess_>1000

ptmemo_scalethr_mess_;

ptmemo_scalethr_mess_('时间:'+timetostr(now)+'

码:'+ps_scalethr_mess_response);

end;

procedure _get_scale_response_MSG;

///含read

var s_sendstring,s_RecStr: String;

i,i_nPos,i_nlen,i_nlen1,i_nPos1: integer;

str_1,str_2,str_3,str_4,str_5,str_6,str_7,str_8:string;

s_wt_str,str,tmp_str:string;

nlen1:integer;

begin

pr_wt:=-9999;

s_SendString := 'P';

proc_Write_scale_Data(s_sendstring,length(s_sendstring));

Sleep(1000);

proc_Read_scale_Data(s_RecStr,-1);

// s_RecStr :=trim( s_RecStr);

i_nlen := Length(s_RecStr);

if i_nlen<16 then

begin

then

ps_wt_str:=floattostr(pr_wt);

ps_scalethr_mess_response := TimeToStr(time)+'--无称重数据或者数据异常--'+s_RecStr;

Synchronize(proc_Update_ptmemo_scalethr_mess_response);

sleep(1000);

Exit;

end;

//i_nPos := Pos(chr(ci_STX),s_RecStr);

i_nPos :=Pos('=',s_recstr);

// i_nPos1 := Pos('KG',s_RecStr);

i_nPos1 := 9;

//s_wt_Str := trim(Copy(s_RecStr,2 ,i_npos1 - i_npos - 1));

s_wt_Str := trim(Copy(s_RecStr,i_nPos+1 ,7));

//str_1:=copy(s_wt_str,8,1);

str_2:=copy(s_wt_str,7,1);

str_3:=copy(s_wt_str,6,1);

str_4:=copy(s_wt_str,5,1);

str_5:=copy(s_wt_str,4,1);

str_6:=copy(s_wt_str,3,1);

str_7:=copy(s_wt_str,2,1);

str_8:=copy(s_wt_str,1,1);

s_wt_str:=trim(str_2+str_3+str_4+str_5+str_6+str_7+str_8);

// s_RecStr :=trim( s_RecStr);

//ps_scalethr_mess_response := TimeToStr(time)+':'+s_RecStr;

ps_scalethr_mess_response :='Time:'+TimeTostr(time)+ '----Wt: '+s_wt_str+' Kg';

i_nlen1:=length(s_wt_Str);

// FOR i:=1 TO length(s_wt_Str) do

// if not ((s_wt_Str>'') and (copy(s_wt_Str,i,1)>='0') and ( copy(s_wt_Str,i,1)<='9') or

(copy(s_wt_Str,i,1)='.') or (copy(s_wt_Str,i,1)='-')) then exit;

if not funb_strtofloat_ok(s_wt_Str) then exit;

try

pr_wt :=strtofloat(s_wt_Str);

except on EConvertError do

pr_wt := -9999;

end;

ps_wt_str:=floattostr(pr_wt);

//g_arr_swt[pi_scalethr_fscale]:=pr_wt;

Synchronize(proc_Update_ptmemo_scalethr_mess_response);

end;

procedure e;

begin

While True do

begin

proc_get_scale_response_MSG;

sleep(ci_scaletimeInterval);

end;

end;

end.


本文标签: 线程 数据 吊车