Wiki

Clone wiki

PyRTEdoc / Modbus master

Thanks to the beloved Python community, lets' see how straight forward to convert FX30 or alike devices into a Modbus master.

Prepare Modbus slave device

If you don't have a Modbus slave device such as a RTU, you can put on a simulator on your PC such as PyModSlave. Just make sure the serial settings are 19200, 8N1 which will be the default settings next on FX30 side. Connect PC to FX30's serial port. I use a USB to serial adapter as I don't have a serial port on my laptop.

Set up Modbus master on FX30 side

ssh in FX30, create myapp directory if not yet, and in myapp, install great modbus driver from Jonas Berg. You could scp the file from PC to FX30 in some firmware versions. And as I have SIM card, APN set up on the FX30, so I just use its LTE internet connection and download the driver file directly to FX30.

root@fx30s:~/myapp# curl -O https://raw.githubusercontent.com/pyhys/minimalmodbus/master/minimalmodbus.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  128k  100  128k    0     0   4491      0  0:00:29  0:00:29 --:--:-- 12115
root@fx30s:~/myapp# ls -l minimalmodbus.py
-rw-r--r--    1 root     root        131588 Jul 22 00:24 minimalmodbus.py

Assume the Modbus slave side on PC has been set up, now give it a go and read some registers via Modbus,

root@fx30s:~/myapp# python3
Python 3.5.2 (default, Oct  2 2020, 02:54:47)
[GCC 6.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import minimalmodbus
>>> instr = minimalmodbus.Instrument('/dev/ttyHS0', 1)
>>> instr.read_register(3, 1)
0.0
>>> instr.read_register(3, 1)
6279.3999999999996
>>> instr.read_register(3, 1)
5676.6000000000004

Great, now you have made a Modbus master with cellular connectivity(hopefully in minutes) - you may be interested in applying your code with the global certified FX30 to a real project. Further readings,

Updated