Application of Spcomm Control in Delphi Serial Communication

Source: Internet
Author: User
Application of Spcomm Control in Delphi Serial Communication

22:20:31 | category: personal diary | report | font size subscription

Application of Spcomm Control in Delphi Serial Communication

Abstract: The use of Delphi to develop industrial control system software has become the choice of more and more developers, 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 focuses on the use of the Spcomm Control in the Delphi development environment to achieve serial communication between PC and single chip microcomputer, the key technical problems of Spcomm serial communication are studied. An example is provided to show the application of Spcomm Control in Delphi7.0 serial communication.
Keywords: Serial Communication; Delphi7.0; Spcomm Control

1 Introduction

At present, with the development of modern information technology, computer serial communication technology has become increasingly mature. Because of the high performance and price ratio of the microcomputer, strong analytical processing ability, fast processing speed, strong anti-interference ability of the single chip microcomputer, and flexible use, the PC is used as the host computer, single-chip microcomputer is widely used as the master-slave mode in industrial control.

The communication between the PC and the lower computer can be implemented through advanced programming languages, such as Delphi and VC. Delphi is a new-generation object-oriented visual development tool. It features powerful functions, ease of use, and fast code execution. It is playing an important role in building enterprise information systems. Thanks to the notable features of Delphi, the use of Delphi to develop industrial control system software has become the choice of more and more developers, the key to implementing system monitoring control and information processing is to solve the problem of serial communication between PC and single chip microcomputer.

2. Introduction to Spcomm serial communication components

Use Delphi to implement serial communication. Common methods include using controls, such as MSComm and Spcomm controls, using API functions, and calling other serial communication programs in Delphi [1]. The advantage of using the API method is that it is more suitable for compiling complex low-level communication programs, but the disadvantage is that writing serial communication programs is more complicated and requires a lot of communication knowledge. Spcomm is a third-party Delphi serial control developed by small-pig team [2]. It has a wide range of attributes and events closely related to serial communication and provides various operations on serial ports, simple programming, strong versatility, and good portability. It has become a widely used serial communication development control in Delphi software development.

Spcomm implements three categories: Serial Port tcomm, read thread treadthread, and write thread twzitethread [1]. An instance of tcomm opens the serial port in method startcomm and instantiates a read thread readthread and a write thread writethread. messages are transmitted between the instance and the main thread to implement serial communication.

To install the Spcomm Serial Communication Control in Delphi7.0, select the "component" menu in Delphi7.0 and click "install Component... "menu item, and then select Spcomm in the unit file name input box on the into existing package property page. in the PAS file, click the "OK" button twice to install the Spcomm Control on the system component page of Delphi7.0.

3 Implementation of Spcomm Control Serial Communication

3.1 basic attributes, methods, and events of the Spcomm Control

The basic attributes, methods, and events of the Spcomm serial communication control are described as follows:

Commname attribute: name of the computer serial port number, COM1, com2 ...... Before enabling the serial port, you must enter this value.

Parity attributes: Check bit none, odd, even, Mark, space, etc.

Baudrate: Set the baud rate (,) that can be used to support serial communication. You can change the baud rate after enabling the serial port based on your actual needs.

Bytesize attribute: indicates the number of data bits used to send and receive data in a byte. The values include 5, 6, 7, and 8.

Stopbits attribute: indicates the number of bits in a byte that use the Stop bits. Set the number of BITs according to the actual situation, such as 1, 1.5, and 2.

Senddataempty attribute: Boolean attribute. If it is set to true, the sending cache is empty, or there is no information in the sending queue. If it is set to false, the sending cache is not empty, or there is information in the sending queue.

Startcomm method: This method is used to open the communication serial port and start communication. If it fails, it will cause a serial port error. The error types are roughly divided into the open status of the serial port, so the serial port cannot be opened, the read/write process cannot be created, and the serial port buffer cannot be created.

Stopcomm: This method is used to stop all processes of a communication serial port and disable the serial port.

The writecommdata (pdatatowrite: pchar; dwsizeofdatatowrite: Word) method is a function with a Boolean return value. The parameter pszstr-ingtowrite is the string to be written to the serial port, dwsizeaf-datatowrite is the length of the string to be written. This function sends data to the buffer zone of the serial port through a write thread. The sending operation is performed by default in the background. If the write thread postmessage succeeds, the return value is true. If the write thread fails, the return value is false.

