Delphi串列埠通訊的監聽

2021-09-08 05:52:09 字數 4251 閱讀 5053

delphi串列埠通訊的監聽

<?xml:namespace prefix = st1 />2001-06-25··

aizb

··天極論壇

串列埠程式我後來研究了好久,寫了下面的**,後台生成乙個執行緒監聽串列埠,不影響前台工作。效果很好,一直用於

gps儀器的資料接收。

unit frmcomm;

inte***ce

uses

windows, messages, sysutils, classes, graphics, controls, forms, dialogs,

stdctrls, comctrls,geoutils,geogps;

const maxblock = 160;

type

tcomm = record

idcomdev : thandle;

fconnected : boolean;

end;

tcommform = class(tform)

combobox1: tcombobox;

button1: tbutton;

statusbar1: tstatusbar;

button2: tbutton;

combobox2: tcombobox;

procedure button1click(sender: tobject);

procedure button2click(sender: tobject);

procedure formclose(sender: tobject; var action: tcloseaction);

private

public

end;

tcommthread = class(tthread)

protected

procedure execute;override;

public

constructor create;

end;

var

commform: tcommform;

commhandle : thandle;

connected : boolean;

commthread : tcommthread;

implementation

uses

frmmain,frmmdimapview;

procedure tcommthread.execute;

var

dwerrorflags,dwlength : dword;

comstat : pcomstat;

freadstat : boolean;

inchar : char;

abin : string;

xx,yy : double; file://經度、緯度

vid : string; file://車號

begin

while connected do begin

getmem(comstat,sizeof(tcomstat));

clearcommerror(commhandle, dwerrorflags, comstat);

if (dwerrorflags > 0) then begin

purgecomm(commhandle,(purge_rxabort and purge_rxclear));

// return 0;

end;

dwlength := comstat.cbinque;

if (dwlength>0) then begin

freadstat := readfile(commhandle, inchar, 1,dwlength, nil);

if (freadstat) then begin

if (inchar <> chr(13)) and (length(abin) < maxblock+5 ) then abin := abin + inchar

else begin

...

end;//if (freadstat>0)

end;

constructor tcommthread.create;

begin

freeonterminate := true;

inherited create(false); file://createsuspended = false

end;

// procedure tcommform.button1click(sender: tobject);

var

commtimeout : tcommtimeouts;

dcb : tdcb;

fretval : boolean;

begin

statusbar1.******text := '連線中...';

commhandle := createfile(pchar(combobox1.text),generic_read,0,nil,open_existing,file_attribute_normal

, 0);

if commhandle = invalid_handle_value then begin

statusbar1.******text := '連線失敗';

exit;

end;

statusbar1.******text := '已同埠'+ combobox1.text + '連線!';

commtimeout.readintervaltimeout := maxdword;

commtimeout.readtotaltimeoutmultiplier := 0;

commtimeout.readtotaltimeoutconstant := 0;

setcommtimeouts(commhandle, commtimeout);

getcommstate(commhandle,dcb);

dcb.baudrate := 9600;

dcb.bytesize := 8;

dcb.parity := noparity;

dcb.stopbits := onestopbit;

fretval := setcommstate(commhandle, dcb);

if (fretval) then begin

connected := true;

try

commthread := tcommthread.create;

except

connected := false;

closehandle(commhandle);

fretval := false;

statusbar1.******text := '執行緒建立失敗';

exit;

end;

end

else begin

connected := false;

closehandle(commhandle);

end;

end;

procedure tcommform.button2click(sender: tobject);

begin

connected := false;

closehandle(commhandle);

commthread.terminate;

statusbar1.******text := '關閉埠'+combobox1.text;

end;

procedure tcommform.formclose(sender: tobject; var action: tcloseaction);

begin

connected := false;

closehandle(commhandle);

statusbar1.******text := '關閉埠'+combobox1.text;

end;

end.

Delphi 基於狀態機的串列埠通訊

序列通訊介面 如rs232 rs485等 作為計算機與微控制器互動資料的主要介面,廣泛用於各類儀器儀表 工業監測及自動控制領域中。通訊協議是需要通訊的雙方所達成的一種約定,它對包括資料格式 同步方式 傳送速度 傳送步驟 檢糾錯方式以及控制字元定義等問題作出統一規定,在雙方的通訊中必須共同遵守。在實際...

Delphi 基於狀態機的串列埠通訊

序列通訊介面 如rs232 rs485等 作為計算機與微控制器互動資料的主要介面,廣泛用於各類儀器儀表 工業監測及自動控制領域中。通訊協議是需要通訊的雙方所達成的一種約定,它對包括資料格式 同步方式 傳送速度 傳送步驟 檢糾錯方式以及控制字元定義等問題作出統一規定,在雙方的通訊中必須共同遵守。在實際...

在WIN32中的串列埠通訊(Delphi)

在win32中的串列埠通訊 delphi 由在win32作業系統中禁止應用程式象dos中那樣直接訪問計算機硬體,因此,無法象以前那樣採用中斷讀寫串列埠。但是在win32中我們可發採用兩種方法訪問串列埠 1 使用vb中的mscomm串列埠控制項 2 採用api函式,本文主要介紹採用api函式實現串列埠...