How to read and calculate temperature and humidity data and wind speed and direction data

Embedded systems are application-centered, based on modern computer technology, special computer systems that can flexibly tailor software and hardware modules according to user needs. It is widely used in industrial production, daily life, industrial control, aerospace and other fields. Embedded systems are usually implemented using the "software and hardware co-design" method, which requires designers to master not only the knowledge of embedded operating systems and embedded software development, but also the principles and programming methods of hardware interfaces and common peripherals. Qt has the characteristics of cross-platform and strong scalability, and has developed rapidly in recent years. Especially in the embedded field and the field of national defense science and industry, Qt has been widely used.

  1. Master the frame structure of UART protocol and Modbus protocol
  2. Learn how to use a logic analyzer
  3. Master the reading and calculation methods of temperature and humidity data and wind speed and direction data

Highlights of this chapter:

  1. Frame structure of UART protocol
  2. Using a logic analyzer

Difficulties in this chapter:

  1. Reading and calculation method of temperature and humidity data and wind speed and direction data

    1 Serial communication data format
    1.1 Protocol introduction
    UART is an asynchronous serial communication protocol that can achieve two-way full-duplex transmission. The UART breaks data into frames and transmits them bit by bit. The complete data frame consists of start bits, data bits, parity bits and stop bits. When transmitting data, there is an idle bit between the two characters, and the idle bit is logic 1, indicating that the line is in a waiting state.

1.2 UART related parameters
The baud rate is the number of binary code bits transmitted per second, the unit is bps. Common baud rates include 4800bps, 9600bps, 115200bps, 921600bps, etc.
The start bit is used to indicate the start of data transmission and is represented by a logic 0 data bit.
The length of valid data is often agreed to be 5, 6, 7 or 8 bits, usually 8-bit data. Data bits are transmitted sequentially from low bit to high bit.
The check bit is used to verify whether the data is correct, including odd check (odd), even check (even), 0 check (space), 1 check (mark), and no check (noparity). Odd parity requires that the number of logical 1's in the valid data and check digits is an odd number, even parity requires that the number of logical 1's in the valid data and check digits is an even number, 0 checksum and 1 checksum respectively require check digits Always 0 or 1, no parity bit is used.
The stop bit indicates the end of data transmission and must be logic 1. Common stop bit numbers can be 1, 1.5 or 2 bits.
1.3 UART communication process
During UART communication, the low bit is sent first, and then the high bit is sent. Here's an example:

Assuming the baud rate is 100bps, each bit lasts 10ms. The original data is 0110 0011B, which is 0x63. Convert this data into a UART data frame:
starting bit, valid data, check bit, stop bit
0 01100011 1 1 (transmission in reverse order)
, then the UART data frame is 1100 0110 10.
2 Use of USB to serial port module
When debugging hardware, you need to use the USB to serial port module to convert the USB interface signal into the TTL signal of the UART interface.

Serial communication equipment can be connected using a three-wire system, which is two data wires and a ground wire. The data line used to receive data is represented by English RX, Rx or RXD, and the data line used to send data is represented by English TX, Tx or TXD. The TX pin of device 1 is connected to the RX pin of device 2, and the RX pin of device 1 is connected to the TX pin of device 2.
3 Use of serial port debugging assistant
Serial port debugging assistant is a type of tool software that assists PC in serial port debugging. Commonly used serial port debugging assistants include XCOM, SSCOM, Wildfire Multi-Function Debugging Assistant, etc. The serial port debugging assistant software used here is XCOM V2.0.
1 Serial port control area
The serial port control area is used to set the COM number, baud rate, stop bit length, data bit length, parity type, etc.
2 Send control area
The send control area is used to control the parameters of sending data.

  • Scheduled sending: Automatically send data repeatedly according to a certain period.
  • Hexadecimal sending: The data to be sent is 41. Using hexadecimal to send will understand the data as 0x41 and send it; not using hexadecimal to send will understand the data as 0x34 0x31 (that is, the ASCII code of 4 and 1) and send it.
  • Send new line: Send a '\r\n' after the data.
    3. Receiving control area
    The receiving control area is used to control the display format and flow control method of the received data.
    4 Principle and use of GY-39 meteorological information module
    4.1 Function introduction
    GY-39 meteorological information module can measure various meteorological information such as air pressure, temperature, humidity, light intensity, altitude, etc. The chip inside the module can process the data. The default output method is UART.
    4.2 Data packet structure
    When the GY-39 module is working, it will send light intensity data packets and meteorological information data packets. Both types of data packets are composed of packet header, packet type, data amount, data and checksum.
    Light intensity data packet structure:
    Header flag (2 bytes): 5A 5A
    packet type flag (1 byte): 15
    Data length (1 byte): 04
    Data (4 bytes): 00 00 FE 40
    Checksum ( 1 byte): 0B

