📄 ucommsg.pas
字号:
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;
function AnsiToByte(AnsiStr: WideString): TBytes; //将一个Unico串变为字节数组
function ByteToStr(bolHend:boolean;const bytBuf :TBytes): string; //将一个字节数组为字符串
function bolReply(const bytBuf :TBytes): boolean; //是否存在回应
implementation
uses
uFrmTest,uFrmMain;
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;
wpgBytes: 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';
frmTest.Memo2.Lines.Add('1');
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;
frmTest.Memo2.Lines.Add('2');
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;
frmTest.Memo2.Lines.Add('3');
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;
frmTest.Memo2.Lines.Add('4');
ClearCommError(hComm,dwError,@cs);
ReadFile(hComm,byteData,cs.cbInQue,nBytesRead,nil);//读串口操作
ClearCommError(hComm,dwError,@cs);//清除串口错误并获取当前状态
wpgBytes :=nil;
if nBytesRead>0 then begin
if byteData[0]<>$FF then begin
setLength(wpgBytes,nBytesRead+1);//第0位存实际长度 setLength(rtn,20);长为20;从0,1...18,19
if nBytesRead>=255 then wpgBytes[0] :=255
else wpgBytes[0] := nBytesRead;
for i:=1 to high(wpgBytes) do begin
wpgBytes[i] := byteData[i-1];//rtn[0]=长 byteData[0]=值
end;
end else begin //头为 $FF
setLength(wpgBytes,nBytesRead);//第0位存实际长度 setLength(rtn,20);长为20;从0,1...18,19
if nBytesRead>=255 then wpgBytes[0] :=255
else wpgBytes[0] := nBytesRead-1;
for i:=1 to high(wpgBytes) do begin
wpgBytes[i] := byteData[i]; //rtn[0]=长 byteData[0]=FF
end;
end;
end else begin
wpgBytes :=nil;setLength(wpgBytes,1);wpgBytes[0]:=0;
end;
Result :=true;
except
wpgBytes :=nil;setLength(wpgBytes,1);wpgBytes[0]:=0;
Result :=false;
end;
frmTest.Memo2.Lines.Add('5');
CloseHandle(hComm);
tmpBytes :=wpgBytes;
end;//bytRecB完
function AnsiToByte(AnsiStr: WideString): TBytes; //将一个Unico串变为字节数组
var
len,i:integer;
wChar:Word;
rtn :TBytes;
begin
len :=length(AnsiStr);
SetLength(rtn,len*2);
for i:=1 to len do begin
wChar :=Ord( AnsiStr[i] );
rtn[ (i-1)*2 ] :=wChar div $100;
rtn[ (i-1)*2+1] :=wChar and $FF;
end;
Result :=rtn;
end;
function ByteToStr(bolHend:boolean;const bytBuf :TBytes): string; //将一个字节数组为字符串
var
len,i,iStart:integer;
myArray : array of char;
rtn :TBytes;
begin
if bolHend then begin
if length(bytBuf)>251 then len:=250
else len :=length(bytBuf)-1;
iStart :=1;
end else begin
if length(bytBuf)>250 then len:=250
else len :=length(bytBuf);
iStart :=0;
end;
SetLength(myArray,len);
for i:=(1+iStart) to len+iStart do myArray[i-1-iStart] :=Chr(bytBuf[i-1]);
Result :=String(myArray);
end;
function bolReply(const bytBuf :TBytes): boolean; //是否存在回应
var
str :string;
nPos1,nPos2 :integer;
b1,b2,rtn:boolean;
begin
rtn :=false;
if length(bytBuf)<8 then begin
rtn :=false;
end else begin
str :=ByteToStr(true, bytBuf); //将一个字节数组为字符串
nPos1 :=Pos('SEND OK',str);
nPos2 :=Pos('SUCCESS',str);
if (nPos1>0)or(nPos2>0) then rtn :=true;
end;
Result :=rtn;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -