📄 ucommsg.pas.~11~
字号:
unit uComMsg;
interface
uses
Windows,SysUtils;
function SendMsg(CommPort,tmpBaud:Word;const bytBuf:array of byte) :boolean;
function bytRecB(CommPort,tmpBaud:Word; var tmpBytes:TBytes) :boolean;
implementation
function SendMsg(CommPort,tmpBaud:Word;const bytBuf:array of byte) :boolean;
var
hComm:THandle;
cc:TCOMMCONFIG; //Windows引用
tmp:string;
iBaud :integer;
lrc:LongWord;
i:integer;
begin
Result := false;
//设置端口
if(CommPort=2) then tmp := 'com2'
else if(CommPort=3) then tmp := 'com3'
else if(CommPort=4) then tmp := 'com4'
else tmp := 'com1';
if tmpBaud=1200 then iBaud :=CBR_1200
else if tmpBaud=2400 then iBaud :=CBR_2400
else if tmpBaud=4800 then iBaud :=CBR_4800
else iBaud :=CBR_9600;
hComm := CreateFile(PChar(tmp),Generic_Read or Generic_Write, 0, nil, Open_Existing, 0, 0);//打开串行口
if(hComm=Invalid_Handle_Value)then begin
Result := false;
exit;
end;
GetCommState(hComm,cc.dcb); //获取默认通信参数
cc.dcb.BaudRate:=iBaud;
cc.dcb.ByteSize:=8;
cc.dcb.Parity:= 0; //NOPARITY = 0; MARKPARITY = 3; SPACEPARITY = 4;
cc.dcb.StopBits:=ONESTOPBIT; //ONESTOPBIT = 0;
if not SetCommState(hComm,cc.dcb) then begin //设置通信参数
CloseHandle(hComm); //关闭串行口
Result := false;
exit;
end;
WriteFile(hComm,bytBuf,length(bytBuf),lrc,nil);//写串口操作
CloseHandle(hComm);
Result := true;
end;
function bytRecB(CommPort,tmpBaud:Word; var tmpBytes:TBytes) :boolean;
var
hComm:THandle;
cc:TCOMMCONFIG; //Windows引用
iBaud :integer;
tmp:string;
nBytesRead,dwError:LongWord;
byteData:array[0..25000] of byte; //8000卡, 各3字节
cs:TCOMSTAT;
i:integer;
rtn: TBytes; //(因为后期问题,实际取数据时从1开始,第0位存长度)
begin
try
Result := false;
//设置端口
if(CommPort=2) then tmp := 'com2'
else if(CommPort=3) then tmp := 'com3'
else if(CommPort=4) then tmp := 'com4'
else tmp := 'com1';
if tmpBaud=1200 then iBaud :=CBR_1200
else if tmpBaud=2400 then iBaud :=CBR_2400
else if tmpBaud=4800 then iBaud :=CBR_4800
else iBaud :=CBR_9600;
hComm := CreateFile(PChar(tmp),Generic_Read or Generic_Write, 0, nil, Open_Existing, 0, 0);//打开串行口
if(hComm=Invalid_Handle_Value)then begin
Result := false;
exit;
end;
GetCommState(hComm,cc.dcb); //获取默认通信参数
cc.dcb.BaudRate:=iBaud;
cc.dcb.ByteSize:=8;
cc.dcb.Parity:= 0; //NOPARITY = 0; MARKPARITY = 3; SPACEPARITY = 4;
cc.dcb.StopBits:=ONESTOPBIT; //ONESTOPBIT = 0;
if not SetCommState(hComm,cc.dcb) then begin //设置通信参数
CloseHandle(hComm); //关闭串行口
Result := false;
exit;
end;
ClearCommError(hComm,dwError,@cs);
ReadFile(hComm,byteData,cs.cbInQue,nBytesRead,nil);//读串口操作
ClearCommError(hComm,dwError,@cs);//清除串口错误并获取当前状态
rtn :=nil;
if nBytesRead>0 then begin
if byteData[0]<>$FF then begin
setLength(rtn,nBytesRead+1);//第0位存实际长度 setLength(rtn,20);长为20;从0,1...18,19
if nBytesRead>=255 then rtn[0] :=255
else rtn[0] := nBytesRead;
for i:=1 to high(rtn) do begin
rtn[i] := byteData[i-1];//rtn[0]=长 byteData[0]=值
end;
end else begin //头为 $FF
setLength(rtn,nBytesRead);//第0位存实际长度 setLength(rtn,20);长为20;从0,1...18,19
if nBytesRead>=255 then rtn[0] :=255
else rtn[0] := nBytesRead-1;
for i:=1 to high(rtn) do begin
rtn[i] := byteData[i]; //rtn[0]=长 byteData[0]=FF
end;
end;
end else begin
rtn :=nil;setLength(rtn,1);rtn[0]:=0;
end;
except
rtn :=nil;setLength(rtn,1);rtn[0]:=0;
Result :=false;
end;
tmpBytes :=rtn;
end;//bytRecB完
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -