|
Unit frmcomm;
Interface
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)
Combox1: tcombobox;
Button1: tbutton;
Statusbar1: tstatusbar;
Button2: tbutton;
Combox2: tcombobox;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Procedure formclose (Sender: tobject; var action: tcloseaction );
Private
{Private Declarations}
Public
{Public declarations}
End;
Tcommthread = Class (tthread)
Protected
Procedure execute; override;
Public
Constructor create;
End;
VaR
Commform: tcommform;
Commhandle: thandle;
Connected: Boolean;
Commthread: tcommthread;
Implementation
{$ R *. DFM}
Uses
Frmmain, frmmdimapview;
Procedure tcommthread. Execute;
VaR
Dwerrorflags, dwlength: DWORD;
COMSTAT: pcomstat;
Freadstat: Boolean;
Inchar: Char;
Abin: string;
XX, YY: Double; file ://Longitude and latitude
Vid: string; file ://Vehicle ID
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
...
{Received,}
End; // If (freadstat> 0 ){
End; file: // If (dwlength> 0 ){
Freemem (COMSTAT );
End; {While}
End;
Constructor tcommthread. Create;
Begin
Freeonterminate: = true;
Inherited create (false); file: // createsuincluded = false
End;
//
Procedure tcommform. button1click (Sender: tobject );
VaR
Commtimeout: tcommtimeouts;
DCB: tdcb;
Fretval: Boolean;
Begin
Statusbar1.simpletext: ='Connecting...';
Commhandle: = createfile (pchar (combobox1.text), generic_read, 0, nil, open_existing, file_attribute_normal
, 0 );
If commhandle = invalid_handle_value then begin
Statusbar1.simpletext: ='Connection Failed';
Exit;
End;
Statusbar1.simpletext: ='Same port already exists'+ Combobox1.text +'Connection!';
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.simpletext: ='Thread creation failed';
Exit;
End;
End
Else begin
Connected: = false;
Closehandle (commhandle );
End;
End;
Procedure tcommform. button2click (Sender: tobject );
Begin
Connected: = false;
Closehandle (commhandle );
{Terminate thread}
Commthread. Terminate;
Statusbar1.simpletext: ='Close the port'+ Combobox1.text;
End;
Procedure tcommform. formclose (Sender: tobject; var action: tcloseaction );
Begin
Connected: = false;
Closehandle (commhandle );
Statusbar1.simpletext: ='Close the port'+ Combobox1.text;
End;
End. |