Mange
 
OK, I start a new thread about my hat, https://www.waveshare.com/wiki/RS485_CAN_HAT#RS485_Bus

RS485 CAN HAT


Im trying now to connect it to a simulated slave in this USB connector into a PC:
https://jmux.se/products/ch340-usb-till-rs485-konverter
CH340 USB till RS485 konverter

, using the PeakHMI MB serial slave simulator, https://www.hmisys.com/
With one exception, there dont seem to come any signals into the slave simulator. No registers are changed when I run Runtime in the PI.

When I start the Runtime in the Rasp, I get, as before without anything on the bus:

OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
Device SpeedControl 1002 is disconnected. Attempting to reconnect...
Connection failed on MB device SpeedControl 1002: No such file or directory
Persistent Storage is empty
Device SpeedControl 1002 is disconnected. Attempting to reconnect...
Connection failed on MB device SpeedControl 1002: No such file or directory
Device SpeedControl 1002 is disconnected. Attempting to reconnect...
.....
 
Is there something I set wrong in Runtime? What means "No such file or dir" ?
 
The (only one) slave look like this:
[203128%2F9160067%2Fmceclip0]
 
Setting in the slave emulator, the USB-converter shows up fine in Win11 on COMport 10, and is said to work in order:
[203128%2F9160070%2Fmceclip0]
 
Data in the slave emulator:
[203128%2F9160071%2Fmceclip0]
 
In fact, in the help file below its written that it can only have holding registers from 4000000 and up, is this in conflict w my current holding register adress 0 at %QW100?
 
This program simulates some of the register space of a slave device using
MODBUS addressing and protocol.
This program supports multiple serial connections.
Each running instance of the program needs a unique set of configuration files.
This is accomplished via a command line switch. If a command line switch is not
supplied the default path (OS dependent) is used.
The command line switch format is: -path <complete path>
Example: -path "C:\Honeywell\MODBUSRTU\Com 2"
Notes:
1) A space between the -path and the first character of the path.
2) Use the quotation mark (“) at the path start and end. Quotes are required if
the path has any space characters.
3) If the path does not exist the path will be created.
Registers
The following registers are supported.
000001 - 065535 Coil
100001 - 165535 Input
300001 - 365535 Input Register
400001 - 465535 Holding Register
Function Codes
The following function codes are supported.
Command Name
01 Read coil status
02 Read input status
03 Read holding register
04 Read input registers
05 Force single coil
 
Reply 0 0
thiagoralves
COM Port seems to be blank in your slave configuration. You must select a port for it to work
Reply 0 0
Mange
COM Port seems to be blank in your slave configuration. You must select a port for it to work
Ah, this is interesting! I am actuallly unable to choose a COM port when I create a slave in Runtime. I guessed that it is logic as there is only one serial port but if not, then there is something wrong in Runtime. If I click on the down pointing arrow, there are no alternatives coming up.
How do I set a COM port in Raspberry Pi?
Reply 0 0
thiagoralves
If there are no alternatives coming, then there are no serial ports available. The runtime uses pyserial to list available ports.
Reply 0 0
energieberg
 
Perhaps I'm getting you wrong, but if you want to comunicate with modbus rtu devices, you need something like this:
 
 
COM Port will be: /dev/ttyUSB0 (if only this device is used)
 
I'm using this device for modbus and it works on Raspberry Pi out of the box.
Reply 1 0
Mange
 
Perhaps I'm getting you wrong, but if you want to comunicate with modbus rtu devices, you need something like this:
 
 
COM Port will be: /dev/ttyUSB0 (if only this device is used)
 
I'm using this device for modbus and it works on Raspberry Pi out of the box.
I know, I got a similar one (but different brand) for the testing now and it can be the backup but I went for the HAT version before for some reason.
Im starting to think I might have done something with the serial ports, there are some commands around that in the documentation from Waveshare., Ill check it up.
Reply 0 0
Mange
Is this a problem here, only serial 0, not 1?
~ $ ls -l /dev/serial*
lrwxrwxrwx 1 root root 5 19 jan 22.17 /dev/serial0 -> ttyS0
 
Hmm:
~ $ python -m serial.tools.list_ports
no ports found
 
