当前位置:网站首页>Rs232/rs485 to 4G DTU uploading temperature and humidity sensor data based on Modbus protocol to remote TCP server

Rs232/rs485 to 4G DTU uploading temperature and humidity sensor data based on Modbus protocol to remote TCP server

2022-06-11 02:47:00 Seeking to fly

1 Hardware preparation

  1. 4G DTU a
  2. USB turn 232 An adapter line
  3. 485 A temperature and humidity sensor
  4. 12V Power Supply 2 individual
  5. LTE-Cat1 A IOT network card

2 Topology

DTU RS485 <-----> Sensors 485 Line (A Pick up A,B Pick up B)
DTU RS232 <-----> USB turn RS232 Adapter
DTU Power Supply ( Headphone hole power supply )<-----> DC 12V External power supply
Sensor power cord <-----> DC 12V External power supply

3 Prepare knowledge

3.1 What is? DTU

3.1.1 Concept

DTU(Data Transfer Unit): Data transmission unit , It is specially used to convert serial port data to IP Data or will IP Data conversion to serial port data , A terminal device that transmits through a wireless communication network . Save development time , Quickly upload device data to the server .

3.1.2 DTU type

There are two kinds of classification , One is the following according to the communication mode 7 class ;

  1. GPRS
  2. 4G
  3. NB-IoT
  4. LORA
  5. ZigBee
  6. WiFi
  7. satellite

The second is to classify according to the interface type .

  1. RS232
  2. RS485
  3. TTL

3.2 RS232

RS Express Recommend Standard( Recommended standards ), One of the serial port standards , The more commonly used . Industrial controlled RS-232 Mouth generally only uses RXD、TXD、GND Three lines . The interface form is DB9.

3.3 RS485

RS The above meaning .RS232 Interface , It can realize point-to-point communication , But it can not realize the networking function , Later RS485 That solved the problem .RS485 The master-slave communication mode is generally adopted in the communication network .RS485/MODBUS It is a popular way of network distribution , Easy to implement , Support RS485 There are many instruments .1

3.4 Modbus RTU agreement

3.4.1 Concept

Modbus RTU It's a compact , Binary representation of data . Subsequent orders / The data has a checksum with cyclic redundancy check (CRC16).
Modbus The agreement is a Master/Slave Architecture protocol . and TCP It's kind of similar .
A node is master node , Other uses Modbus The nodes of the protocol participating in the communication are slave node . every last slave Every device has a The only address .

3.4.2 An example of an order

request :01 03 00 00 00 02 C4 0B

byte function meaning
01 Device address The device address is 1
03 Function code At present, it is 03 Read request
00 00 Device address Register start address , from 00 00 Register starts reading
00 02 Read register length Read 2 Length , Corresponding reply 4 Bytes
C4 0B Effectiveness and effectiveness CRC16 Effectiveness and effectiveness

reply :01 03 04 00 0C 00 02 BB F1

byte function meaning
01 Device address The device address is 1
03 Function code At present, it is 03 Read reply
04 Data length At present, it is 03 Read reply
00 0C Device address register 1 The number
00 02 Read register length Read 2 Length , Corresponding reply 4 Bytes
BB F1 Effectiveness and effectiveness CRC16 Effectiveness and effectiveness

4 Looking forward to the goal

  1. Pass the data of the sensor device through 4G The network reports to the ECS ( Completed )
  2. Send commands to sensors through the network

5 Debugging process

5.1 build TCP The server

Install... On the server Socket Program , start-up TCP monitor .

5.2 To configure 4G DTU Set up TCP Server connection

  1. Set up DTU The way it works is Data transparent transmission ;
  2. Set the... Of the above servers IP And port values ;
  3. Format message , Including heartbeat packets and registration data ;
  4. Click Save parameter , Click restart device .( After configuring the device every time , Must be restarted to take effect )
  5. Check whether the data is received on the server .

If you receive data , Indicates that the network communication service is OK Of .

5.3 Equipped with temperature and humidity sensors

Use USB turn 485 The adapter is connected to the temperature sensor ,DC 12V The power supply supplies power to the sensor .
To avoid conflicts with other sensor addresses , Set the address value of the temperature sensor to 3, Then write to the sensor , Then read the data .
 Insert picture description here
You can see that the data collection is successful .

5.4 Set up Modbus Active polling parameters

 Insert picture description here
The function of this configuration is ,DTU A read command can be sent to the sensor 03 03 00 00 00 02 C5 E9, Corresponds to the address of the temperature sensor set in the previous step .DTU After receiving the reply data , The byte array data will be uploaded according to the set upload type 【json belt IMEI】 adopt 4G Network upload to step 5.1 Set up TCP Server . The format of the entire packet is as follows .

{
    "IMEI":863488052947903,"time":"2022-06-10 16:03:43","wenshidu":[1,15,3,42]}

5.5 Integration testing

Connect the above devices , Conduct integration test . There are a few lines , You can use a combiner .

6 result

Read temperature and humidity data ,4 Bytes , front 2 One represents the temperature , after 2 One represents humidity , take [1,27,3,6] give an example .
[1,27] = 0x011B = 0d283 , except 10 be equal to 28.3℃
[3,6] = 0x0306 = 0d777, except 10 be equal to 77.7 RH
 Insert picture description here


  1. RS485 Interface - Baidu Encyclopedia

原网站

版权声明
本文为[Seeking to fly]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110215590125.html