Delphi is a new generation of visual development tools. It has the characteristics of powerful, simple and easy to perform speed, and is a pioneer of the world's recognized rapid application development tool technology. It is increasingly played an important part of the information system of architecture enterprise. effect. Due to these significant features of Delphi, many programmers choose Delphi as a development tool to prepare a variety of applications. However, it is a pity that Delphi has no self-contained control, and has not mentioned serial communication in its helper document, which brings a lot of trouble to develop program developers, affecting development progress, below This technology discusses.
Use Delphi to realize serial communication, several common methods are:
Use controls such as MSCOMM and SPCOMM,
Use an API function or in Delphi
Call other serial communication programs. Using the API to write serial communication programs more complicated, you need to master a large number of communication knowledge, and the advantage is that the function is more powerful, more widely applied, more suitable for more complex low-level communication programs. In contrast, the use of SPCOMM controls is relatively simple, which has a wealth of attributes and events closely related to serial port communication, providing various operations for serial ports.
This method using the control is easy to master, and SPCOMM supports multi-threaded, so the application of the SPCOMM control is wider. Introduction to the use of SPComm in detail.
One. Installation of the SPCOMM control
1. Select the second INSTALL Component of the drop-down menu component.
figure 1
The window shown in Figure 1 is popped, and the path to the control SPCOMM control is filld by the Unit File Name. Other use default values, click OK to press New.
2. After the installation is successful, a red control COMM will appear in the System Control Panel. Now use the COMM control, you can use it as a self-contained control of Delphi.
two. The main properties, methods, and events of SPCOMM 1. Properties Commname: Fill in serial ports such as COM1, COM2 ..., you must fill in this value before opening the serial port. BaudRate: Set the baud rate 9600, 4800, etc., according to the actual needs, the baud rate can be changed after the serial port is opened, and the actual wavetel rate will change. ParityCheck: parity. BYTESIZE: byte length _5, _6, _7, _8, etc., according to the actual situation. Parity: Parity SendDataEmpty: This is a Boolean property, indicating that the send cache is empty when True, or there is no information in the send queue; indicates that the send cache is not empty, or the send queue information. 2. Method STARTCOMM processes are used to open the serial port. When it is open, it will usually report error, and the error has mainly 7 kinds: (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 a read process; ⑺ can't generate a write process; the StopComm process is used to turn off the serial port without returning the value. Function WriteCommdata (PDATATEOWRITE: WORT): Boolean is used to send a string to write thread, send successfully returns true, send fails to return false, performing this function will immediately get the return value, the send operation is then executed. The function has two parameters, where pDatatowrite is the string to send, DWSIZEOFDATATOWRITE is the length of the transmitted. 3. Event OnreceiveData: Procedure (Sender: Tobject; Buffer: Pointer; BufferLength: Word) Of Object When the cached has data, the event will be triggered where data is processed from the serial port. Buffer is received, and bufferLength is the length of data received. OnreceireError: Procedure (Sender: Tobject; Eventmask: DWORD) The event will be triggered when the data is received. three. Using SPCOMM Next, we combine a serial communication example to illustrate the use of SPCOMM. In order to achieve communication between the PC and the microcontroller 8051, first, the handshake signal between them is first, assume that the communication protocol between them is, and the PC to 8051 one-frame data 6 bytes, 8051 to PC one frame data is also 6 bytes, when the PC is issued (F0, 01, FF, FF, 01, F0), can be received such a frame (F0, 01, FF, FF, 01, F0), indicating that the data communication is successful, both The data can be transferred in accordance with the protocol. The following steps are required to send and accept data in the PC party: 1. Create a new project Comm. DPR, change the name property of the form to FCOMM, change the title of the form to test communication, add controls. The property design is performed on the COMM1 (the control surrounded by black rectangles), the baud rate 4800, the check digit, byte length _8, stop bit _1, serial port selection COM1. The data sent and accepted in Memo1 will be displayed. Select File / Save AS to store new forms as comm.pas.