Home > Back-end >  Suitable for industrial applications of the serial port object unit
Suitable for industrial applications of the serial port object unit

Time:10-11

http://download.csdn.net/download/supermantm/5977685
Unit: EzCommPortz pas
Simple and efficient serial port drive unit

Function description:

This unit base class defines a serial port TCommPortz, can complete the standard UART serial port of the underlying function, including to send data, receive data, the output lines (DTR, RTS) level, read input line (CTS, DSR, RING) level, and define the related events of virtual methods lead to the subsequent data processing

Because TCommPortz object is the foundation of serial port function, so from TObject inheritance in order to achieve the purpose of efficient resource conservation

Method of use:

All instances of TCommPortz (including its subclass instance) share a global message window (usually to the main application window) and a custom window message, used as a thread together, specific implementation is as follows:

Every TCommPortz instance contains two threads may monitor the serial port to read and write operations (via EnableRx, UseTxQue properties to decide whether to create using multithreading), thread captured in the incident, the specific messages sent to the specified window (added to the main thread's message queue) again by the main thread calls the global TCommPortz. The HandleMessage method for distribution, therefore, the application usually contains the following sections:

Const
CM_COMMS=WM_USER +?? ;//define serial notification message
Type
TMainForm=class (TForm)
Private
//serial port message statement
Procedure DoCmComms (var M: TMessage); The message CM_COMMS;
end;

Implementation

//define their own serial port
Type
TMyComm=class (TCommPortz)
Protected
//processing data reception, such as do not need to receive don't overload the method)
Procedure RxData (nSize: Integer; Var Data: TByteArray); Override.
//handle to send data (such as after to send whatever don't overload the method)
Procedure TxData (TxFrmId: Integer; PData: PTxFrmBuf; TxFailed: Boolean); Override.
//process the input line level changes, such as serial port as the data transceiver is not only overloading the method)
Procedure LineChanged; Override.
//error handling (usually don't overload the method)
Procedure ErrorHandle (ErrCode: Integer); Override.
end;

//the main window has been created need to initialize serial port driver:
Procedure TMainForm. CreateForm (Sender: TObject);
The begin
//initialization: set message window (main window) and message number, global receive buffer is set to 4 k bytes
TCommPortz. The Initialize (Handle, CM_COMMS, 4096);
MyComm:=TMyComm. Create (Ini. ReadInt (... ));//create a serial port instance and quick initialization parameters (note)
With MyComm do begin//item by item configuration parameter
PortNo: number=1;//serial number (COM1 - COM255)
Baud:=1200;//baud rate (600-115200)
EnableRx:=True;//allow receiving and state capture (only do send serial port does not need to)
UseTxQue:=True;//allow using multi-threading queue send
//(False use direct push serial port, oneself consider sending clearance)
TxBuffSize:=8192;//send queue buffer size (UseTxQue=False, ignoring the parameter)
end;
MyComm. Open;//open the serial port
If MyComm. Connected then Caption:='OK';//open the success!
end;

//message transfer:
Procedure TMainForm. DoCmComms (var M: TMessage);
The begin
TCommPortz. HandleMessage (m. Param, M.L Param);
end;

//then respectively data reception, data reporting, mouth line level change, error notification events such as
.

Because the system can operate more than one serial port at the same time, TCommPortz provide Ports (Index: Integer) functions provide access to each instance, instances can be MyComm. PortIndex properties to achieve their own port number

TCommPortz. Create the creation process with an integer parameter, through the parameters rapidly initialize the serial port, serial number, baud rate, allowing the state monitoring, allow multiple threads to send and send queue buffer size), the parameter can be MyComm. GetIntSettings function is obtained, which by this feature is simply to write the configuration of serial port to the INI file or system registry, this parameter is a 24 bit integer (0-16777215), the application can also separate the parameters by using the method of each attribute configuration

Property description:
~ ~ ~ ~ ~ ~ ~ ~
PortIndex: Byte
Serial port internal sequence number, generated automatically when creating, through TCommPortz. Ports (PortIndex) to access the serial port
This property is read-only

PortNo: number Byte
Serial number, COM1 - COM255
On the serial port can't write this attribute (void)

Baud: Integer
Baud rate, can be set to 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 33600, 38400, 57600, one of 115200, illegal baud rate Settings won't write
On the serial port can't write this attribute (void)

TxQueSize: Integer
Send queue buffer size, this parameter in UseTxQue=True effective, this parameter domain for 256-8192, with 256 - byte alignment, that is, set TxQueSize:=1 equivalent TxQueSize:=256
On the serial port can't write this attribute (void)

EnableRx: Boolean
Allow state capture threads, EnableRx=False leads to serial port not create threads, so also can't receive data, cannot obtain input line level
Just send as a function of serial port can be installed EnableRx:
=False to conserve resourcesOn the serial port can't write this attribute (void)

UseTxQue: Boolean
Send thread allows you to create a queue, UseTxQue=False, TxQueSize attribute is invalid when sending data does not guarantee that will happen overlapped operations (data in data message is too long, the buffer is not empty when loading to send the new data lead to unreliable)
If the application clearly not foreseeable overlapped operations (such as sent by a timer to trigger and timer interval to ensure enough data), can be set up UseTxQue:
=False to conserve resourcesOn the serial port can't write this attribute (void)

Connected: Boolean
On the serial port has been successfully Connected to True, after close to False

TxEmpty: Boolean
Send free indication, serial port is not open this property is always False, Connected, UseTxQue=False, the program will (should) send method by this property to ensure that the call in your spare time to ensure not occur overlapped operations

CTSLevel DSRLevel, RingLevel DTRLevel, RTSLevel
Port line level, True represent high level (RS232 level for + 12 v), or False on behalf of the low level (RS232 level - 12 v), the CTS/DSR/RING line as read-only, DTR/RTS line for read/write attributes, can be output

Levels [9] 1.. : Boolean
To the DB9 RS232 standard outlet line number in the achievement of port line level, including
Levels [1].. Levels [3] : shell/RX/TX: read constant is False, writes invalid
Levels [4] for the DTR line: read/write
Levels [5] for GND line: read constant is False, writes invalid
Levels [6] for the DSR line: read-only
Levels [7] for the RTS line: read/write
Levels [8] for the CTS line: read-only
Levels [9] for the RING line: read-only

Methods:
~ ~ ~ ~ ~ ~ ~ ~
The function GetTxBuff (nSize: Integer) : PTxFrmBuf
To allocate a buffer for sending, when UseTxQue=False invalid (returns nil), the method UseTxQue=True returns a pointer to the send buffer structure, TTxFrmBuf structure are defined as follows:
TTxFrmBuf=the packed record
Size: Integer;
Data: an array of Byte [0.. 255].
end;
PTxFrmBuf=^ TTxFrmBuf;
NSize parameter is not greater than half TxQueSize
Allocated buffer does not need to also can not release (FreeMem)! , the segment of memory exists in a circular buffer, the system automatically to allocate

The function SendData publishes the event (pData: PTxFrmBuf) : an Integer
To send data, pData as the pointer (Size) have been set, pData is not must be through GetTxBuff obtained, the caller can manage memory by oneself, but must pay attention to, when UseTxQue=True, sent may not be loaded immediately, so the caller must ensure that the data for effective
When UseTxQue=False, serial port object does not create the send queue thread, data is loaded to send immediately, so that data can be a process of internal variables (in stack allocation)

Procedure Open;
Open the serial port

Procedure Close;
Close the serial port

The function GetIntSettings: Integer;
Achieve serial port configuration parameters, the parameters can be directly when creating examples of serial port configuration

Protected parts:
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
The function Openned: Boolean
Indicating whether serial port documents already open
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related