Onreceivedata (buffer: pointer; bufferlength: Word), where buffer is a pointer to the input buffer. Bufferlength is the length of data received from the buffer. This event is triggered when the input buffer receives data. When the input cache contains data, this event is triggered to process the data received from the serial port.

3.2 Implementation of Spcomm Serial Communication

The Spcomm serial communication control has the multithreading feature. The receiving and sending data are completed in two threads respectively. The receiving thread is responsible for triggering the onreceivedata event when receiving the data; using writecommdata () the function writes the data to be sent to the output buffer, and the sending thread completes data transmission in the background. Before receiving and sending data, you need to initialize the serial port. Use startcomm to open the serial port, and use stopcomm to close the serial port when exiting the program.

To send and receive data between a PC and a single-chip microcomputer, follow these steps:

(1) initialize and enable the serial port

You need to select the serial port used for this communication and determine the communication protocol, that is, set the baud rate, verification method, data bit, stop bit, and other attributes to open this serial port. The sample code is as follows:

// Initialize and enable the serial port

Comm1.baudrate: = 9600; // baud rate 9600bps

Comm1.parity: = none; // No parity check

Comm1.bytesize: = 8; // data bit 8

Comm1.stopbits: = 1; // stop bit 1

Comm1.startcomm; // open the serial port

(2) establish a handshake Signal

To achieve communication between PCs and single-chip microcomputer, you must first call the handshake signal between them. The handshake signal can select a specific string at will. When the PC sends such a frame of data, by receiving an event, you can receive this frame of data or a specific string returned by the microcontroller. This indicates that the handshake is successful and the system communication is normal. The two can transmit data to each other according to the protocol. Otherwise, a new handshake signal is required.

(3) send data

When writing serial-Port-Based Computer Industrial Measurement and Control, a PC usually needs to send commands to the lower computer to control the behavior of the lower computer and send relevant data to the lower computer at the same time. Sample Code for sending data to the lower computer using the Spcomm serial control 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) receive data

When writing serial port-Based Computer Industrial Measurement and Control, the lower computer usually needs to send data to the PC to understand the test data of the system or the running status of the lower computer, and then control the behavior of the lower computer. The following is an example code that uses the Spcomm serial control to receive data sent by a lower computer:

// Event-driven data receiving program

Procedure tform1.commlreceivedata (Sender: tobject;

Buffer: pointer; bufferlength: Word );

VaR

Receivedata: array of byte;

Begin

Sleep (100); // wait for 100 ms to ensure that all data is received

Move (buffef, receivedata, bufferlength );

// Transfer the data in the receiving cache to the array

......

End;

(5) disable the serial port

During system development, you should disable the serial port in time to release system resources without using the serial port; otherwise, other applications of the system may be affected. The code for disabling the serial port is as follows:

Procedure tform1.formclose (sender; tobj ect: var action: tcioseaction );

Begin

Comml. stopcomm;

End;

4 key technical issues of Spcomm Serial Communication

The core of the Spcomm application is the message transmission mechanism between the main thread, the read thread, and the write thread, and the communication data-related information transmission is also carried out in the form of message transmission. When using Spcomm for serial communication programming, pay special attention to the following two issues in addition to instructions.

First, Spcomm sets the readintervaltimeout attribute to determine whether the received data belongs to the same frame of the child. The default value is 100 ms, that is, as long as the interval between the arrival of any two bytes is less than 1 Ooms, it is 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, Spcomm's default attribute settings support software flow control. The characters used for flow control are 13 H (xoffchar) and 11 h (xonchar). When the MCU sends data in binary mode, you must disable Spcomm's support for software traffic control. Otherwise, the 13 H and 11 h appearing in the data frame will be ignored by Spcomm as the control character.

5 conclusion

Practice has proved that the use of Spcomm serial communication control to develop serial communication programs in Delphi7.0 is flexible, convenient, and efficient. The innovation of this article is based on a careful analysis of multiple computer serial port-based industrial control and control systems, analysis of the use of Spcomm controls to achieve serial communication between PC and single chip microcomputer methods, the key technical problems of Spcomm serial communication are studied. It has achieved good application results in practical applications and has certain practical guiding significance.

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.