Delphi serial communication listener

Source: Internet
Author: User

DelphiSerial Communication listener

 

2001-06-25 · Aizb · Tianji Forum


After studying the serial port program for a long time, I wrote the following code. The background generates a thread to listen to the serial port, which does not affect front-end work. It works well and has been used for receiving data from GPS devices.

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.