GusBus

Hi,

I have installed OpenPLC_V2 on a Raspberry Pi 3B. On prompt I selected the Modbus option.

I then uploaded your Hello World example, as well as a basic modbus config file with a single RTU device and got the UART working using /dev/ttyAMA0 (After fixing the UART issues associated with the addition of the BT module and Raspbian stretch). Verified by using a scope and looking at BCM14 (Pin 8 on the header) of the Pi.

We have added a small RS485 PCB driver that uses an SP3485 ( https://www.exar.com/ds/sp3485_100_061912.pdf )
The SP3485 is a half duplex RS485 Transceiver and use reverse logic Drive/Receiver enable pins (which we have tied together) to hold and release the RS485 Bus.

I have looked through the source files and hardware layers for solutions but I have two problems that I am unable to solve and any help/guidance would be greatly appreciated:


1) Modbus RTS signal
Seeing that the code manages to use the default UART by simply setting the name in the modbus config file, I first assumed it would default to using the RTS line on BCM17 (Pin 11 on the header). That was not the case. Just to make sure I went and checked every other GPIO on the PI and still no sign of an RTS signal.

I then started looking through the code to find references of RTS/CTS. This is what I found:

  • modbus-rtu.c looks for a #define called HAVE_DECL_TIOCM_RTS and HAVE_DECL_TIOCSRS485
    • The terminal printout from build.sh verifies that both are declared
    • All over the modbus-rtu.c the HAVE_DECL_TIOCM_RTS bit is checked and what looks like hardware pin toggling should be happening.
  • _modbus_rtu_ioctl_rts is then defined and called in the modbus_new_rtu function
    • Not sure in which header file TIOCM_RTS is defined though.
  • Then I can find the following functions and documentation for them, but they are not called anywhere from what I can see: modbus_rtu_get_rts; modbus_rtu_set_rts; 
  • From the documentation MODBUS_RTU_RS485/MODBUS_RTU_RS232 and MODBUS_RTU_RTS_NONE/MODBUS_RTU_RTS_UP/MODBUS_RTU_RTS_DOWN are the important bits and there is mention that it defaults to MODBUS_RTU_RS232 and MODBUS_RTU_RTS_NONE.
    • After looking I assumed this is referencing the declaration in modbus-rtu.c in the function modbus_new_rtu() (Lines 1255 and 1260)
    • I changed the lines to read 
      • ctx_rtu->serial_mode = MODBUS_RTU_RS485
      • ctx_rtu->rts = MODBUS_RTU_RTS_UP
      • I rebuilt and still nothing is happening

2) Modbus Sampling Rate
I see that the MODBUS requests come out at ~500ms. Where is this set in the code and is there an easy way to change this sampling rate? Because changing the cyclic interval of the TaskMain does not seem to affect this rate.



I have been struggling with this for a while now and am sure it must be something I am doing or misunderstanding since RTS and CTS are very standard for this kind of application as well as the option of changing sampling rates. Please can someone offer a helping hand in getting hardware handshaking going and varying the modbus sample timing.


Kind Regards,
Angus Steele
Mechatronic Engineer
CSIR, South Africa
Reply 0 0
thiagoralves
The modbus master transactions are handled by libmodbus (http://libmodbus.org/), an open source modbus library. OpenPLC code doesn't go low enough to control the actual RX/TX pins, neither the media where the information is going. It leaves this job for libmodbus to figure out. There might be some setting or configuration that may enable the RTS/CTS pins. Their google groups discussion might be a good place to ask about it: https://groups.google.com/forum/#!forum/libmodbus

About the sampling rate, it is defined by the sleeping call at the end of the void *exchangeData(void *arg) function (line 455). This function is the one responsible for querying all modbus devices and updating the internal buffers. By changing the value of the sleep, you can change how long it waits until another call is made.
Reply 0 0
GusBus
Thanks very much for the update! I will do a bit more of an investigation and post any updates on my progress.

Thanks again for the support and toolkit.
Reply 0 0
Reply