📄 ucommsg.pas.~9~
字号:
unit uComMsg;
interface
uses
Windows,SysUtils;
function SendMsg(CommPort,tmpBaud:Word;const bytBuf:TBytes) :boolean;
implementation
function SendMsg(CommPort,tmpBaud:Word;const bytBuf:TBytes) :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);//写串口操作
for i:=0 to high(bytBuf) do WriteFile(hComm,bytBuf[i],1,lrc,nil);
CloseHandle(hComm);
Result := true;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -