0_Ayano
I want to get the value of the LED of the Arduino running in the following PLC program using Modbus RTU (Serial),
but I can't get it because I don't know the "register address" and "Modbus function code".
If anyone knows how to solve this, please let me know.
 
スクリーンショット (97).png
スクリーンショット (98).pngスクリーンショット (99).png
 
The Python program used is as follows.
import minimalmodbus

# Set the serial port and Modbus address of the microcontroller
instrument = minimalmodbus.Instrument('COM3', 1) 
instrument.serial.baudrate = 115200  # Set the baud rate
instrument.serial.bytesize = 8      # Set the data bits to 8
instrument.serial.parity = minimalmodbus.serial.PARITY_NONE  # Set parity
instrument.serial.stopbits = 1      # Set the stop bits
instrument.mode = minimalmodbus.MODE_RTU  # Set Modbus RTU mode

try:
    read_value = instrument.read_register(__adress??__, functioncode=__function???__, signed=True)
    print(read_value)
except Exception as e:
    print("Error:", str(e))

instrument.serial.close()
 
Reply 0 0
thiagoralves
Reply 0 0
0_Ayano
I ran the python you taught me, but I can't communicate. I don't know why communication is not possible, so if there is an error in the program, please let me know.

I tried the code below.
- For the address, modbus slave ID (1) was used.
- The number of coils was only blink_led, so I set it to 1.
>>> from pymodbus.client import ModbusSerialClient as ModbusClient
>>> client = ModbusClient(method = "rtu", port="COM3", baudrate= 115200, timeout=0.1)
>>> client.connect()
True
>>> client.read_coils(1,1)
ModbusIOException('[Input/Output] Modbus Error: [Invalid Message] No response received, expected at least 4 bytes (0 received)')
 
Reply 0 0
thiagoralves
QX0.3 is coil address 3, not 1
Reply 0 0
thiagoralves
Also, you might want to increase that timeout a little bit
Reply 0 0
0_Ayano
Thank you for letting me know.
 
Communication is not possible even after changing the function code.
I executed adress and function code with all patterns from 0 to 9, but the same error occurred.
I couldn't find a solution even after looking on the web and YouTube...
 
The microcontroller I'm using is Arduino UNO, could that be the cause?
>>> from pymodbus.client import ModbusSerialClient as ModbusClient
>>> client = ModbusClient(method = "rtu", port="COM3", baudrate= 115200, timeout=2)
>>> client.connect()
True
>>>
>>> rr = client.read_coils(1, 3)
>>> rr.bits
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ModbusIOException' object has no attribute 'bits'
 
Reply 0 0
0_Ayano
The lamp blinked when serial communication was established.
Therefore, I believe that serial communication is possible and empty data is being returned.
Reply 0 0
thiagoralves
According to PyModbus documentation you should do 
rr.isError()
to validate that there isn't any error and then 
rr.bits[0]
Reply 0 0
0_Ayano
I checked for errors using isError(), but it seems that there are still errors.
The error message was "ModbusIOException('[Input/Output] No Response received from the remote slave/Unable to decode response')".
Was there anyone else who had similar problems communicating?
If you have ever encountered someone who was unable to communicate in the same way, how did that person solve this problem?
Reply 0 0
0_Ayano
In python's minimalmodbus, numerical data was returned,
so I guess that the decoding of the data returned from OpenPLC failed.
Reply 0 0
thiagoralves
It is possible that Modbus is not really working on your board, or that you’re not using the right COM port or something else. Have you tried using a Modbus program to read from the board like Modbus Poll or Radzio?
Reply 0 0
0_Ayano
When I verified whether Modbus communication was possible using Modbus Radzio, Modbus communication was possible.
I also checked the COM port in Device Manager and found that it was using the correct COM port.
Reply 0 0
thiagoralves
It seems you forgot to set the slave id on your Python code
Reply 0 0
Reply