基于python的4G DTU设备温度数据的仿真并传输

塔石4G DTU设备温度数据的仿真传输

  • 数据编码模块
    • OneNet平台设备开发文档
    • 数据编码代码如下:
  • 串口模块
  • 源代码

数据编码模块

OneNet平台设备开发文档

基于python的4G DTU设备温度数据的仿真并传输_第1张图片
开发文档链接:ONeNet设备开发文档

数据编码代码如下:

class Converter(object):
    def to_hex(data):
        # onenet平台 .Json数据类型
        byte_list = '\x03'
        # 获取ANSI编码及长度
        list = data.encode('mbcs')
        len_list = len([format(i, 'X') for i in list])
        #塔石4G DTU编码格式
        byte_list += chr(0) + chr(len_list)
        for i in list:
            byte_list += chr(i)
        byte_list += '\r\n'
        # 串口使用比特流传输
        s_byte_list = bytes(byte_list, encoding='UTF-8')
        return s_byte_list

串口模块

class mSerial:
    def __init__(self, com, bps, timeout):
        self.port = com
        self.bps = bps
        self.timeout = timeout
        global Ret
        try:
            # 打开串口,得到串口对象
            self.main_serial = serial.Serial(self.port, self.bps, timeout=self.timeout)
            # 判断是否打开成功
            if (self.main_serial.is_open):
                Ret = True
        except Exception as error:
            print("---Error---:", error)
    # 打开串口
    def Open_serial(self):
        self.main_serial.open()
    # 发送数据
    def Send_data(self, data):
        self.main_serial.write(data)
    # 关闭串口
    def Close_serial(self):
        self.main_serial.close()
        print(self.main_serial.is_open)  # 检验串口是否打开

源代码

# -*- coding: utf-8 -*-
"""
@author: 浅笑醉红楼.([email protected])
"""
import serial
import serial.tools.list_ports
import time
import random

class Converter(object):
    def to_hex(data):
        # 数据类型
        byte_list = '\x03'
        # 获取ANSI编码及长度
        list = data.encode('mbcs')
        len_list = len([format(i, 'X') for i in list])
        #塔石4G DTU编码格式
        byte_list += chr(0) + chr(len_list)
        for i in list:
            byte_list += chr(i)
        byte_list += '\r\n'
        # 串口使用比特流传输
        s_byte_list = bytes(byte_list, encoding='UTF-8')
        return s_byte_list

class mSerial:
    def __init__(self, com, bps, timeout):
        self.port = com
        self.bps = bps
        self.timeout = timeout
        global Ret
        try:
            # 打开串口,得到串口对象
            self.main_serial = serial.Serial(self.port, self.bps, timeout=self.timeout)
            # 判断是否打开成功
            if (self.main_serial.is_open):
                Ret = True
        except Exception as error:
            print("---Error---:", error)
    # 打开串口
    def Open_serial(self):
        self.main_serial.open()
    # 发送数据
    def Send_data(self, data):
        self.main_serial.write(data)
    # 关闭串口
    def Close_serial(self):
        self.main_serial.close()
        print(self.main_serial.is_open)  # 检验串口是否打开

if __name__ == '__main__':
    count=1
    myserial=mSerial('COM5',9600,0.5)
    if (Ret):
        while (1):
            Chang = random.uniform(-5, 15)
            temperature = 20 + Chang
            temperature = round(temperature, 2)
            data = {"Temperature": temperature}
            a = Converter.to_hex(str(data))
            myserial.Send_data(a)
            time.sleep(3)
            print('数据序号:',count)
            print('温度值:',temperature)
            count+=1

你可能感兴趣的:(物联网,python,开发语言,iot,物联网)