感谢楼上各位的回答,现在我自己写了一个线程,发现有如下问题:新创建一个线程后,给设备发送一次命令并且收到数据后,线程就报错,我根本监控不到错误出自何处。
我把所有代码都贴出来,希望高手能把我看看,我这个项目急,多谢大家了。
线程代码:
unit Unit_Thread;
interface
uses
Classes,Windows, Messages, SysUtils, Variants, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,SPComm,StrUtils;
type //定义线程对象
TComThread = class(TThread)
private
protected
procedure Execute;override;
public
FGo:Boolean;
SComm:TComm;
//线程数带一个Comm控件
SendList:TStringList;//Comm控件的命令列表
Count:Integer;
//记录命令列表中已经发送到第几条命令了
ReceiveData:Array of Integer;
//Comm收到的数据所存放的数组
ReceiveDataCount:Integer;
//comm收到的数组的个数
procedure SendData(Data:String);//发送命令函数
procedure Initial(m:Integer);
//初始化数组
procedure CommReceiveData(Sender: TObject;
Buffer: Pointer;BufferLength: Word);
//串口收到的数据
constructor Create(CommN:String;BaudR:Integer;SendL:TStringList);
//创建线程函数
destructor Destroy;
//注销线程
end;
implementation
uses Unit_DataModule,Unit_Main;
procedure TComThread.Initial(m:Integer);
var
i:Integer;
begin
For i:=0 to mdo
ReceiveData:=0;
ReceiveDataCount:=0;
end;
procedure TComThread.SendData(Data:String);
var //发送命令数据
i:Integer;
btBuffer:Array of Byte;
begin
i:=0;
Data:=Trim(Data);
SetLength(btBuffer,100);
while Data<>''do
begin
btBuffer:=byte(StrToInt('$'+Data[1]+Data[2]));
Data:=Trim(MidStr(Data,3,Length(Data)));
i:=i+1;
end;
SComm.WriteCommData(@btBuffer[0],i);
end;
procedure TComThread.CommReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
i:Integer;
r:array [0..127] of byte;
Reply:String;
begin
Reply:='';
For i:=0 to 127do
r:=0;
move(buffer^,r,BufferLength);
SetLength(ReceiveData,BufferLength);
Initial(BufferLength);
i:=0;
while (i<bufferLength)do
begin
ReceiveData[ReceiveDataCount]:=r;
ReceiveDataCount:=ReceiveDataCount+1;
Reply:=Reply+' '+IntToHex(r,2);
i:=i+1;
end;
application.ProcessMessages;
Sleep(500);
self.Resume;
end;
constructor TComThread.Create(CommN:String;BaudR:Integer;SendL:TStringList);
begin
FGo:=True;
SComm := TComm.Create(application);
SComm.BaudRate:=BaudR;
SComm.CommName:=CommN;
SComm.OnReceiveData:=CommReceiveData;
try
SComm.StartComm;
except
application.MessageBox(PChar(CommN+'被占据或不存在,无法建立与设备的通讯连接!'),'提示',MB_OK);
Exit;
end;
SendList:=TStringList.Create;
SendList:=SendL;
inherited Create(False);//False:线程一旦创立就运行 True:线程创立后先挂起
FreeOnTerminate := True;
end;
destructor TComThread.Destroy;
begin
inherited destroy;
end;
procedure TComThread.Execute;
var
DataStr:String;
begin
self.Count:=0;
while FGodo
begin
if (SendList<>Nil) and (SendList.Count>0) then
begin
DataStr:=SendList.Strings[Count];
SendData(DataStr);
if self.Count=SendList.Count-1 then
self.Count:=0
else
self.Count:=self.Count+1;
self.Suspend;
//发出一条命令后挂起,等待接收到命令后才重新唤醒
end;
end;
end;
end.
创建线程的代码:
var
ThreadArray:Array of TComThread;
ComCount:Integer
begin
ComCount:=0
SetLength(ThreadArray,10);//定义存放TComThread对象的数组长度为10
//下列的代码本身在一个循环里面,即创建多个TComThread
begin
SendL:=TStringList.Create;
SendL:=CreateSendList(Node);//CreateSendList函数的作用是生成Comm命令的TStringList
ComName:=Trim(Node.Text);//Comm的串口名字
ComBaudRate:=9600;//所有串口的波特率为9600
Com:=TComThread.Create(ComName,ComBaudRate,SendL);
ThreadArray[ComCount]:=Com;
ComCount:=ComCount+1;
end;
end;