V1/Cet.Develop.NETMF.IO.Test/Cet.Develop.NETMF.IO.Test/Program.cs
/// <summary>
/// Host server for the incoming request from a remote master
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
Host server for the incoming request from a remote master
Host server for the incoming request from a remote master
static void listener_ServeCommand(object sender, ServeCommandEventArgs e)
        {
            var command = (ModbusCommand)e.Data.UserData;
            switch (command.FunctionCode)
            {
                case ModbusCommand.FuncReadCoils:
                    for (int i = 0; i < command.Count; i++)
                        command.Data[i] = (ushort)(_coils[i + command.Offset].Read() ? 1 : 0);
                    break;
                case ModbusCommand.FuncReadInputDiscretes:
                    for (int i = 0; i < command.Count; i++)
                        command.Data[i] = (ushort)(_inputs[i + command.Offset].Read() ? 1 : 0);
                    break;
                case ModbusCommand.FuncWriteCoil:
                    bool state = command.Data[0] != 0;
                    _coils[command.Offset].Write(state);
                    break;
                case ModbusCommand.FuncForceMultipleCoils:
                    for (int i = 0; i < command.Count; i++)
                        _coils[i + command.Offset].Write(command.Data[i] != 0);
                    break;
                case ModbusCommand.FuncReadInputRegisters:
                    for (int i = 0; i < command.Count; i++)
                        command.Data[i] = (ushort)_analogs[i + command.Offset].Read();
                    break;
                case ModbusCommand.FuncReadMultipleRegisters:
                case ModbusCommand.FuncWriteMultipleRegisters:
                case ModbusCommand.FuncWriteSingleRegister:
                case ModbusCommand.FuncReadExceptionStatus:
                    break;
                default:
                    command.ExceptionCode = ModbusCommand.ErrorIllegalFunction;
                    break;
            }
        }
static void listener_ServeCommand ( object sender , ServeCommandEventArgs e ) { var command = ( ModbusCommand ) e . Data . UserData ; switch ( command . FunctionCode ) { case ModbusCommand . FuncReadCoils : for ( int i = 0 ; i < command . Count ; i ++ ) command . Data [ i ] = ( ushort ) ( _coils [ i + command . Offset ] . Read ( ) ? 1 : 0 ) ; break ; case ModbusCommand . FuncReadInputDiscretes : for ( int i = 0 ; i < command . Count ; i ++ ) command . Data [ i ] = ( ushort ) ( _inputs [ i + command . Offset ] . Read ( ) ? 1 : 0 ) ; break ; case ModbusCommand . FuncWriteCoil : bool state = command . Data [ 0 ] != 0 ; _coils [ command . Offset ] . Write ( state ) ; break ; case ModbusCommand . FuncForceMultipleCoils : for ( int i = 0 ; i < command . Count ; i ++ ) _coils [ i + command . Offset ] . Write ( command . Data [ i ] != 0 ) ; break ; case ModbusCommand . FuncReadInputRegisters : for ( int i = 0 ; i < command . Count ; i ++ ) command . Data [ i ] = ( ushort ) _analogs [ i + command . Offset ] . Read ( ) ; break ; case ModbusCommand . FuncReadMultipleRegisters : case ModbusCommand . FuncWriteMultipleRegisters : case ModbusCommand . FuncWriteSingleRegister : case ModbusCommand . FuncReadExceptionStatus : break ; default : command . ExceptionCode = ModbusCommand . ErrorIllegalFunction ; break ; } }
Host server for the incoming request from a remote master
Host server for the incoming request from a remote master
//take the proper function command handler //TODO //return an exception
sender
object
None
e
ServeCommandEventArgs
None