Decree of serial communication in Delphi

zhaozj2021-02-17  87

Delphi is a visual rapid application development tool with advantages such as powerful, easy to use and code execution speed. It plays an increasingly important role in architectural enterprise information systems. Many programmers are willing to choose Delphi as development tools. Prepare a variety of applications. However, the deficiencies in the US is that Delphi has no self-contained serial communication control, and has not mentioned serial communication in its helper document, which brings many inconveniences to developers developers.

At present, there are three common methods for realizing serial communication using Delphi: First, use controls such as MSCOMM controls and SPCOMM controls; the other is to use the API function; third is to call other serial port communications programs. Among them, the use of the API to write a serial port communication program is more complicated, and a large number of communication knowledge is needed. In contrast, the use of SPCOMM controls is relatively simple, and the control has a wealth of attributes and events closely related to serial communication, providing various operations for serial ports, but also support multithreading. The use of the SPCOMM control is described in detail below in conjunction with the example.

SPCOMM installation

1. Select the Install Component option in the drop-down menu Component, pop up the window shown in Figure 1.

figure 1

Fill in the path where the SPCOMM control is filld by the Unit File Name, and the other items can be used by default, click the OK button.

2. After installation, a red control CoM will appear in the System Control Panel. Now you can use a COM control like Delphi combo controls.

Properties, methods, and events of SPCOMM

Property

● COMMNAME: Represents the name of serial ports such as COM1, COM2;

● BaudRate: This value can also change this value after the serial port is opened, and the actual wavetect rate is changed after the actual needs set.

● ParityCheck: Indicates if a parity is required;

● BYTESIZE: The length of the byte set according to the actual situation;

● Parity: parity;

● Stopbits: Stop bit;

● Senddataempty: This is a Boolean property that indicates that the send cache is empty, or there is no information in the send queue; when the send cache is not empty, or the send queue has information.

2. method

● STARTCOMM method is used to open the serial port, which usually reports in an error when opening fails. There are mainly 7 kinds of errors: (1) The serial port has been opened; (2) Open the serial port error; (3) The file handle is not a communication handle; ⑷ can't install communication cache; ⑸ can't generate an event; ⑹ can't generate a process;

● The StopComm method is used to turn off the serial port, no return value;

● WriteCommdata (PDATATOWRITE: PCHAR; DWSIOFDATATE: WORD) method is a function with a Boolean return value, used to send a string to the write process, send success to Return true, send fails to return false. Executing this function will immediately get the return value, the send operation is then executed. This function has two parameters, where pDatatowrite is the string to send, DWSizeOfDataTowrite is the length of the sending string.

3. event

● OnreceiveData: Procedure (Sender: Tobject; Buffer: Pointer; BufferLength: Word) Of Object

This event will be triggered when there is a data input cache, where data received from the serial port can be processed. Buffer is received, and bufferLength is the length of data received. ● OnRecEireError: procedure (sender: Tobject; Eventmask: DWORD)

This event will be triggered when an error occurs.

SPCOMM

Below is an example of serial communication using the SPCOMM control.

Take the communication between the PC and the single-chip 8051 as an example, first to transfer the handshake signal between them. It is assumed that the communication protocol between them is: 6 bytes of PC to 8051, and 8051 to PC one frame data is also 6 bytes. When the PC is emitted (F0, 01, FF, FF, 01, F0), 8051 can receive a frame (F0, 01, FF, FF, 01, F0), indicating that the data communication is successful, and the two can be followed. The protocol is transmitted to each other.

Create a new project comm.dprise, set the form's Name property to FCOMM, define the title of the form as test communication, add controls as shown in Figure 2 (the control of the black rectangle in Figure 2 is COMM1).

figure 2

1. Set the COMM1 property:

● Porture rate: 4800;

● Parity position: None;

● byte length: 8;

● Stop bit: 1;

● Serial port: COM1.

The data sent and received in MEMO1 will be displayed. Store new forms as comm.pas.

2. Write the source code

// Variable description

VAR

FCOMM: TFCMM;

ViewString: string;

i: integer;

RBUF, SBUF: Array [16] of byte;

// Open the serial port

Procedure TFComm.FormShow (Sender: TOBJECT);

Begin

Comm1.startComm;

END;

// Turn off the serial port

Procedure tfcomm.formclose (Sender: TpoBject; var Action: tclosection);

Begin

Comm1.stopComm;

END;

/ / Customize the sending data process

Procedure senddata;

VAR

i: integer;

Commflg: boolean;

Begin

ViewString: = '';

Commflg: = true;

For i: = 1 to 6 do

Begin

IF not fcomm.comm1.writecommdata (@sbuf [i], 1) THEN

Begin

COMMFLG: = false;

Break;

END;

// Time to send time by time

Sleep (2);

ViewString: = ViewString INTTOHEX (SBUF [i], 2) '';

ViewString: = 'Send' ViewString;

FCOMM.MEMO1.LINES.ADD (ViewString);

FCOMM.MEMO1.LINES.ADD ('');

IF not command ('Send Fail!', mTerror, [mbyes], 0);

END;

// Send button Click Event

Procedure TFCMM.BTN_SENDCLICK (Sender: TOBJECT);

Begin

SBUF [1]: = Byte ($ F0); // Frame head

SBUF [2]: = Byte ($ 01); // Command number

SBUF [3]: = Byte ($ ff);

SBUF [4]: ​​= Byte ($ ff); sbuf [5]: = byte ($ 01);

SBUF [6]: = BYTE ($ f0); // frame end

Senddata; // Call the send function

END;

// Receive process

Procedure TFCMMM.COMM1RECEIVEDATA (Sender: Tobject; Buffer: Pointer; BufferLength: Word);

VAR

i: integer;

Begin

ViewString: = '';

Move (buffer ^, pchar (@rbuf ^), bufferlength;

For i: = 1 to bufferlength do

ViewString: = ViewString INTTOHEX (RBUF [i], 2) '';

ViewString: = 'Receive' ViewString;

Memo1.Lines.Add (ViewString);

Memo1.Lines.Add ('');

END;

If the transmit F0 01 FF FF 01 F0 is displayed on the MEMO1 and the received F0 01 FF FF 01 F0, this means that the serial port has been properly transmitted and the data is received, and the serial port communication is successful.

转载请注明原文地址:https://www.9cbs.com/read-31257.html

New Post(0)