void listener_ServeCommand(object sender, ServeCommandEventArgs e) { var command = (ModbusCommand)e.Data.UserData; Thread.Sleep(SlaveDelay); //take the proper function command handler switch (command.FunctionCode) { case ModbusCommand.FuncReadCoils: case ModbusCommand.FuncReadInputDiscretes: case ModbusCommand.FuncReadInputRegisters: case ModbusCommand.FuncReadMultipleRegisters: case ModbusCommand.FuncReadCustom: DoRead(command); break; case ModbusCommand.FuncWriteCoil: case ModbusCommand.FuncForceMultipleCoils: case ModbusCommand.FuncWriteMultipleRegisters: case ModbusCommand.FuncWriteSingleRegister: DoWrite(command); break; default: AppendLog(String.Format("Illegal Function, expecting function code {0}.", command.FunctionCode)); //return an exception command.ExceptionCode = ModbusCommand.ErrorIllegalFunction; break; } }
/// <summary> /// Host server for the incoming request from a remote master /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ListenerServeCommand(object sender, ServeCommandEventArgs e) { var command = (ModbusCommand)e.Data.UserData; //take the proper function command handler switch (command.FunctionCode) { case ModbusCommand.FuncReadInputDiscretes: for (int i = 0; i < command.Count; i++) { InputPort input = HardwareModel.Instance.Discretes[i + command.Offset]; if (input != null) { command.Data[i] = (ushort)(input.Read() ? 1 : 0); } } break; case ModbusCommand.FuncWriteCoil: bool state = command.Data[0] != 0; HardwareModel.Instance.Coils[command.Offset].Write(state); break; case ModbusCommand.FuncForceMultipleCoils: for (int i = 0; i < command.Count; i++) { HardwareModel.Instance.Coils[i + command.Offset].Write(command.Data[i] != 0); } break; case ModbusCommand.FuncReadInputRegisters: for (int i = 0; i < command.Count; i++) { var analog = HardwareModel.Instance.Analogs[i + command.Offset]; command.Data[i] = (ushort)analog.Read(); } break; case ModbusCommand.FuncReadCoils: case ModbusCommand.FuncReadMultipleRegisters: case ModbusCommand.FuncWriteMultipleRegisters: case ModbusCommand.FuncWriteSingleRegister: case ModbusCommand.FuncReadExceptionStatus: //TODO break; default: //return an exception command.ExceptionCode = ModbusCommand.ErrorIllegalFunction; break; } }
void listener_ServeCommand(object sender, ServeCommandEventArgs e) { var command = (ModbusCommand)e.Data.UserData; Thread.Sleep(SlaveDelay); //take the proper function command handler switch (command.FunctionCode) { case ModbusCommand.FuncReadCoils: if (_function == Function.CoilStatus) { DoRead(command); } else { IllegalFunction(command); } break; case ModbusCommand.FuncReadInputDiscretes: if (_function == Function.InputStatus) { DoRead(command); } else { IllegalFunction(command); } break; case ModbusCommand.FuncReadInputRegisters: if (_function == Function.InputRegister) { DoRead(command); } else { IllegalFunction(command); } break; case ModbusCommand.FuncReadMultipleRegisters: if (_function == Function.HoldingRegister) { DoRead(command); } else { IllegalFunction(command); } break; case ModbusCommand.FuncWriteCoil: if (_function == Function.CoilStatus) { DoWrite(command); } else { IllegalFunction(command); } break; case ModbusCommand.FuncForceMultipleCoils: if (_function == Function.CoilStatus) { DoWrite(command); } else { IllegalFunction(command); } break; case ModbusCommand.FuncWriteMultipleRegisters: if (_function == Function.HoldingRegister) { DoWrite(command); } else { IllegalFunction(command); } break; case ModbusCommand.FuncWriteSingleRegister: if (_function == Function.HoldingRegister) { DoWrite(command); } else { IllegalFunction(command); } break; case ModbusCommand.FuncReadExceptionStatus: //TODO break; case ModbusCommand.FuncReadCustom: if (_function == Function.HoldingRegister) { for (int i = 0; i < command.Count; i++) { command.Data[i] = _registerData[command.Offset + i]; } } else { IllegalFunction(command); } break; default: //return an exception command.ExceptionCode = ModbusCommand.ErrorIllegalFunction; break; } }
void ListenerServeCommand(object sender, ServeCommandEventArgs e) { LastAccess = DateTime.Now; var command = (ModbusCommand)e.Data.UserData; var traceLines = new StringBuilder(); traceLines.AppendLine($"[{Port}:{Id}] Command fn={command.FunctionCode}, addr={command.Address}, cnt={command.Count}"); //take the proper function command handler switch (command.FunctionCode) { case ModbusCommand.FuncReadCoils: var boolArray = DataAccess.ReadCoils(command.Offset, command.Count); for (var i = 0; i < command.Count; i++) { command.Data[i] = (ushort)(boolArray[i] ? 1 : 0); traceLines.Append($"[{command.Offset + i}]={command.Data[i]} "); } traceLines.AppendLine(string.Empty); break; case ModbusCommand.FuncReadInputDiscretes: boolArray = DataAccess.ReadInputDiscretes(command.Offset, command.Count); for (var i = 0; i < command.Count; i++) { command.Data[i] = (ushort)(boolArray[i] ? 1 : 0); traceLines.Append($"[{command.Offset + i}]={command.Data[i]} "); } traceLines.AppendLine(string.Empty); break; case ModbusCommand.FuncWriteCoil: DataAccess.WriteCoils(command.Offset, new[] { command.Data[0] != 0 }); traceLines.AppendLine($"[{command.Offset}]={command.Data[0]} "); break; case ModbusCommand.FuncForceMultipleCoils: var boolList = new List <bool>(); for (var i = 0; i < command.Count; i++) { var index = command.Offset + (i / 16); var mask = 1 << (i % 16); var value = (command.Data[index] & mask) != 0; boolList.Add(value); traceLines.Append($"[{index}]={value} "); } DataAccess.WriteCoils(command.Offset, boolList.ToArray()); traceLines.AppendLine(string.Empty); break; case ModbusCommand.FuncReadInputRegisters: command.Data = DataAccess.ReadInputRegisters(command.Offset, command.Count); for (var i = 0; i < command.Count; i++) { traceLines.Append($"[{command.Offset + i}]={command.Data[i]} "); } traceLines.AppendLine(string.Empty); break; case ModbusCommand.FuncReadMultipleRegisters: command.Data = DataAccess.ReadHoldingRegisters(command.Offset, command.Count); for (var i = 0; i < command.Count; i++) { traceLines.Append($"[{command.Offset + i}]={command.Data[i]} "); } traceLines.AppendLine(string.Empty); break; case ModbusCommand.FuncWriteMultipleRegisters: DataAccess.WriteRegisters(command.Offset, command.Data); for (var i = 0; i < command.Count; i++) { traceLines.Append($"[{command.Offset + i}]={command.Data[i]} "); } traceLines.AppendLine(string.Empty); break; case ModbusCommand.FuncWriteSingleRegister: DataAccess.WriteRegisters(command.Offset, command.Data); for (var i = 0; i < command.Count; i++) { traceLines.Append($"[{command.Offset + i}]={command.Data[i]} "); } traceLines.AppendLine(string.Empty); break; case ModbusCommand.FuncReadExceptionStatus: traceLines.AppendLine("ModbusSlave: Unhandled command FuncReadExceptionStatus"); break; default: //return an exception Trace.TraceError("ModbusSlave: Illegal Modbus FunctionCode"); command.ExceptionCode = ModbusCommand.ErrorIllegalFunction; break; } if (_enableCommandTrace) { Trace.TraceInformation(traceLines.ToString()); } }