Isnt Pyserial installed?
Reply 0 0
thiagoralves
If pyserial wasn't installed, your command serial.tools.list_ports would fail. It is not failing, instead, it is saying there are no serial ports. This serial0 might be being used by console, so it's not available for applications. You need to change configs in your system (maybe through raspi-config) to make serial0 available for applications. As @energieberg said, a USB-485 adapter is a much simpler approach to this and is guaranteed to work.
Reply 0 0
fg71
  1. User need to be member of group dialout to access USB device. (ex. to use Arduino on Linux platform to upload a sketch to the board)
  2. Different Linux distribution use different devices, on Debian I have both ttyAMA0 and ttyAMA10 and the owner is root and group is dialout.
 
Running: -> .venv/bin/python -m serial.tools.list_ports
/dev/ttyAMA10
1 ports found
Reply 0 0
Mange
If pyserial wasn't installed, your command serial.tools.list_ports would fail. It is not failing, instead, it is saying there are no serial ports. This serial0 might be being used by console, so it's not available for applications. You need to change configs in your system (maybe through raspi-config) to make serial0 available for applications. As @energieberg said, a USB-485 adapter is a much simpler approach to this and is guaranteed to work.
Alright, I got it working now:
~ $ python -m serial.tools.list_ports
/dev/ttyAMA0
1 ports found
 
 it is neccesary to disable Bluetooth as it grabs the first serial port, in RaspberryPi config but also in /boot/firmware/config.txt :  
dtoverlay=disable-bt
 
and then I think also to add:  enable_uart=1
 
After reboot I can now select /dev/ttyAMA0 as COM-port in the slave definition of runtime, even in an existing slave. I can now see other messages rolling when the PLC-prgram is running so it looks promising! Hopefully I will now be able to see the register writings coming into my slave simulator.
Reply 0 0
Mange
Well, it looks different, but something Runtime dont like still.
This is the log at startup:
 
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
Device SpeedControl 1002 is disconnected. Attempting to reconnect...
Connected to MB device SpeedControl 1002
Persistent Storage is empty
Modbus Read Input Registers failed on MB device SpeedControl 1002: Too many data
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Issued start_dnp3() command to start on port: 20000
DNP3 ID manager: Starting thread (0)
DNP3 ID DNP3_Server: Listening on: 0.0.0.0:20000
Issued start_enip() command to start on port: 44818
Server: Listening on port 44818
Server: waiting for new client...
Issued start_pstorage() command with polling rate of 10 seconds
Starting Persistent Storage thread
Modbus Write Holding Registers failed on MB device SpeedControl 1002: Connection timed out
Modbus Read Input Registers failed on MB device SpeedControl 1002: Too many data
Modbus Write Holding Registers failed on MB device SpeedControl 1002: Connection timed out
 
 
What means Too many data Issued  ?
 
 
 
 
Reply 0 0
thiagoralves
Most likely this hat requires manual control of the RS485 EN pin (flow control), which OpenPLC doesn't do. That's why the USB adapter is usually a better choice.
Reply 0 0
Mange
Most likely this hat requires manual control of the RS485 EN pin (flow control), which OpenPLC doesn't do. That's why the USB adapter is usually a better choice.
Its said it dont.
RSE4Tx/Rx Control
For the RSE pin, you can choose not to use it, the module factory defaults to use the hardware automatic receive and transmit.
 
 
(Or does that mean I must disable flow control by PIN4?)
 
Here maybe?:
The default factory setting of this module is to use hardware automatic sending and receiving, or you can choose to control the pins on the software to choose to send and receive, you can choose the control method by soldering the 0-ohm resistor on the board.
Hardware automatic control:
RS485 CAN HAT (B)07.png
Data Rx: When P_TX is high, it's in an idle state. At this point, the transistor conducts, and the RE pin of the SP3485 chip is at a low level, enabling data reception. RO begins to receive data and forwards the data received at the 485AB port to the MCU.
Data Tx: When P_TX is pulled low, indicating the start of data transmission, the transistor is cut off, and the DE pin is set to a high level, enabling data transmission. At this point, if the data being transmitted is '1', the transistor will be conducting. Although reception becomes valid, the chip remains in a high-impedance state during the transmission phase, so it continues to maintain the transmission state, allowing for the normal transmission of '1'.
Note: the use of an automatic transceiver due to the on-off speed of the transistor, will lead to the automatic transceiver baud rate that can not be too high, if you need a very high baud rate, it is recommended to use the manual transceiver.
But Im confused by this they say:
 
