Tashi Lora181 modbus reads the vialon register

modbus reads the vialon register

problem

Weiluntong MT6103IP touch screen, no network port, one RS485 connected to Siemens 200PLC, spare RS232
The equipment is the waste gas adsorption treatment system, which is placed on the platform on the second floor in the west of the workshop. People go to record the data for one day every day.

I want to transfer the recorded value to the office in the east of the workshop, which spans more than 100 meters in the middle of the workshop. I want to achieve it at a low cost.
Pass 9 values.

Problems to be solved

  1. MT6103IP does not support serial port transparent transmission, and the address mapping in the help can not be used.
    Then make your own mapping, use macro timing execution, and read the register of s7-200 to the local register of the touch screen.
macro_command main()

int a[9]={0}
short b[9]={0}
GetData(a[0], "SIEMENS S7-200", VD, 700, 9)

LOWORD(a[0], b[0])
LOWORD(a[1], b[1])
LOWORD(a[2], b[2])
LOWORD(a[3], b[3])
LOWORD(a[4], b[4])
LOWORD(a[5], b[5])
LOWORD(a[6], b[6])
LOWORD(a[7], b[7])
LOWORD(a[8], b[8])

SetData(b[0], "Local HMI", LW, 20, 9)

end macro_command

It should be noted that the data read from 200PLC is double word and LW is single word. A low byte should be read. The temperature should not be greater than 1000 and the data will not be lost

  1. network connections
    The workshop is 20 meters high, and the labor cost of network cable is high; Take the wireless AP to shoot.
    If you want to use Lora, you don't need to care more about visualization, and Lora is already very cheap.
    Tashi 181 also supports modbus active polling, but the firmware upgrade needs to be sent back to the original factory for operation, which is inconvenient.

For lora networking, restore the original settings. The settings of nodes at both ends can be the same as those selected in the box. The default rate is 2400, and 18 bytes of data is enough.

Module working mode, select transparent.

modbus communication with python

Using threads, poll once every 30 seconds.

#!/usr/bin/env python
# coding: utf-8

from pymodbus.client.sync import ModbusSerialClient
from pymodbus.exceptions import ModbusIOException,InvalidMessageReceivedException
from datetime import datetime
import time
import threading
import taos

class MyData(threading.Thread): 

    def __init__(self): 
        super().__init__() 
        self._stop = threading.Event() 
        # Serial port
        self._client=ModbusSerialClient(method='rtu',port="com7", 
                          stopbits = 1, bytesize = 8, parity = 'E' ,
                          baudrate= 9600)
        # tbos
        self._conn = taos.connect(host="cmms", user="root", password="taosdata", config="/etc/taos")
        self._c1 = self._conn.cursor()
        self._c1.execute('use ac')

    def stop(self): 
        self._stop.set()
    def close_serial():
        _client.close()

    def stopped(self): 
        return self._stop.isSet() 
    
    def read(self,unit,fa):
        try:
            # unit is the station number and address
            rr = self._client.read_holding_registers(20, 9, unit=unit) 
            data=rr.registers.copy()
            data.append(fa)
            dt=datetime.now()
            sql='%d,%d,%d,%d,%d,%d,%d,%d,%d,%d' %tuple(data)
            sql = '\'%s\',%s' % (dt, sql)
            print('data:' + sql + '\n')
            self._c1.execute('insert into t values(%s) ' % sql)
        except AttributeError:
            pass
        except ModbusIOException:
            print('Distal%d´╝îNo reaction' % unit)
        except InvalidMessageReceivedException:
            print('Distal%d´╝îInformation error' % unit)


    def run(self): 
        while True: 
            if self.stopped(): 
                return
            self.read(0x01, 2)
            # sleep
            time.sleep(30)


# In[47]:

if __name__ == '__main__':
    a=MyData()
    a.start()

The code is tested in Jupyter Notebook and then downloaded as py.

Storage and reality

TDengine is selected as the database. Download ubuntu from github and ubuntu runs in PVE virtual machine.
The data show that Grafana is used.
Note that the time zone of ubuntu should be Beijing. Select this for the time zone in Grafana settings.
If the time is not correct, the curve will not appear. If you place the cursor on the panel, you can see that the time is NAN.

query of data, such as select ts, a from ac.t where factory = 2
ac.t represents the table t in the database ac.

achievement

The original touch screen investment can be retained without replacing the advanced screen such as MT8071IE.
If the existing computer hardware is used, only three Lora s need to be configured for Ad Hoc Networking (a total of two data points are uploaded and one is connected to the computer).
The hardware cost was 300 yuan, and the test and program took 3 working days.

If grafana suddenly has no data, check whether the data value and format of the database have changed.
Yesterday's error was that the factory classification value was filled in with 1, but the query was filtered by 2. As a result, grafana could not get new data.

Tags: Python

Posted on Sun, 24 Oct 2021 23:12:16 -0400 by big-dog1965