下面的代码可以控制Nokia n73手机拨打电话号码和挂断电话
procedure TForm1.btn1Click(Sender: TObject);
var
Err: Cardinal;
CommStat : _COMSTAT;
s : string;
W : Cardinal;
overlap: _OVERLAPPED;
DCB : TDCB;
Buf : array[0..1023] of char;
LEvent: TEvent;
LEOL : Boolean;
LIndex : Integer;
begin
LEvent := TEvent.Create(nil, True, False, '');
FComm := CreateFile('COM3', GENERIC_READ or GENERIC_WRITE, 0, nil,
OPEN_EXISTING, 0, 0);
if FComm = INVALID_HANDLE_VALUE then
begin
ShowMessage('Error');
Exit;
end;
SetupComm(FComm, 1024, 1024); // 设置缓冲区
BuildCommDCB('460800,N,8,1', DCB);
SetCommState(FComm, DCB); /// 设置串口参数
PurgeComm(FComm, PURGE_TXCLEAR); /// 清空发送缓冲区
ClearCommError(FComm, Err, @CommStat); /// 清除错误状态
s := 'atd3826041;'#13#10; /// 拨打电话号码AT指令
FillChar(overlap, SizeOf(overlap), 0);
overlap.hEvent := LEvent.Handle;
WriteFile(FComm, s[1], Length(s), W, @overlap);
FlushFileBuffers(FComm);
ReadFile(FComm, Buf, 1, W, @overlap);
case LEvent.WaitFor(50) of
wrSignaled:
begin
GetOverlappedResult(FComm, overlap, W, True);
LEOL := False;
LIndex := W;
while not LEOL and (LIndex < SizeOf(Buf)) do
begin
ReadFile(FComm, Buf[LIndex], 1, W, @overlap);
GetOverlappedResult(FComm, overlap, W, True);
LEOL := (Buf[LIndex] = #$0A) or (Buf[LIndex] = #$0D);
Inc(LIndex);
end;
try
mmo1.Lines.Add(Buf);
except
end;
end;
wrTimeout, wrAbandoned, wrError:
begin
Sleep(100);
end;
end;
Caption := SysErrorMessage(GetLastError);
end;
// 如果要读取当前缓冲区的所有数据,可以利用下面的代码:
s := 'ati2;'#13#10; /// 拨打电话号码AT指令
FillChar(overlap, SizeOf(overlap), 0);
overlap.hEvent := LEvent.Handle;
WriteFile(FComm, s[1], Length(s), W, @overlap);
FlushFileBuffers(FComm);
Sleep(50);
ClearCommError(FComm, Err, @CommStat); /// 获取COM状态
FillChar(Buf, SizeOf(Buf), 0);
ReadFile(FComm, Buf, CommStat.cbInQue, W, @overlap); /// CommStat.cbInQue 就是当前返回的数据总大小
procedure TForm1.btn2Click(Sender: TObject);
var
s: string;
W: Cardinal;
begin
/// 挂断通话
s := 'at+chup'#13#10;
WriteFile(FComm, s[1], Length(s), W, nil);
Caption := SysErrorMessage(GetLastError);
end;