Exemplo n.º 1
0
        public static Message CreatePresetMultipleRegistersCommand(SlaveAddress slaveAddress, HoldingRegisterAddress firstRegister, ushort[] desiredValues)
        {
            if (desiredValues == null)
            {
                throw new ArgumentNullException();
            }
            if (desiredValues.Length == 0)
            {
                throw new ArgumentException("Must write at least one register.");
            }
            if (desiredValues.Length > MaxUShortArrayLength)
            {
                throw new ArgumentException("Too many registers.");
            }

            var data = new List <byte>();

            WriteSlaveAddress(data, slaveAddress);
            WriteFunctionCode(data, FunctionCode.PresetMultipleRegisters);
            WriteHoldingRegisterAddress(data, firstRegister);
            WriteBigEndianUInt16(data, (ushort)desiredValues.Length);
            WriteLengthPrefixedUShortArray(data, desiredValues);

            return(CreateFromRaw(data));
        }
Exemplo n.º 2
0
        public static Message CreateForceMultipleCoilsCommand(SlaveAddress slaveAddress, CoilAddress firstCoil, bool[] desiredStates)
        {
            if (desiredStates == null)
            {
                throw new ArgumentNullException();
            }
            if (desiredStates.Length == 0)
            {
                throw new ArgumentException("Must write at least one coil.");
            }
            if (desiredStates.Length > MaxBooleanArrayLength)
            {
                throw new ArgumentException("Too many coils.");
            }

            var data = new List <byte>();

            WriteSlaveAddress(data, slaveAddress);
            WriteFunctionCode(data, FunctionCode.ForceSingleCoil);
            WriteCoilAddress(data, firstCoil);
            WriteBigEndianUInt16(data, (ushort)desiredStates.Length);
            WriteLengthPrefixedPackedBooleanArray(data, desiredStates);

            return(CreateFromRaw(data));
        }
 internal ModbusControllerConnection(NModbus.Device.IModbusMaster master, SlaveAddress slave)
 {
     _master           = master;
     _slave            = slave;
     _slaveRaw         = slave.WireValue;
     _declarationSpace = new DeclarationSpace(this);
 }
 public ModbusControllerConnectionFactory(ModbusSerialProtocol protocol, SlaveAddress slave, string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
 {
     _protocol = protocol;
     _slave    = slave;
     _portName = portName;
     _baudRate = baudRate;
     _parity   = parity;
     _dataBits = dataBits;
     _stopBits = stopBits;
 }
Exemplo n.º 5
0
        public static Message CreatePresetSingleRegisterCommand(SlaveAddress slaveAddress, HoldingRegisterAddress register, ushort desiredValue)
        {
            var data = new List <byte>();

            WriteSlaveAddress(data, slaveAddress);
            WriteFunctionCode(data, FunctionCode.PresetSingleRegister);
            WriteHoldingRegisterAddress(data, register);
            WriteBigEndianUInt16(data, desiredValue);

            return(CreateFromRaw(data));
        }
Exemplo n.º 6
0
        public static Message CreateReadInputRegistersCommand(SlaveAddress slaveAddress, InputRegisterAddress firstRegister, ushort numberOfRegisters)
        {
            var data = new List <byte>();

            WriteSlaveAddress(data, slaveAddress);
            WriteFunctionCode(data, FunctionCode.ReadInputRegisters);
            WriteInputRegisterAddress(data, firstRegister);
            WriteBigEndianUInt16(data, numberOfRegisters);

            return(CreateFromRaw(data));
        }
Exemplo n.º 7
0
        public static Message CreateReadCoilStatusCommand(SlaveAddress slaveAddress, CoilAddress firstCoil, ushort numberOfCoils)
        {
            var data = new List <byte>();

            WriteSlaveAddress(data, slaveAddress);
            WriteFunctionCode(data, FunctionCode.ReadCoilStatus);
            WriteCoilAddress(data, firstCoil);
            WriteBigEndianUInt16(data, numberOfCoils);

            return(CreateFromRaw(data));
        }
Exemplo n.º 8
0
        public static Message CreateForceSingleCoilCommand(SlaveAddress slaveAddress, CoilAddress coil, bool desiredState)
        {
            var data = new List <byte>();

            WriteSlaveAddress(data, slaveAddress);
            WriteFunctionCode(data, FunctionCode.ForceSingleCoil);
            WriteCoilAddress(data, coil);

            const byte On  = 0xff;
            const byte Off = 0x00;

            data.Add(desiredState ? On : Off);
            data.Add(0x00);

            return(CreateFromRaw(data));
        }
Exemplo n.º 9
0
 private static void WriteSlaveAddress(List <byte> buffer, SlaveAddress address)
 {
     buffer.Add(address.WireValue);
 }