Delphi uses Tcomm component Spcomm for serial communication

Source: Internet
Author: User

Delphi uses Tcomm component Spcomm for serial communication

Absrtact: Using Delphi to develop industrial control system software becomes more and more developer's choice, and serial communication is one of the problems that must be solved in this process. Based on the analysis and comparison of several common serial communication methods, this paper emphatically discusses the method of using Spcomm control to realize serial communication between PC and MCU in Delphi development environment, and studies the key technical problems of SPCOMM serial communication. The application of Spcomm control in Delphi7.0 serial communication is given through an example.


1 Introduction
At present, with the development of modern information technology, computer serial communication technology has become more and more mature. Because of the high cost of computer performance, analysis and processing ability, fast processing speed and strong anti-jamming ability of single-chip microcomputer, the use of PC as the host computer, single chip microcomputer as the subordinate machine master-slave working mode in the field of industrial control is widely adopted. Worry-free s3c2410 ARM9 Development Board s3c44b0 ARM7 Development Board 350 yuan single-chip microcomputer development system 498 yuan worry-free single-chip computer experimental Development Board 238 yuan
The communication between PC and subordinate computer can be realized by high-level language programming, such as Delphi, VC, etc. Delphi is a new generation of object-oriented visual development tools, it has a powerful, easy-to-use and code execution speed, and more and more in the framework of enterprise information systems play an important role. Because of these notable characteristics of Delphi, the use of Delphi to develop industrial control system software has become more and more developers choice, and the key to realize system monitoring control and information processing is to solve the serial communication between PC and MCU.
2. Introduction to Spcomm Serial communication components
Using Delphi to achieve serial communication, commonly used methods are: the use of controls, such as MSComm and Spcomm controls, using API functions, in Delphi calls other serial communication program [1]. The advantage of using API method is more suitable for writing more complicated low-level communication program, but the disadvantage is that it is more complicated to write serial communication program and need to master a lot of communication knowledge. Spcomm is a third-party Delphi serial port control developed by Small-pig team [2], which has rich properties and events related to serial communication, provides various operations on the serial port, and has simple programming, strong versatility and good portability. In the development of Delphi Software has become a widely used serial communication development control.
Spcomm has implemented three classes: Serial port class Tcomm, read thread class Treadthread, and Write thread class Twzitethread[1]. An instance of Tcomm opens the serial port in method Startcomm, and instantiates a read thread readthread and a write thread Writethread, which communicates with the main thread and implements the serial communication.
To install the Spcomm serial communication control in Delphi7.0: Select the Delphi7.0 "Component" menu, click on the "Install Component ..." menu item and then in the popup into existing After you select the Spcomm.pas file in the Unit file name box in the Package property page, and click the OK button two times, you can install the Spcomm control on the Delphi7.0 system Components page.
Implementation of serial communication of 3 Spcomm control
3.1 Basic properties, methods, and events for Spcomm controls
The basic properties, methods, and events of the Spcomm serial communication control are described below:
Commname Properties: Computer serial port number name, COM1, COM2 ... Wait, this value must be filled in before opening the string.
Parity properties: Check digit None, ODD, even, Mark, space, etc.
BaudRate: Set to support serial communication with the baud rate 9600,4800, according to the actual needs to be fixed, the serial port can also be changed after the baud rate, the actual baud rate with the change.
ByteSize property: Represents a byte, the number of data bits used to send and receive data, according to the specific circumstances set 5, 6, 7, 8 and so on.
StopBits Property: Represents the number of bits in a byte, using the Stop bit, set 1, 1.5, 2, and so on the specific case.
Senddataempty Property: Boolean property, True when the send cache is empty, or there is no information in the send queue, or false indicates that the send cache is not empty, or that there is information in the Send queue.
Startcomm method: Used to open the communication serial port, start communication. If it fails, it causes a serial port error. The error type is broadly divided into the serial port is open, so the serial port cannot be opened, cannot create a read-write process, cannot establish a serial port buffer, etc.
Stopcomm method: Used to stop the communication serial port of all processes, close the serial port.
The Writecommdata (Pdatatowritechar;dwsizeofdatatowrite:word) method is a function with a Boolean return value, where the parameter pszstr-ingtowrite is the string to be written to the serial port. Dwsizeaf-datatowrite is the length of the string to write. The function sends data to the serial port output buffer through a write thread. The send operation is performed by default in the background. If the write thread PostMessage succeeds, the return value is true, and if the write thread fails, the return value is false.
Onreceivedata (Bufferointer; Bufferlength:word), where buffer is a pointer to the input buffer. Bufferlength is the length of data received from the buffer. The event is triggered when the input buffer receives data. This event is triggered when the input cache has data, and the data received from the serial port is processed.
Realization of 3.2 Spcomm serial communication
Spcomm Serial communication control has the characteristics of multithreading, receiving and sending data in two threads, respectively, the receiving thread is responsible for receiving the data when the Onreceivedata event is triggered, and the data to be sent is written to the output buffer with the Writecommdata () function. The sending thread completes the data sending work in the background. Before receiving and sending the data need to initialize the serial port, using Startcomm method to open the serial port, exit the program with the Stopcomm method to close the serial port.
The following steps are needed to transmit and receive data between PC and MCU:

