1
votes

Problems communicating with Modbus devices via USB to rs485.

so I am using some python 3.5 and am using the PyModbus, PySerial

and my code is based on the PyModbus serial example and under windows it works fine and communicates to the Modbus device correctly

when I run my code on the Raspberry Pi3 running Ubuntu mate 16.04.5 LTS, the device is not responding from my code. I first thought the adapter was not installed correctly but after double checking everything, I installed gtkterm and configured the port to /dev/ttyUSB0 9600 8-n-2 and tried sending the hex data to the Modbus and still no reply I then noticed under flow control it has an RS485-HalfDuplex(RTS) setting and under the advanced options if I set the send delay to 20 milliseconds and 10 ms RTS off

now when I try and send the hex modus packet the device responds as expected.

I connected my Oscilloscope and from the python code, it is transmitting some small packet of data randomly looking more like after a timeout when the data is sent. after digging around a bit it seems that some USB to rs485 devices need the RTS to toggle the device to transmit mode and then RTS off to put it back into receive mode.

I also discovered that pySerial has an RS485 setting and have tried these and then the packets being transmitted was more regular and larger before but still much shorter than the gtkterm, and much messing around with the timing helped a bit, I have now gone back to using the original code but putting in

socket.setRTS(1)  
time.sleep(0.02)
socket.write(request)
time,sleep(0.1)
socket.setRTS(0)

seems to give me the longest packet picked up on the oscilloscope but the packet is 8.42ms long as where the packet from gtkterm is 9.36ms and taking a glance between the trace of both messages it seems as if the end of the packet is missing. but even if I increase the sleep time before turning off the RTS signal it makes no difference.

1

1 Answers

0
votes

ok after installing interceptty and getting gtkterm to use the virtual port, it chucked up a tone of error messages because interceptty do not support RTS and DTR, but for some reason it was able to still communicate to the modbus device,

I then pointed my python code at the interceptty virtual port and I was transmitting same information as gtkterm but no response but I noticed I wrote the data much faster

so I then changed the code to only send 1 byte at a time and a 10ms delay between them and then it suddenly started working.

so with the sample code from pyModbus https://pymodbus.readthedocs.io/en/latest/ I edited client/sync.py

added 2 extra imports

import time
import struct

and changed the _send function of ModbusSerialClient slightly

from

size = self.socket.write(request)

to

        size=0
        for item in request:
            size = self.socket.write(struct.pack(">B",item))
            time.sleep(0.01)
        size=len(request)