0Introduction
The host computer design is an important part of the smart home system. It is responsible for management, control, maintenance and user interface operation. An efficient and reliable host computer design is an important guarantee for the normal operation of the entire system. The communication between the host computer and the system is actually the communication with the lower-end single-chip microcomputer. The single-chip microcomputer or single-chip microcomputer system is the core of smart home control. When the computer plays a role in the single-chip system, it is usually called an "industrial computer" or "host computer". Each independent single-chip microcomputer is a module that realizes the function in the system, so the single-chip microcomputer is sometimes also called a module.
Computers and single-chip microcomputers communicate through serial ports. This article focuses on the implementation method of serial port communication between computers and single-chip microcomputers and the MSComm control technology.
1 The role of the host computer in the smart home system
First of all, we must understand the role of computers in smart home systems, which can be summarized as follows:
1.1 Communication test tools
The communication test of the traditional single-chip microcomputer system is monitored by an oscilloscope, which is not intuitive and easy to lose information. By adding a computer to the system, the information in the line can be monitored in real time and presented digitally, so that the communication between each single-chip microcomputer can be tested and the problems in the system can be solved in time. Therefore, the role of computers in accelerating system development and ensuring the quality of system operation is self-evident.
1.2 System maintenance
A single-chip microcomputer system often needs to set parameters for each module (single-chip microcomputer) with different functions in the system to adapt to different operating environments. Although it can be adjusted through the control panel of each module, the operation is usually cumbersome and difficult for non-professionals to master. At the same time, it is difficult to implement some parameters that need to define their geographical location and are not intuitive enough. Adding a computer to the system can not only solve operational problems, but also read the parameters of each module for easy adjustment.
1.3 Single chip microcomputer
The computer can be used as a module of the single-chip microcomputer system to complete the corresponding functions. The computer responds to the system's requests and processes the information in the system. On the one hand, it presents the information to the user. On the other hand, the computer program feeds back the information to the system through automatic calculation or user input, and participates in the control of the system.
1.4 Main control machine
In a single-chip microcomputer system, there is often a master control machine or a master control module that is responsible for the overall planning and setting of the entire system. A single-chip microcomputer system usually implements its functions through the input and response of each functional module, so there must be a master control module responsible for collecting input tasks and specifying response modules. In a computer system, it can be either an ordinary module or a master controller responsible for overall coordination.
2 Communication Protocol
A single-chip microcomputer system is composed of a hardware system and a software system. Hardware is the premise for realizing functions, while system functions are realized through software. In order to ensure the communication between modules and realize system information exchange, a system communication protocol must be formulated.
Physically, the modules of the system communicate in half-duplex mode. The lower module of the intelligent lighting system mentioned below uses the half-duplex RS485 bus line as the communication architecture. The RS232 serial port of the computer realizes communication in full-duplex mode. Therefore, if the computer wants to participate in the single-chip microcomputer system through the serial port, a RS232 and RS485 bus conversion module must be added.
Logically speaking, to achieve communication between modules, a common communication protocol must be agreed upon between modules. The microcontroller uses bits (b) as the basic unit, and transmits 8 bits per byte (B). The microcontroller sends several bytes at millisecond intervals as a data packet, also known as a frame, and the interval between frames is several milliseconds. Each frame of information in the line can be captured through a computer program.
Taking the intelligent lighting system as an example, the communication protocol of the microcontroller system is described below.
This protocol uses asynchronous serial communication, byte frame format: 1 start bit + 8 data bits + 1 stop bit, no check bit.
The frame format is as follows:
l Frame = frame header (F4HF5H) + data packet + frame tail (F4HFBH) < 255B
Data packet = valid bytes (1B) + transport layer data + checksum (1B) < 251B
During the sending and receiving process of frame data, the transmission delay of the two bytes before and after shall not exceed 0.5s.
Each module is assigned a two-byte address code, the first byte represents the module type, and the second byte represents the order in which the modules are assigned in the system. After each module obtains a frame on the bus, it responds to its own address frame and discards other frames.
The following is the agreement on "light on/off control" in this agreement:
Description: This information is the on or off control of each street light of the output submodule.
Composition: When the information is sent, it consists of a message header and message content; when it is returned, it consists of a message header.
When the main controller or PC sends to the output module:
The communication of the system is carried out according to the specified protocols.
3 Implementation of Computer Communication
The computer hardware communicates with the lower module through RS232 and RS485 interface conversion. There are many ways to implement the software. The following mainly introduces the method of implementing communication through MSComm control in VB6.0.
3.1 Introducing the MSComm control into the VB platform
The MSComm control transmits and receives data through the serial port, providing serial communication functions for applications. The MSComm control is very convenient for serial port programming, and programmers do not have to spend time to understand more complex API functions, and it can be used in VC, VB, Delphi and other languages.
3.2 Setting of MSComm Control
3.2.1 Properties of MSComm Control
The MSComm control has many important properties. Correctly setting its properties is the guarantee for the normal communication of the program. Here are several properties that you must be familiar with.
CommPort: Sets and returns the communication port number.
Settings: Sets and returns the baud rate, parity, data bits, and stop bits in the form of a string.
PortOpen: Sets and returns the status of the communication port, and can also open and close the port.
Input: Returns and deletes characters from the receive buffer.
InputMode: Receive mode, when the value is 0, text data is received; when the value is 1, binary data is received.
Output: Writes a string to the transmission buffer.
Rthrehold: Response mode, take an integer. After receiving a character, if the Rthrehold property is set to 0, the OnComm event will not be generated; if it is set to 1, the MSComm control will generate an OnComm event for each character received in the receive buffer.
Sthreshold: Send mode, set to 0 (default value), data transmission events will not generate OnComm events; set to 1, when the transmission buffer is completely empty, the MSComm control will generate an OnComm event. If the number of characters in the transmission buffer is less than value, the CommEvent property is set to comEvSend, and an OnComm event is generated.
Handshake: Communication handshake mode. When the value is 0, there is no handshake. When the value is 1, it indicates XOn/Xoff handshake. When the value is 2, it indicates Request-to\-send/clear-to-send handshake. When the value is 3, both Request-to-send and clear-to-send handshakes are possible.
3.2.2 Two ways of handling communication in MSComm control
The MSComm control provides the following two ways to handle communications: event-driven and query.
(1) Event-driven approach
Event-driven communication is a very effective way to handle serial port interactions. In many cases, you need to be notified when an event occurs, for example, when there are characters in the serial port receive buffer, or when a character arrives or a change occurs on the CarrierDetect (CD) or RequestToSend (RTS) lines. In these cases, you can use the OnComm event of the MSComm control to capture and handle these communication events. The OnComm event can also check and handle communication errors. For a list of all communication events and communication errors, see the CommEvent property. During programming, you can add your own processing code to the OnComm event processing function. The advantage of this method is that the program responds promptly and has high reliability. Each MSComm control corresponds to a serial port. If an application needs to access multiple serial ports, multiple MSComm controls must be used.
(2) Query method
The polling method is still event-driven in nature, but in some cases it is more convenient. After each key function in your program, you can poll for events and errors by checking the value of the CommEvent property. If your application is small and self-sustaining, this method may be preferable. For example, if you are writing a simple phone dialer, there is no need to generate an event for every character received, because the only character waiting to be received is the "OK" response from the modem.
3.3 Implementation of computer information sending and receiving
The following is an example of the communication between a PC and a main controller in an intelligent lighting system to illustrate the implementation of the communication between a computer and a single-chip microcomputer. The following are the main subroutines for the implementation.
Page Loader:
After the communication is established, the computer can communicate with the downstream microcontroller through the set protocol and test the response of the downstream module.
4MSComm control problems and solutions
In actual applications, the MSComm control works well for question-and-answer communication, so it is not a big problem to use it for communication testing of a single module. However, if the computer, as a role in a single-chip system, needs to capture information on the bus in real time and respond, there will be problems of frame loss and frame error. If the system requirements are not high, it can still be dealt with. However, for systems such as intelligent lighting that require continuous, real-time and accurate information, it is difficult to use the MSComm control to handle serial communication.
In the intelligent lighting system, a self-compiled dynamic library Transdata, dll program is used as the interface for processing serial port communication. The frame error rate and frame loss rate are low, which can meet the requirements of productization.
Transdata.dll usage instructions:
(1) Initialize the serial port and put it in communication state
Init_OpenCom(byval m_str as string, byval nbaudas Integer) as Boolean
Parameter description: m_str is the name of the serial port, such as "coml" and "com2", etc.; nbaud is the baud rate, which can be 1200, 2400, 4800, 9600, etc.; the return value is Boolean, if it is true, it is successful, otherwise it fails.
Previous article:Design of home appliance control system based on power line carrier chip
Next article:Intelligent street lighting control system based on nRF905 and 555
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Case! EMI rectification of power supply
- Reasons for choosing 4-20mA for flow meter
- I want to ask why this motor cannot start? The voltage from B+ to B- is normal, but after starting, the voltage from B+ to M- jumps and the motor vibrates.
- Summary of equivalent circuits of common electronic components
- Comparison of several MicroPython IDEs
- [RVB2601 Creative Application Development] @fxyc87 RVB2601-Snake Game
- Qinheng USB PD and other fast charging protocol power receiving chip CH224 evaluation - the first data study and unboxing
- Analysis of the working principles of seven triode collector DC circuits 4
- Based on IPM05F 3Sh board: FOC motor control 400V non-sensing sensory encoder all design data summary (schematic diagram/BOM table...
- AD7190 Issues