ucommsg.pas.~8~
来自「用串口发送手机短信 需要GSM Mode设备」· ~8~ 代码 · 共 58 行
~8~
58 行
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;
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;
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?