I have connected a PC to a Bachmann M1 Controller using Modbus tcp/ip connection in MATLAB. Now I want to read/write variables from/to a Simulink model. So I have created variables in Simulink using SVI-SW blocks (provided by Bachmann M-target), and mapped those variables to holdingsregisters and given them indexes of 0 and 200.
The following images are the configurations in the Bachmann's software called Solution Center:
And the same variables in Simulink (the yellow S blocks having the name 'u' and 'y':
After I run the Simulink model in Bachmann M1 controller using external mode, I can connect the PC to the controller from MATLAB command window:
>> m = modbus('tcpip', '192.168.X.XXX')
m =
Modbus TCPIP with properties:
DeviceAddress: '192.168.X.XXX'
Port: 502
Status: 'Connected'
NumRetries: 1
Timeout: 10 (seconds)
ByteOrder: 'big-endian'
WordOrder: 'big-endian'
Now when I want to read/write the registers using the corresponding indexes, it does not work. Writing generates the following error:
>> write(m, 'holdingregs', 1, 1)
The Modbus server returned an invalid address error. Address + number of values to write is out of range.
The index of the variable u is 0 (which is 1 in MATLAB) and u is an array of 10 elements. So I can't understand why the address is out of range.
As for the reading, I can read the corresponding indexes (1 and 201), but the result is not correct:
>> b = read(m, 'holdingregs', 201, 2, 'double')
b =
1.0e-319 *
0.8079 0.8079
The scope that is connected to that variable shows its value as 0.5, which is correct as I have configured the initial value of a delay block that way. But read function displays a negligibly small value, almost 0.
Does anyone know what I am doing wrong?
Thanks :)
Edit: Now with the same simulation, I get an error while trying to read as well:
>> b = read(m, 'holdingregs', 1, 1, 'double')
The Modbus server returned a read failure error.
What can I use to get more info about this error?