自己写串口通讯程序不就可以了吗
以下是我做的一个项目中使用的一个方法,
我的项目中的串口是多个的,由于原程序太大,我拷几个供参考
//接收数据链表
type
preceivelink=^treceivelink;
treceivelink=record
data:byte;
next
receivelink;
end;
//读串口数据线程
{ TreadThread }
Type
TreadThread = class(TThread)
private
Fport: integer;
Fcomhandle: Integer;
Fdata:integer;
procedure add_onereceivedata;
protected
procedure Execute; override;
procedure data_handle(port:integer;receivelink
receivelink;receivecount:integer);
procedure read_commdata;
public
constructor Create(port:integer;comhandle:thandle);
destructor Destroy;override;
end;
var
overlapped
overlapped;
receivelink
receivelink;
receivecount:integer;
//通讯口初始化
function init_comport(port:string;var comhandle:thandle;sbaud,sstop,sbyte,sparity:integer):boolean;
var
lpdcb:Tdcb;
begin
fillchar(overlapped,sizeof(toverlapped),0);
Comhandle:=CreateFile(PChar(Port),generic_read or generic_write,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if comhandle = INVALID_HANDLE_VALUE then
result:=false
else
begin
if not setupcomm(comhandle,512,512) then //字节数可以根据需要修改
begin
result:=false;
exit;
end;
getcommstate(comhandle,lpdcb);
lpdcb.baudrate:=sbaud;
lpdcb.StopBits:=sstop;
lpdcb.ByteSize:=sbyte;
lpdcb.Parity=sparity;
if not Setcommstate(comhandle,lpdcb) then
begin
result:=false;
exit;
end;
if not setcommMask(comhandle,ev_rxchar) then
begin
result:=false;
exit;
end;
result:=true;
end;
end;
//读串口线程
constructor TreadThread.Create(port:integer;comhandle:thandle);
begin
Fport:=port;
Fcomhandle:=comhandle;
FreeOnTerminate := True;
inherited Create(true);
end;
destructor TreadThread.destroy;
begin
inherited destroy;
end;
//执行读串口线程
procedure TreadThread.Execute;
begin
read_commdata;
end;
//增加一个接收数据
procedure treadthread.add_onereceivedata;
var
headreceivelink,tempreceivepoint
receivelink;
begin
new(tempreceivepoint);
tempreceivepoint^.data:=Fdata;
tempreceivepoint^.next:=nil;
if tempportlink^.receivelink<>nil then
begin
headreceivelink:=tempportlink^.receivelink;
while headreceivelink^.next<>nil do
headreceivelink:=headreceivelink^.next;
headreceivelink^.next:=tempreceivepoint;
tempportlink^.receivecount:=tempportlink^.receivecount+1;
end
else
begin //第一个数据必须为“$A5”才是正确数据,可以改为需要的值
if Fdata<>$A5 then
begin
dispose(tempreceivepoint);
tempportlink^.receivecount:=1;
end
else
begin
tempportlink^.receivelink:=tempreceivepoint;
tempportlink^.receivecount:=1;
end;
end;
if receivecount=11 then //接收的为定长数据11个字符
begin
data_handle //数据处理,自己写
tempportlink^.receivecount:=0;
end;
end;
//接收通讯口数据
procedure treadthread.read_commdata;
var
eventmask:dword;
Clear:Boolean;
Coms:Tcomstat;
cbNum,ReadNumber:cardinal;
lpErrors:cardinal;
Read_Buffer:array[1..512] of byte;
i:integer;
begin
eventmask:=0;
while true do
if (waitcommevent(Fcomhandle,eventmask,overlapped)) then
if (eventmask=ev_rxchar) then
begin
Clear:=Clearcommerror(Fcomhandle,lpErrors,@Coms);
if Clear Then
Begin
cbNum:=Coms.cbInQue;
if ReadFile(Fcomhandle,Read_Buffer,cbNum,ReadNumber,overlapped) then
for i:=1 to cbnum do
begin
Fdata:=read_buffer
;
Synchronize(add_onereceivedata);
end;
end;
end;
end;