Meteorological information data packet structure:
header flag (2 bytes): 5A 5A
packet type flag (1 byte): 45
data length (1 byte): 0A
data (10 bytes): 0B 2D 00 97 C4 3F 12 77 00 9C
checksum (1 byte): FA
4.3 Data accuracy
The measurement results of light intensity (unit: lux), air pressure (unit: Pa) and altitude (unit: m) only retain integers, so only integers need to be transmitted when transmitting Convert to the corresponding binary number.
The measurement results of temperature (unit: ℃) and humidity (unit: %RH) are kept to two decimal places. During transmission, the value is first expanded a hundred times and then converted into the corresponding binary number. The measurement result may be a negative number, and the module will use the complement code to represent the negative result.
5 Use a logic analyzer to capture UART communication waveforms
5.1 1. Working principle of the logic analyzer
The logic analyzer can monitor and collect the data of the communication interface in real time. Its working principle is: monitor the data flow through the probe and send parallel data into The comparator performs level judgment and then outputs, and then the judgment results are sampled and sequentially stored, and finally the communication waveform can be displayed.
5.2 Main parameters of logic analyzer
(1) Sampling frequency. It determines the frequency range that the logic analyzer can collect signals, which is generally more than 4 times higher than the signal under test.
(2) Storage depth. It determines the length of time that the waveform can be collected at a fixed sampling frequency. The larger the storage depth, the longer the signal changes can be observed.
(3) Trigger conditions. Determines the timing when the logic analyzer starts collecting waveforms. Commonly used triggers include rising edge, falling edge, high level and low level trigger.
5.3 Use a logic analyzer to capture the communication waveform of the GY-39 module
(1) Connect the TX pin of the GY-39 module to the logic analyzer, and connect the two to the PC.
(2) Set the sampling frequency of the logic analyzer to 2MHz and the sampling time to 2s. Set the channel name to UART_TX and the trigger mode to falling edge trigger.
(3) Start the logic analyzer and automatically start collecting and displaying the communication waveform of the GY-39 module. The white dots in the waveform represent data bits in a frame of data. 4. Use PC to read the measurement data of GY-39 module
(1) Connect the GY-39 module, USB to serial port module and PC.
(2) Open the corresponding serial port in the serial port debugging assistant XCOM, set the baud rate to 9600, the data bit to 8 bits, and the stop bit to 1 bit.
(3) Open the serial port and observe the received data. The GY-39 module sends data at 1Hz by default and can be received without any operation.
5.4 Calculation of GY-39 module measurement data
According to the data manual, the data sent by the GY-39 module is:
light intensity = (front high 8 digits << 24) | (front low 8 digits << 16) | (rear high 8 digits <<8)|Rear low 8 digits/10 lux
temperature=((High 8 digits<<8)|Low 8 digits)/100 °C
air pressure=(Front high 8 digits<<24)|(Front low 8 digits< <16)|(High 8 digits<<8)|Low 8 digits/100 Pa
humidity=(High 8 digits<<8)|Low 8 digits/100 %RH
Altitude=(High 8 digits<<8) |Low 8 bits m
6 RS485 and Modbus protocol
6.1 1. RS485 communication interface principle
RS485 is a multipoint communication interface standard formulated by the Federation of American Electrical Industries. It works in a master-slave communication mode and is suitable for long-distance, high-sensitivity multipoint communication. . The RS485 interface has two signal lines A and B and works in the form of differential signals. The commonly used rate in long-distance communication is 9600bps, and the communication distance can reach 500~1500 meters.

6.2 3. Modbus protocol principle
Modbus is a serial communication protocol commonly used in the industrial field. It was developed by Modicon in 1979 for the communication of programmable logic controllers. In 2004, the China National Standards Committee officially made the Modbus protocol a national standard (GB/T 19582.2-2008 "Industrial Automation Network Specification Based on Modbus Protocol Part 2: Implementation Guide for Modbus Protocol on Serial Links").
Modbus protocol is widely used in automation control, environmental monitoring, energy management, industrial automation, building automation and other fields. The Modbus protocol is simple, open, and easy to implement, and can be easily applied to communications between various devices.
6.3 Modbus protocol frame structure
Modbus data frame is divided into four parts: device address, function code, data, and check code.

The device address is a byte indicating the address of the slave. Among them, address 0 is the broadcast address, 1 247 is the address available to the slave, and 248 255 is the reserved address. The host can choose the communication object through the address.
A function code is a byte that indicates the operation requested by the host. The Modbus protocol stipulates some commonly used function codes, such as function code 03 for reading holding registers, function code 16 for writing multiple registers, etc.
The length of the data part does not exceed 252 bytes. If the frame is sent by the host, this part is the parameter requested by the host. If it is a frame sent by the slave, this part is the data or exception code returned by the slave.
The check code is data with a length of two bytes and is used to check all bytes in the device address, function code, and data area. Modbus protocol uses CRC16 algorithm for verification.
According to different uses, Modbus frames can be divided into inquiry frames sent by the host to the slave and response frames sent by the slave to the host.
7 PR-3000 Wind Speed ​​and Wind Direction Module
7.1 Module Introduction
This module consists of wind speed module and wind direction module. The wind speed module uses three wind cup sensors to detect wind speed, and the wind direction module detects wind direction through arrow rotation.
The working voltage of the wind speed and wind direction module is 10-30V, the interface is RS485, and the maximum communication rate is 9600bps.
7.2 Wiring method
Connect the power supply, ground wire, and RS485 A and B wires of the PR-3000 module to the UART to RS485 module. The RS485 lines cannot be connected reversely, and the device addresses on the same network cannot be repeated.
7.3 Module address setting
Modbus protocol requires unique slave addresses on the same network. The factory default addresses of the wind speed and wind direction modules are both 1 and need to be modified before use.
7.4 Modbus register address
According to the data sheet, the Modbus register address of the wind and wind direction module is as follows:
Wind speed module (address 1):
Wind speed value: 0x00 (high byte), 0x01 (low byte), unit 0.1m/s.
Wind direction module (address 2):
Wind direction value: 0x00, value 0-7, corresponding to north, northeast, east, southeast, south, southwest, west, northwest.

Guess you like

Origin blog.csdn.net/weixin_40933653/article/details/133443250