1

I am trying to use modbus with RS485 on my Pi4. I'm using modbus_tk with pySerial library, and I managed to get controllig RTS pin while transmitting data, but I cant get it running faster - when TX ends the pins stays UP for aprox. 30ms (25 +- 35) after TX.

,

My code below:

socket = serial.rs485.RS485(port=PORT, baudrate=460800, bytesize=8, parity='N', stopbits=1, rtscts=True, dsrdtr=False, timeout=0.01, xonxoff=0 )
socket.rs485_mode = serial.rs485.RS485Settings(rts_level_for_tx=True, rts_level_for_rx=False, loopback=False, delay_before_tx=0, delay_before_rx=0)

master = modbus_rtu.RtuMaster(socket)

master.set_timeout(2.0)
master.set_verbose(True)
def readAll():
    try:
        data = master.execute(1, cst.READ_HOLDING_REGISTERS, 999, 55)
        os.system('clear')
        print(datetime.now())
        print("IsolInputs: ", data[0:4])
        print("ADCInputs: ", data[4:21])
        print("ADCInputs_raw: ", data[21:37])
        print("RelayOtuputs: ", data[37:43])
        print("PWMFrequency: ", data[43])
        print("PWMDutyCycle: ", data[44:54])
        print("DevStatus: ", data[54])

    except modbus_tk.modbus.ModbusError as exc:
        logger.error("%s- Code=%d", exc, exc.get_exception_code())

I tried to manually control another GPIO Pin via hooks in modbus_tk lib, but it gives me the simmilar result:

def on_before_write(args):
    GPIO.output(EN_485, GPIO.HIGH)
hooks.install_hook("modbus.Master.before_send", on_before_write)

def on_after_write(args):
    GPIO.output(EN_485,  GPIO.LOW)
hooks.install_hook("modbus.Master.after_send", on_after_write)

Any ideas?

Thanks!

6
  • Can you explain why this is a problem? RTS means the host (in this case: the Pi) is ready to send data. In theory, this could stay high forever.
    – PMF
    Apr 15, 2020 at 7:14
  • @PMF i Wanat i to control rs485 hałf duplex, and if it’s high for too long my slaves need to wait with response - this makes my program running too long.
    – SURViR
    Apr 15, 2020 at 7:52
  • I see... So for your case the timeout for resetting RTS is to long. One possibility would be to use an NE555 and manually derive a (short) timeout from the data line.
    – PMF
    Apr 15, 2020 at 7:58
  • I noticed that if i manyally send only one message it works perfectly, but when I do it in loop it makes problem like that.
    – SURViR
    Apr 15, 2020 at 9:36
  • Would adding ser.setRTS(False) as soon as possible in your script help?
    – user115418
    Apr 15, 2020 at 21:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.