📄 commonunit.pas
字号:
unit CommonUnit;
interface
uses
OleCtrls, MSCommLib_TLB;
//和短消息发送相关
function SendShortMsg(AComm:TMSComm;APhone,AMsg:string):smallint;
function SetCenter(AComm:TMSComm;ACenter:string):smallint;
function SetTextMode(AComm:TMSComm):smallint;
//和硬件控制相关
function HardControl(AComm:TMSComm;AKind:smallint):integer;
function IsAbnormal(AComm:TMSComm):smallint;
implementation
uses
windows;
function InitialCom(AComm:TMSComm):smallint;
var
mStr:ShortString;
begin
Result := 1;
try
//如果串口关闭,则打开
if AComm.PortOpen = False then AComm.PortOpen := True;
AComm.Settings := '9600,n,8,1';
//清空缓冲区
AComm.InputLen := 0;
mStr := AComm.Input;
except
Result := -1;
end;
end;
//============================和短消息发送相关=======
function SendShortMsg(AComm:TMSComm;APhone,AMsg:string):smallint;
var
mStr: ShortString;
i: byte;
mSuccess: boolean;
mLen: integer;
begin
//初始化串口
if InitialCom(AComm) < 0 then
begin
Result := -1;
Exit;
end;
mSuccess := False;
//最多通信两次,如果两次都失败返回-1
for i:=1 to 2 do
begin
//====================第一部分发送号码,等GSM应答========================
mStr := #7#7; //串口通信报头
mStr := mStr + #255; //表示由单片机将报文数据转发GSM模块
mStr := mStr + 'AT'; //AT协议报头
mStr := mStr + '+CMGS=' + '<' + APhone + '>'#13#13#10; //
mStr := mStr + #5#5;//串口通信报尾部
AComm.OutPut := mStr;
//等待下位机响应
Sleep(50);
if AComm.InBufferCount <= 6 then Continue;
mStr := AComm.Input;
mLen := Length(mStr);
//报文头尾错误
if (mStr[1] <> #7) or (mStr[2] <> #7) then Continue;
if (mStr[mLen] <> #5) or (mStr[mLen-1] <> #5) then Continue;
////表示GSM模块通过单片机将报文数据转发上位机
if mStr[3] <> #255 then Continue;
//表示手机号码数据没有发送成功
if mStr[mLen-2] <> '>' then Continue;
//======================第二次发送短消息============================
mStr := #7#7; //串口通信报头
mStr := mStr + #255; //表示由单片机将报文数据转发GSM模块
mStr := mStr + AMsg; //短消息
mStr := mStr + #5#5; //串口通信报尾部
AComm.OutPut := mStr;
mSuccess := True;
Break;
end;
if not mSuccess then Result := -1
else Result := 1;
end;
function SetCenter(AComm:TMSComm;ACenter:string):smallint;
var
mStr: ShortString;
i: byte;
mSuccess: boolean;
mLen: integer;
begin
//初始化串口
if InitialCom(AComm) < 0 then
begin
Result := -1;
Exit;
end;
mSuccess := False;
//最多通信两次,如果两次都失败返回-1
for i:=1 to 2 do
begin
mStr := #7#7; //串口通信报头
mStr := mStr + #255; //表示由单片机将报文数据转发GSM模块
mStr := mStr + 'AT'; //AT协议报头
mStr := mStr + '+CMGS=' + '<' + ACenter + '>'#13#10; //
mStr := mStr + #5#5;//串口通信报尾部
AComm.OutPut := mStr;
//等待下位机响应
Sleep(50);
if AComm.InBufferCount <= 6 then Continue;
mStr := AComm.Input;
mLen := Length(mStr);
//报文头尾错误
if (mStr[1] <> #7) or (mStr[2] <> #7) then Continue;
if (mStr[mLen] <> #5) or (mStr[mLen-1] <> #5) then Continue;
////表示GSM模块通过单片机将报文数据转发上位机
if mStr[3] <> #255 then Continue;
//表示手机号码数据没有发送成功
if mStr[mLen-2] <> '>' then Continue;
mSuccess := True;
Break;
end;
if not mSuccess then Result := -1
else Result := 1;
end;
function SetTextMode(AComm:TMSComm):smallint;
var
mStr: ShortString;
i: byte;
mSuccess: boolean;
mLen: integer;
begin
//初始化串口
if InitialCom(AComm) < 0 then
begin
Result := -1;
Exit;
end;
mSuccess := False;
//最多通信两次,如果两次都失败返回-1
for i:=1 to 2 do
begin
mStr := #7#7; //串口通信报头
mStr := mStr + #255; //表示由单片机将报文数据转发GSM模块
mStr := mStr + 'AT'; //AT协议报头
//设置短消息为文本模式
mStr := mStr + '+CMGS=1'#13#10;
mStr := mStr + #5#5;//串口通信报尾部
AComm.OutPut := mStr;
//等待下位机响应
Sleep(50);
if AComm.InBufferCount <= 6 then Continue;
mStr := AComm.Input;
mLen := Length(mStr);
//报文头尾错误
if (mStr[1] <> #7) or (mStr[2] <> #7) then Continue;
if (mStr[mLen] <> #5) or (mStr[mLen-1] <> #5) then Continue;
////表示GSM模块通过单片机将报文数据转发上位机
if mStr[3] <> #255 then Continue;
//表示手机号码数据没有发送成功
if mStr[mLen-2] <> '>' then Continue;
mSuccess := True;
Break;
end;
if not mSuccess then Result := -1
else Result := 1;
end;
//====================和硬件控制相关======================
function HardControl(AComm:TMSComm;AKind:smallint):integer;
var
mStr: ShortString;
i: byte;
mSuccess: boolean;
mLen: integer;
mCmdKind : Char;
begin
//初始化串口
if InitialCom(AComm) < 0 then
begin
Result := -1;
Exit;
end;
//将命令转换为Ascii字符
case AKind of
1: mCmdKind := '1';
2: mCmdKind := '2';
3: mCmdKind := '3';
else begin
Result := -2; //该命令不被支持
Exit;
end;
end;
mSuccess := False;
//最多通信三次,如果三次都失败返回-1
for i:=1 to 3 do
begin
mStr := #7#7; //串口通信报头
mStr := mStr + #00; //表示由单片机直接接收
mStr := mStr + mCmdKind; //
mStr := mStr + #5#5;//串口通信报尾部
AComm.OutPut := mStr;
//等待下位机响应
Sleep(50);
if AComm.InBufferCount <= 6 then Continue;
mStr := AComm.Input;
mLen := Length(mStr);
//报文头尾错误
if (mStr[1] <> #7) or (mStr[2] <> #7) then Continue;
if (mStr[mLen] <> #5) or (mStr[mLen-1] <> #5) then Continue;
////表示GSM模块通过单片机将报文数据转发上位机
if mStr[3] <> #0 then Continue;
//表示手机号码数据没有发送成功
if mStr[mLen-2] <> mCmdKind then Continue;
mSuccess := True;
Break;
end;
if not mSuccess then Result := -1
else Result := 1;
end;
function IsAbnormal(AComm:TMSComm):smallint;
var
mStr: ShortString;
i: byte;
mSuccess: boolean;
mLen: integer;
begin
//初始化串口
if InitialCom(AComm) < 0 then
begin
Result := -1;
Exit;
end;
mSuccess := False;
//最多通信三次,如果三次都失败返回-1
for i:=1 to 3 do
begin
mStr := #7#7; //串口通信报头
mStr := mStr + #00; //表示由单片机直接接收
mStr := mStr + #100; //命令类型为100,查询下位机是否有异常情况
mStr := mStr + #5#5;//串口通信报尾部
AComm.OutPut := mStr;
//等待下位机响应
Sleep(50);
if AComm.InBufferCount <= 6 then Continue;
mStr := AComm.Input;
mLen := Length(mStr);
//报文头尾错误
if (mStr[1] <> #7) or (mStr[2] <> #7) then Continue;
if (mStr[mLen] <> #5) or (mStr[mLen-1] <> #5) then Continue;
////表示GSM模块通过单片机将报文数据转发上位机
if mStr[3] <> #0 then Continue;
////下位机应答没有异常情况
if mStr[mLen-2] <> #0 then
begin
Result := 0;
mSuccess := True;
Break;
end
//下位机应答有异常情况
else if mStr[mLen-2] <> #100 then
begin
Result := 1;
mSuccess := True;
Break;
end
else Continue;
end;
if not mSuccess then Result := -1;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -