Delphi MSComm Real-time serial communicationMSComm controls are rich in properties that are closely related to serial communication, providing a variety of operations on the serial port, which makes serial communication very easy. The MSComm has many control properties, and the commonly used properties are as follows:
1). Commport: Sets or returns the serial port number, which defaults to 1.
2). Setting: Set or return serial communication parameters in the format "baud rate, parity bit, data bit, stop bit". Example: mscomm1.setting:=9600,n,8,1
3). PortOpen: Turn the serial port on or off in the format: mscomm1.portopen:={true| False}
4). Inbuffersize: Sets or returns the size of the receive buffer, with a default value of 1024 bytes.
5). InBufferCount: Returns the number of bytes in the receive buffer that are waiting to be read and empties the receive buffer by setting the property to zero.
6). RThreshold: This property is a threshold that determines if the number of bytes in the receive buffer reaches or exceeds the value, resulting in a OnComm event with code comevreceive.
7). Sthreshold: This property is a threshold that determines the OnComm event in which code is comevsend when the number of bytes in the send buffer is less than this value.
8). Inputlen: Sets or returns the number of bytes in the receive buffer that are read with input, and setting this property to 0 indicates that input reads the contents of the entire buffer.
9). Input: Reads a string of characters from the receive buffer.
10). Outbuffersize: Sets or returns the size of the send buffer, with a default value of 512 bytes.
11). OutBufferCount: Returns the number of bytes waiting to be sent in the send buffer, which can be emptied by setting the property to zero.
12). OutPut: A string of characters is passed to the send buffer.
Errors or events that occur during communication raise the OnComm event and reflect the error type by the CommEvent property value, and the program performs different actions based on that property value. The CommEvent property values and their meanings are as follows:
1). Comevsend: The value is 1, and the content of the send buffer is less than the value specified by Sthreshold.
2). Comevreceive: The value is 2 and the number of characters in the receive buffer reaches the value specified by RThreshold.
3). Comevframe: The value is 1004, and the hardware detects a frame error.
4). Comevrxover: The value is 1008 and the receive buffer overflows.
5). Comevtxfull: The value is 1010 and the send buffer overflows.
6). Comevrxparity: A value of 1009, parity error.
7). Comeveof: The value is 7, and the end of the file (ASCII 26) characters appear in the received data.
4. Application examples
The following is an example of Delphi using MSComm Communication Control, the development of PC through the serial port of a test plant pavement load real-time data collection programming technology. The program code given is universal and has detailed comments, which can be used in other data acquisition or real-time control programs directly or slightly.
The Project host computer using Delphi compilation, data acquisition instrument using Advantech ADAM5510 as the next computer to collect real-time pressure data, the use of ASCII transmission of data, the transmission rate is 9600bps,1 bit start bit, 8 bits of data bits, 1 bit stop bit, no parity check bit. The specific format for data communication is:.
The global variables used by the program are:
receivestr:string; Accepts ASCII code character data strings
f1,f2,f3,f4,fz,fa:double; Four-way pressure value data, total pressure value, average pressure value
The pressure curve drawn in the program takes the Tchart component, and the variable is defined as:
Fchart:tchart;
1). Serial initialization
With MSComm do
Begin
Commport:=1; Using COM1
settings:=9600,n,8,1; Setting Communication port Parameters
Inputlen:=1; Set input to read bytes from the accept buffer at a time of 1
inbuffercount:=0; Clear Accept Buffer
outbuffercount:=0; Clear Send Buffer
inbuffersize:=50; Set the Accept buffer to 50 bytes
outbuffersize:=2; Set send buffer to 2 bytes
Rthreshold:=1; Set accept one byte to produce OnComm event
Inputmode:=cominputmodetext; Set the Accept data pattern to binary mode
if (not portopen) then//determine if the communication port is open
Portopen:=true
Else
ShowMessage (serial port already open!);
End
2). Data reception and processing
In order to achieve real-time data acquisition, the real-time data acquisition and processing program adopts MSComm event-driven mode.
Procedure Tform1.mscommcomm (Sender:tobject);
Var
Buffer:olevariant;
stemp:string;
Cnow:char;
tmpx:double;
Begin
I f mscomm.commevent = Comevreceive then//Accept Event
Begin
Buffer:=mscomm.input; Read the Received data
Stemp:=buffer;
CNOW:=STEMP[1];
if ((CLAST=CHR) and (CNOW=CHR)) Then
Begin
Sf1:=trim (sF1);
Df1:=strtofloat (sF1);
With Seriesdepth do//Draw depth curve with Tchart
Begin
Tmpx:=xvalues[1]-xvalues[0];
Delete (0);
AddXY (xvalues.last+tmpx,df1,,clged);
End
sf1:=;
End
if ((CLAST<>CHR) and (CNOW<>CHR)) Then
Begin
Sf1:=sf1+cnow;
End
Clast:=cnow;
End
End
3). Serial Port off
If (Mscomm.portopen) Then
Mscomm.portopen:=false;
Delphi uses MSComm to process 16 binary () The following code is for send and receive as 16 binary processing. Var SENDDATA:ARRAY[1..10] of Char; Redata:array of Variant; sendstr:string; restr:string; I:longint; Begin Mscomm1.commport: = 1; Specify port MSComm1. Settings: = ' 9600,n,8,1 '; Other parameters MSComm1. Inbuffersize: = 1024; Receive buffers MSComm1. Outbuffersize: = 1024; Send buffer MSComm1. Inputmode: = cominputmodebinary; Receive mode MSComm1. Inputlen: = 0; Read all data at once MSComm1. Sthreshold: = 0; Send all data at once MSComm1. InBufferCount: = 0; Emptying the Read buffer MSComm1. OutBufferCount: = 0; Emptying the Send buffer MSComm1. Portopen:=true; Open port Mscomm1.rthreshold: = 16; Sets the number of bytes received to generate the OnComm event SENDDATA[1]:=CHR ($06); The data to be sent SENDDATA[2]:=CHR ($03); SENDDATA[3]:=CHR ($00); SENDDATA[4]:=CHR ($03); SENDDATA[5]:=CHR ($); Sendstr:= "; For I:=1 to 5 do Sendstr:=sendstr + senddata[i]; MSCOMM1.OUTPUT:=SENDSTR; Send data i:=0; Bzw:=false;
Repeat Sleep (10); Application.processmessages; I: = i + 1; If i > 30000 Then Begin ShowMessage (' Send timeout! '); Break End Until bzw = true; Redata:=mscomm1. Input; Receive data Restr:= "; For i:=0 to Vararrayhighbound (redata,1) do Restr:=restr + Inttohex (redata[i],2) + "; MSComm1. Portopen:=false; Flatmemo1. TEXT:=RESTR; End OnComm Events Procedure Tform1.mscomm1comm (Sender:tobject); Begin Case Mscomm1.commevent of COMEVRECEIVE:BZW: = true; End End |
[Delphi Technology] Delphi MSComm Real-time serial communication