Modbus Tcp/IP communication Rpi(Rexygen)<->PC(Matlab)
-
Hi,
I have a question related to example
\0402_Modbus_TCP\0402-04_Modbus_TCP_PID_Loop
I want to communicate between Rpi and PC, while Rpi will represent the master and PC will serve as the slave.
If I run your example as it is, everything works just fine. THe master part goes to Rpi and slave part to PC, both using REXYGEN.
However, I need to create some testing utility that would use the master part as it is, but the client would be implemented in some other environment than REXYGEN. I downloaded master part to Rpi and then used the following syntax in Matlab:
m = modbus('tcpip', '192.168.111.2', 1,502)
I double checked all IP addresses and ports. I get the below mentioned error. It's the same if I use Python or some external dedicated utility to analyze modbus communication.
Does the slave part in your example do anything special? What else should I check to be able to establish the connection?
Thanks, Stepanm = modbus('tcpip', '192.168.111.2', 1,502)
Error using modbus (line 96)
Requested transport resource is either in use or does not exist. Cannot create a communication link with the remote server. Please check the input arguments(ADDRESS and PORT) and make sure the server is running. Additional Information: Pokus o připojení selhal, protože připojená strana v časovém intervalu řádně neodpověděla, nebo vytvořené připojení selhalo, protože neodpověděl připojený hostitel. -
Hi Stepan,
I believe there might be a misunderstanding in the way the connection is being set up. It seems there might be a confusion between the roles of Master and Slave in Modbus TCP communication.
In Modbus communication, the Slave holds the Modbus table, meaning the Master should connect to the Slave. Therefore, the correct approach should be:
- Upload the REXYGEN Slave to your Raspberry Pi.
- Try to connect to the Raspberry Pi from Matlab using the following command, where Matlab acts as the Modbus Master:
m = modbus('tcpip', '192.168.111.2', 'Port', 502);
This way, you are ensuring that the Master (Matlab) is correctly connecting to the Slave (Raspberry Pi with REXYGEN).
Please let us know if you encounter any further issues.
Best regards,
Jan -
@Jan-Reitinger Thanks for your assistance. Having followed your advice
m = modbus('tcpip', '192.168.111.2',502)
I get the following result:
m =Modbus TCPIP with properties:
DeviceAddress: '192.168.111.2'
Port: 502
Status: 'Connected'
I will now focus on how to read and write the data, because so far I have no success after playing with this for two hours.It seems that my bad was using RPi as master instead of slave.
However, there are two things that made me confused and did not ring a bell in order to make me try to switch the roles:
-documentation for the modbus command does not explicitely declares if it creates master or slave, it says it "constructs a Modbus object"
-moreover, the documentation of the modbus function declares that when using
m = modbus(Transport,DeviceAddress)
"DeviceAddress is the IP address or host name of the Modbus server"If it's not a mistake, it's very confusing then.
-
@Jan-Reitinger UPDATE:
Now I'm able to read and write uint16 numbers, and I will focus on doubles.
I still have some doubts about this solution, for example where the modbus server is actually running or by which machine is the modbus server created (Matlab's modbus command seems to connect somee server and not to create any, and RPi hosts the slave part). -
@stepan-ozana
That's good news. I believe your confusion arises from different terminology. The Master/Slave terminology is used in the original context, which we are still adhering to for now. However, the Server/Client terminology is more commonly used nowadays. In the earlier terminology, the client was referred to as master and the server as slave. -
@Jan-Reitinger I share my solution if anyone is interested. It shows writing and reading uint16 and double values over the Modbus TcpIP between some REXYGEN target hosting the Modbus server on the client (slave) side and Matlab running on PC.
example_ModbusTcpIP_Comm_RpiToPC.zip -
@stepan-ozana Thanks for sharing your solution! Unfortunately, I don't have the required toolboxes to run the script on my computer. However, the code is well-documented and well-organized, which should make it easy for anyone with the necessary Matlab toolboxes to use it without any issues.
'modbus' requires one of the following: Industrial Communication Toolbox MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms Error in example_ModbusTcpIP_Comm_RpiToPC (line 9) m = modbus('tcpip', IP_ADDR,MODBUS_PORT) %establish communication with the server (client)
Cheers,
Jan