I have a MOXA Modbus TCP module (M-4210 in combination with the NA-4010 networking module that also has some other modules attached) that works as a 2-channel analog output, each with voltages from 0 to 10 Volts.
In my C# application I need to get the current values of these outputs, which is not as easy as I'm quite new to the whole Modbus thing.
In my code I already have a working modbus tcp client that does its job, I tested it by reading and writing single coils of another digital output module. The analog output module however seems to work with registers instead of coils.
To start from the beginning, these are the modbus settings for the two channels within this module (taken from the MOXA ioAdmin Tool):
and the addresses:
And here's another screenshot from the web interface:
So I tried to read the values like this:
ModbusClient c = new ModbusClient();
c.Connect("172.17.6.15", 502);
int[] r = c.ReadHoldingRegisters(2048, 1);
for (int i = 0; i < r.Length; i++)
{
textBox1.Text += r[i].ToString() + System.Environment.NewLine;
}
This code returns one value, it changed as follows:
When channel #0 is set to the (raw) value of 1139
, the returned int value is 29440
When channel #0 is set to 1140
, the returned value is 29696
I seem to be on the right track, but I don't quite understand the offsets and how to separate the channels when the value comes back. It would be great if someone could shed some light on this, thanks in advance!
Is your client handling Modbus endianess correctly? Modbus is big endian.
1140 is 0x474, 29696 is 0x7400. 1139 is 0x473, 29440 is 0x7300. I can see a pattern. It seems that your Modbus client is setting the LSB to 0 and taking the MSB by shifting the received LSB to the left.
Try changing the channel's value to 1141, you'll probably read 29952 in your client. That will confirm my suspicion.