L
liang1chao
Unregistered / Unconfirmed
GUEST, unregistred user!
API的串口通讯,用以显示手机的来电显示。重新启动电脑打开程序正常,但时间长了(4-5个小时或者一天)就不能接收到串口发过来的号码了,重新启动或者不重启用超级终端与手机连接发送AT+CLIP=1后在运行程序又恢复正常。是不是缓存的原因?为什么用超级终端连接后又能用呢?望各位指点迷津。主要的代码如下
procedure TForm1.Button1Click(Sender: TObject);
var
lpdcb: TDCB;
Ini: TIniFile;
com: string;
begin
if Opened then
Exit;
Ini := TIniFile.Create(apppath + 'config.ini');
// com:=Ini.ReadString('HW','Port','');
// IF com='' Then
Com := ComboBox1.Text;
ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(COM);
hCom := CreateFile(Pchar(ComboBox1.Text), generic_read or
generic_write, 0, nil, open_existing,
file_attribute_normal or
file_flag_overlapped, 0); // 打 开 串行 口
if hCom = INVALID_HANDLE_VALUE then
begin
Ini.Free;
Exit;
end;
Opened := True;
SetupComm(hCom,4096,4096); //设 置 输 入, 输 出 缓 冲区 皆 为4096 字 节
shezhi1:=setupcomm(hcom,4096,4096);
GetCommState(hcom, lpdcb); // 获 取 串 行 口 当 前 默 认设 置
lpdcb.baudrate :=19200;
lpdcb.StopBits := 0;
lpdcb.ByteSize := 8;
lpdcb.Parity:=NoParity;
Setcommstate(hcom, lpdcb);
SetcommMask(hcom, EV_RXCHAR); // 指 定 串 行 口 事 件 为 接 收 到 字 符
Ini.WriteString('HW', 'PORT', ComboBox1.Text);
Ini.Free;
//SendCMD('AT+VCID=1');
SendCMD('AT+CLIP=1');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Opened := False;
LoadCodeTableFromDisk;
idtcpclient1.Host := '192.168.0.8';
idtcpclient1.Port := 20000;
self.phonetype := 1;
LogInfo:=LoginSMS;
if LogInfo.User='' then application.Terminate ;
end;
constructor TComThread.Create(Handle: DWORD; HWND: DWORD);
begin
FhCom := Handle;
FHWND := HWND;
FExit := False;
inherited Create(False);
FreeOnTerminate := True;
end;
procedure TComThread.Execute; // 线 程 执 行 过 程
var
dwEvtMask: Dword;
Wait: Boolean;
begin
fillchar(lpol, sizeof(toverlapped), 0);
while True do
begin
dwEvtMask := 0;
Wait := WaitCommEvent(Fhcom, dwevtmask, nil); // 等 待 串 行 口事 件;
if Wait then
begin
waitforsingleobject(post_event, infinite); // 等 待 同 步 事件 置
位;
resetevent(post_event); // 同 步 事 件 复 位;
PostMessage(FHWND, WM_COMMNOTIFY, 0, 0); // 发 送 消 息;
end
else
Exit;
end;
end;
procedure TForm1.MsgComProcess(var Message: Tmessage);
var
Clear: Boolean;
Coms: Tcomstat;
cbNum, ReadNumber, lpErrors: DWORD;
Read_Buffer: array[1..100] of char;
iPos, Pos2: Integer;
Num: Pchar;
tmpstr: string;
begin
Clear := Clearcommerror(hcom, lpErrors, @Coms);
if Clear then //清 除 串 口 错 误 并 获 取 当 前 状 态
begin
cbNum := Coms.cbInQue;
FillChar(Read_Buffer, 100, 0);
Sleep(100);
ReadFile(hCom, Read_Buffer, cbNum, ReadNumber, @lpol);// 处 理 接 收
数 据/////(就是这里的Read_Buffer运行时的到返回值,如果没有OK那么来电显示就失败了!!!!)各位大哥大姐只要能让这里出现OK就是救了我啦!!!
SetEvent(Post_Event); // 同 步 事 件 置 位
procedure TForm1.SendCMD(CMD: string);
var
Buffer: array[0..127] of char;
cbNum: DWORD;
begin
Fillchar(Buffer, 128, 0);
CMD := CMD + #$0D#$0A;
StrCopy(@Buffer, Pchar(CMD));
fillchar(lpol, sizeof(toverlapped), 0);
ComThread := TComThread.Create(hCom, Handle);
WriteFile(hCom, Buffer, Length(CMD), cbNum, @lpol);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
lpdcb: TDCB;
Ini: TIniFile;
com: string;
begin
if Opened then
Exit;
Ini := TIniFile.Create(apppath + 'config.ini');
// com:=Ini.ReadString('HW','Port','');
// IF com='' Then
Com := ComboBox1.Text;
ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(COM);
hCom := CreateFile(Pchar(ComboBox1.Text), generic_read or
generic_write, 0, nil, open_existing,
file_attribute_normal or
file_flag_overlapped, 0); // 打 开 串行 口
if hCom = INVALID_HANDLE_VALUE then
begin
Ini.Free;
Exit;
end;
Opened := True;
SetupComm(hCom,4096,4096); //设 置 输 入, 输 出 缓 冲区 皆 为4096 字 节
shezhi1:=setupcomm(hcom,4096,4096);
GetCommState(hcom, lpdcb); // 获 取 串 行 口 当 前 默 认设 置
lpdcb.baudrate :=19200;
lpdcb.StopBits := 0;
lpdcb.ByteSize := 8;
lpdcb.Parity:=NoParity;
Setcommstate(hcom, lpdcb);
SetcommMask(hcom, EV_RXCHAR); // 指 定 串 行 口 事 件 为 接 收 到 字 符
Ini.WriteString('HW', 'PORT', ComboBox1.Text);
Ini.Free;
//SendCMD('AT+VCID=1');
SendCMD('AT+CLIP=1');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Opened := False;
LoadCodeTableFromDisk;
idtcpclient1.Host := '192.168.0.8';
idtcpclient1.Port := 20000;
self.phonetype := 1;
LogInfo:=LoginSMS;
if LogInfo.User='' then application.Terminate ;
end;
constructor TComThread.Create(Handle: DWORD; HWND: DWORD);
begin
FhCom := Handle;
FHWND := HWND;
FExit := False;
inherited Create(False);
FreeOnTerminate := True;
end;
procedure TComThread.Execute; // 线 程 执 行 过 程
var
dwEvtMask: Dword;
Wait: Boolean;
begin
fillchar(lpol, sizeof(toverlapped), 0);
while True do
begin
dwEvtMask := 0;
Wait := WaitCommEvent(Fhcom, dwevtmask, nil); // 等 待 串 行 口事 件;
if Wait then
begin
waitforsingleobject(post_event, infinite); // 等 待 同 步 事件 置
位;
resetevent(post_event); // 同 步 事 件 复 位;
PostMessage(FHWND, WM_COMMNOTIFY, 0, 0); // 发 送 消 息;
end
else
Exit;
end;
end;
procedure TForm1.MsgComProcess(var Message: Tmessage);
var
Clear: Boolean;
Coms: Tcomstat;
cbNum, ReadNumber, lpErrors: DWORD;
Read_Buffer: array[1..100] of char;
iPos, Pos2: Integer;
Num: Pchar;
tmpstr: string;
begin
Clear := Clearcommerror(hcom, lpErrors, @Coms);
if Clear then //清 除 串 口 错 误 并 获 取 当 前 状 态
begin
cbNum := Coms.cbInQue;
FillChar(Read_Buffer, 100, 0);
Sleep(100);
ReadFile(hCom, Read_Buffer, cbNum, ReadNumber, @lpol);// 处 理 接 收
数 据/////(就是这里的Read_Buffer运行时的到返回值,如果没有OK那么来电显示就失败了!!!!)各位大哥大姐只要能让这里出现OK就是救了我啦!!!
SetEvent(Post_Event); // 同 步 事 件 置 位
procedure TForm1.SendCMD(CMD: string);
var
Buffer: array[0..127] of char;
cbNum: DWORD;
begin
Fillchar(Buffer, 128, 0);
CMD := CMD + #$0D#$0A;
StrCopy(@Buffer, Pchar(CMD));
fillchar(lpol, sizeof(toverlapped), 0);
ComThread := TComThread.Create(hCom, Handle);
WriteFile(hCom, Buffer, Length(CMD), cbNum, @lpol);
end;