
22.10.2007, 20:07
|
Прохожий
|
|
Регистрация: 17.09.2007
Сообщения: 10
Репутация: 10
|
|
Сначала открываем порт:
Код:
procedure TForm1.Button4Click(Sender: TObject);
begin
Button5.Enabled:=True;
Button4.Enabled:=False;
Port := CreateFile(PChar(ComboBox1.Items[ComboBox1.ItemIndex]),GENERIC_READ+GENERIC_WRITE,0,nil,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,LongInt(0));
SetupComm(Port,1024,1024);
PurgeComm(Port,PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR);
GetCommState(Port,PortDCB);
PortDCB.BaudRate := StrToInt(BaudTrans.Items[BaudTrans.ItemIndex]);
PortDCB.StopBits := 1;
PortDCB.Flags:=4113;
if ControlCTS.Checked then PortDCB.Flags:=PortDCB.Flags xor 4;
SetCommState(Port,PortDCB);
if AutoRTS.Checked then EscapeCommFunction(Port,CLRRTS);
TimerReceive.Enabled:=True;
end;
Читаем из него:
Код:
procedure TForm1.TimerReceiveTimer(Sender: TObject);
var CountCharWait: word;
TimeOut: byte;
NumberWritten: cardinal;
PhoneCommand: string;
procedure ReadPort;
var inBuf: array [0..1024] of char;
cbCharsRead,dwErrorCode: dword;
i: integer;
procedure HexDecReceive;
var
InBuf: array [0..1024] of byte;
cbByteRead:dword;
i: integer;
begin
if ReadFile(Port,InBuf,CountCharWait,cbByteRead,nil)
then
begin
for i:=0 to cbByteRead-1 do
if RadioButton1.Checked
then CharsReceived.Caption:=CharsReceived.Caption+intToHex(inBuf[i],2)+' '
else CharsReceived.Caption:=CharsReceived.Caption+intToStr(inBuf[i])+' ';
end;
end;
begin
inc(TimeOut);
ClearCommError(Port,dwErrorCode,@PortStat);
CountCharWait := PortStat.cbInQue;
if CountCharWait > 0 then
begin
TimeOut:=0;
if (RadioButton1.Checked) or (RadioButton2.Checked) then HexDecReceive else
if ReadFile(Port,InBuf,CountCharWait,cbCharsRead,nil)
then for i:=0 to cbCharsRead-1 do
CharsReceived.Caption:=CharsReceived.Caption+inBuf[i]
else dwErrorCode := GetLastError;
end;
end;
begin
ReadPort;
if (TimeOut<>0)and (CharsReceived.Caption<>'') then
begin
if trim(CharsReceived.Caption) = 'RING' then begin //Ждём вызова
NumberWritten := 0;
PhoneCommand:='ATA'+#13#10;
WriteFile(Port, PChar(PhoneCommand)^, Length(PhoneCommand), NumberWritten, nil);
end;
if trim(CharsReceived.Caption) = 'send' then begin //Ждём команду приёма файла
Timer2.Enabled:=True;
TimerReceive.Enabled:=False;
exit;
end;
if triim(Char)
MemoRx.Lines.Add(CharsReceived.Caption);
if CheckBox1.Checked then
begin
Edit1.Text:=CharsReceived.Caption;
if Radiobutton3.Checked
then TransmitClick(Sender)
else TxHexButtonClick(Sender);
Edit1.Text:='';
end;
CharsReceived.Caption:='';
end;
// LedTrans.Brush.Color:=clNone;
if TimeOut>10 then TimeOut:=0;
end;
Пишем в него:
Код:
procedure TForm1.TransmitClick(Sender: TObject);
var
PhoneCommand: string;
NumberWritten: cardinal;
begin
PhoneCommand := Edit1.Text + #13 + #10;
NumberWritten := 0;
WriteFile(Port, PChar(PhoneCommand)^, Length(PhoneCommand), NumberWritten, nil);
end;
|