boddmg / nmodbus

Automatically exported from code.google.com/p/nmodbus
1 stars 0 forks source link

The function code received in the query is not an allowable action for the server (or slave) #81

Closed kaikai0115 closed 4 years ago

kaikai0115 commented 4 years ago

Hi, I'm trying to connect my PC to a PLC with modbud protocol, using RTU.

here is snippet of my code: private void WriteMultipleRegisters_Click(object sender, EventArgs e) {

try
{
IModbusMaster masterRTU = ModbusSerialMaster.CreateRtu(serialPort1);
byte slaveAddress = 1;
ushort startAddress = 40123;
string[] strArray = textBox1.Text.Split(',');
float[] floatArray = new float[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
floatArray[i] = float.Parse(strArray[i]);
}
List byteList = new List();
foreach (var item in floatArray)
{
byte[] byteArray = BitConverter.GetBytes(item);
Array.Reverse(byteArray, 0, byteArray.Length);
byteList.AddRange(byteArray);
}
ushort[] data = ModbusUtility.NetworkBytesToHostUInt16(byteList.ToArray());
masterRTU.WriteMultipleRegisters(slaveAddress, startAddress, data);//write data to slave;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}

When I push a button, an exception is generated :

'Modbus.SlaveException'. Function Code: 144 Exception Code: 1 - The function code received in the query is not an allowable action for the server (or slave). This may be because the function code is only applicable to newer devices, and was not implemented in the unit selected. It could also indicate that the server (or slave) is in the wrong state to process a request of this type, for example because it is unconfigured and is being asked to return register values.

Probably I miss something, but I can't figure out what. Any advice is really appreciated.

Thank you