For Raspberry Pi 5/2B/zero and some Raspberry Pi that have been changed in configuration, the user's serial device number may be ttyAMA0; you can use the following command line to confirm, serial0 for the selection of the serial device number, as follows:
ls -l /dev/serial*
RM520n-gl faq90.png
And then reboot the Raspberry Pi:
 
 
I get only
$ ls -l /dev/serial*
lrwxrwxrwx 1 root root 7 22 jan 23.37 /dev/serial0 -> ttyAMA0
 
I have disabled BT so I was expecting to get both, or at least serial0 on ttyS0
Read somewhere that its better for many application to use /serial0 rather than ttyS0 but this is gibberich to me.
Or it simply says that in fact, a symlink from serial0 to ttyAMA0 in my case?
This too much data, it cant have something to do with Baudrate or similar?
 
 
 
This part I also find confusing, I think I have these all, why they cant be used...?

Install Library

If you use bookworm system, only the lgpio library is available, bcm2835 and wiringPi libarary cannot be installed or used. Please note that the python library does not need to install, you can directly run the demo.

BCM2835

#Open the Raspberry Pi terminal and run the following commandwget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gztar zxvf bcm2835-1.71.tar.gz cd bcm2835-1.71/sudo ./configure && sudo make && sudo make check && sudo make install# For more, you can refer to the official website at: http://www.airspayce.com/mikem/bcm2835/

WiringPi

#Open the Raspberry Pi terminal and run the following commandcdsudo apt-get install wiringpi#For Raspberry Pi systems after May 2019 (earlier than that can be executed without), an upgrade may be required:wget https://project-downloads.drogon.net/wiringpi-latest.debsudo dpkg -i wiringpi-latest.debgpio -v# Run gpio -v and version 2.52 will appear, if it doesn't it means there was an installation error# Bullseye branch system using the following command:git clone https://github.com/WiringPi/WiringPicd WiringPi. /buildgpio -v# Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error

lgpio

wget https://github.com/joan2937/lg/archive/master.zipunzip master.zipcd lg-mastersudo make install #for more details, you can refer to https://github.com/gpiozero/lg

Python3

sudo apt-get updatesudo apt-get install python3-serialsudo apt-get install python3-can
 
Hmm, Waveshare have an USB stick too that seems to be based on the same chip, https://www.waveshare.com/wiki/USB_TO_RS485_(B)
 
USB-TO-RS485-1.jpg
 
Here they write similar I think about flow control:

CDC Driver

The default driver of the computer is the CDC driver, which can be viewed through the device manager.
USB TO RS485 (B) 01.jpg
The software flow control is set by default. If you need to use CDC hardware flow, you need to connect the CFG of the board to EN (power-on self-test).
 
Would it mean that also their USB-to RS485 adapter would not work without flow control?
Reply 0 0
energieberg
Hello @Mange,
 
the USB stick uses the FT232RL chip, AFAIK it's a different chip as on the CAN hat.
 
AFAIK you need for modbus communication three contacts: A, B, G. On the CAN hat there's only A and B. Why do you go the hard way? The developer (@thiagoralves) of OpenPLC say: "Get the USB stick". This USB stick works out of the box. So why you don't buy the stick?
Reply 1 0
thiagoralves
OpenPLC does not manually control the flow. Flow control on the USB RS485 is done through the USB CDC driver, and therefore does not need an extra hardware pin to handle the signal. That’s why it works with OpenPLC out of the box. If this HAT says that it has automatic flow control, try to enable that and check if it works. I personally think it will be harder to make it work as it is tied directly to the Pi UART pins, instead of being abstracted by the USB CDC layer. If it doesn’t work and you still want to use this HAT, then OpenPLC Modbus master code would have to be changed to take in consideration a flow control pin.
Reply 1 0
Reply