Design of smart home host computer based on serial communication

Publisher:Dingsir1902Latest update time:2010-10-19 Source: 现代电子技术 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 host 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.

MSComm is an ActiveX control provided by Microsoft Corporation to simplify serial communication programming under Windows . It provides an easy way for applications to send and receive data through a serial interface. Specifically, it provides two methods for dealing with communication problems: one is the event-driven method and the other is the query method.

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:

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.

Reference address:Design of smart home host computer based on serial communication

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号