Decree of serial communication in Delphi

zhaozj2021-02-17  59

Decree of serial communication in Delphi

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. Installation of SPCOMM 1. Select the Install Component option in the drop-down menu component, fill in the path where the SPCOMM control is filld by the Unit File Name, and the other items can be used, 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. SPCOMM properties, methods and events 1. COMMNAME: Represents the name of serial ports such as COM1, COM2; ● BaudRate: This value can be changed after the serial port is opened, and the actual baud rate Change; ● ParityCheck: Indicates whether the parity is required; ● BYTESize: The length of the byte set according to the actual situation; ● Parity: parity: stop bit; ● STOPBITS: Stop bit; Indicates that the send cache is empty, or the send queue is not information; when the send cache is not empty, or the send queue has information. 2. Method ● STARTCOMM method is used to open the serial port, usually an error is usually reported when the open 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; ⑷ cannot be installed in communication cache; ⑸ can't generate an event; ⑹ can't generate a process; ● Stopcomm method Used to close the serial port, no return value; ● WriteCommdata (PDATATATE: WORD) method is a function with a boolean return value, used to send a string to the write process, send success to returns true, send failed 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 When there is a data input cache, the event will be triggered where data received from the serial port can be processed. Buffer is received, and bufferLength is the length of data received.

● OnRecError: Procedure (Sender: Tobject; Eventmask: DWORD) This event will be triggered when an error occurs. The use of SPCOMM is the 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 the corresponding control. 1. Set the COMM1 property: ● baud rate: 4800; ● parity bit: 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.

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

New Post(0)