(1) Initialize and open the serial port


Need to choose the communication using the serial port, determine the communication protocol, that is, set the baud rate, check mode, data bits, stop bits and other properties, open the serial port. The sample code is as follows:

Initialize and open the serial port
comm1.baudrate:=9600;//baud rate 9600bps
comm1.parity:=none;//Parity No
comm1.bytesize:=8;//Data bits 8
comm1.stopbits:=1;//Stop bit 1
Comm1.startcomm; Open the serial port

(2) Building a handshake signal

The realization of the communication between the PC and the microcontroller, the first to adjust the handshake between them, the handshake signal can choose a particular string, when the PC sends such a frame of data, by receiving the event can receive a single-chip computer returned by the frame of data or a specific string, then the handshake is successful, the system communication is normal. The two can transfer data to each other according to the protocol. Otherwise, the handshake signal needs to be re-established.

(3) Send data

In the process of writing serial port-based computer industry, it is usually necessary to send the command by PC to control the behavior of the subordinate machine, and send the relevant data to the down-level machine. The sample code for sending data using the Spcomm serial control down-level machine is as follows:
Send data and control word programs
Procedure SendData;
Var
I:integer; Commflg:boolean;
Begin
Commflg:=true;
For I:=1 to 8 do
Begin
If not Fcomm comml writecommdata (sendbutter,i) Then
Begin
Commflg=false;
Break
End
End
End

(4) Receiving data

In writing serial-based computer industry measurement and control, it is usually necessary to send data to PC machine to make the PC understand the test data of the system or the running state of the lower machine, and then control the behavior of the subordinate machine. The sample code that uses the Spcomm serial control to receive the data sent by the lower machine is as follows:
Event-driven approach to receiving data programs
Procedure Tform1.commlreceivedata (Sender:tobject;
Bufferointer; Bufferlength:word);
Var
Receivedata:array of Byte;
Begin
Sleep (100);//wait 100ms to ensure all data is received
Move (BUFFEF, receivedata,bufferlength);
Transfer data from the receiving buffer to the array
......
End

(5) Close the serial port

In the system development, should pay attention to not use the serial port should close the serial port in time, release the system resources, otherwise may affect the system other application. The code to close the serial port is as follows:
Procedure Tform1.formclose (Sender; Tobj Ect:var action:tcioseaction);
Begin
COMML. Stopcomm;
End
Key technical problems of 4 Spcomm serial communication
The core of Spcomm application is the message passing mechanism between the main thread, the read thread and the writing thread, and the transmission of the communication data related information is carried out in the way of message passing. In the use of SPCOMM serial communication programming, in addition to follow the instructions to use, but also need to pay special attention to the following two questions.
First, Spcomm is the setting of the ReadIntervalTimeout property to determine whether the received data is a child of the same frame of data, the default value is 100ms, that is, as long as any two bytes arrive at a time interval of less than 1OOms, are considered to belong to the same frame of data, When working with a single chip microcomputer, pay special attention to this problem [2].
In addition, the default property settings for Spcomm are support for software flow control, the characters used for flow control are 13H (XoffChar) and 11H (XonChar), and when a microcontroller sends data in binary mode, it must disable Spcomm support for software flow control, otherwise, The 13h,11h that appear in the data frame are ignored by the Spcomm as the control character.
5 Conclusion
The practice proves that using SPCOMM serial communication control to develop serial communication program in Delphi7.0 has the characteristics of flexibility, convenience and high efficiency. The author of this paper is based on the analysis of a number of computer serial control and control system, analyzes the method of the serial communication between PC and SCM by using Spcomm, and studies the key technical problems of SPCOMM serial communication. It has achieved good application effect in practical application, and it has certain practical guiding significance.

Delphi uses Tcomm component Spcomm for serial communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.