// ------------------------------------------------------------------------ /// <summary>Write single register in slave synchronous.</summary> /// <param name="id">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="startAddress">Address to where the data is written.</param> /// <param name="values">Contains the register information.</param> /// <param name="result">Contains the result of the synchronous write.</param> public void WriteSingleRegister(UInt16 myInvocationId, UInt16 startAddress, Byte[] values, ref Byte[] result) { var _ModbusPDU = ModbusProtocol.CreateWriteHeader(myInvocationId, startAddress, 1, 1, FunctionCode.WriteSingleRegister); _ModbusPDU.Write(values, 10, 2); result = WriteSyncData(_ModbusPDU, myInvocationId); }
// ------------------------------------------------------------------------ /// <summary>Write multiple coils in slave synchronous.</summary> /// <param name="id">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="startAddress">Address from where the data read begins.</param> /// <param name="numBits">Specifys number of bits.</param> /// <param name="values">Contains the bit information in byte format.</param> /// <param name="result">Contains the result of the synchronous write.</param> public void WriteMultipleCoils(UInt16 myInvocationId, UInt16 startAddress, UInt16 numBits, Byte[] values, ref Byte[] result) { var _NumBytes = Convert.ToByte(values.Length); var _ModbusPDU = ModbusProtocol.CreateWriteHeader(myInvocationId, startAddress, numBits, (byte)(_NumBytes + 2), FunctionCode.WriteMultipleCoils); _ModbusPDU.Write(values, 13, _NumBytes); result = WriteSyncData(_ModbusPDU, myInvocationId); }
// ------------------------------------------------------------------------ /// <summary>Write single coil in slave synchronous.</summary> /// <param name="id">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="startAddress">Address from where the data read begins.</param> /// <param name="OnOff">Specifys if the coil should be switched on or off.</param> /// <param name="result">Contains the result of the synchronous write.</param> public void WriteSingleCoils(UInt16 myInvocationId, UInt16 startAddress, bool OnOff, ref Byte[] result) { var _ModbusPDU = ModbusProtocol.CreateWriteHeader(myInvocationId, startAddress, 1, 1, FunctionCode.WriteSingleCoil); if (OnOff == true) { _ModbusPDU.Seek(10, SeekOrigin.Begin); _ModbusPDU.WriteByte(255); } result = WriteSyncData(_ModbusPDU, myInvocationId); }
// ------------------------------------------------------------------------ /// <summary>Read/Write multiple registers in slave synchronous. The result is given in the response function.</summary> /// <param name="id">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="startReadAddress">Address from where the data read begins.</param> /// <param name="numInputs">Length of data.</param> /// <param name="startWriteAddress">Address to where the data is written.</param> /// <param name="values">Contains the register information.</param> /// <param name="result">Contains the result of the synchronous command.</param> public void ReadWriteMultipleRegister(UInt16 myInvocationId, UInt16 startReadAddress, UInt16 numInputs, UInt16 startWriteAddress, Byte[] values, ref Byte[] result) { UInt16 numBytes = Convert.ToUInt16(values.Length); if (numBytes % 2 > 0) { numBytes++; } var _ModbusPDU = ModbusProtocol.CreateReadWriteHeader(myInvocationId, startReadAddress, numInputs, startWriteAddress, Convert.ToUInt16(numBytes / 2)); _ModbusPDU.Write(values, 17); result = WriteSyncData(_ModbusPDU, myInvocationId); }
// ------------------------------------------------------------------------ /// <summary>Write multiple registers in slave synchronous.</summary> /// <param name="id">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="startAddress">Address to where the data is written.</param> /// <param name="values">Contains the register information.</param> /// <param name="result">Contains the result of the synchronous write.</param> public void WriteMultipleRegister(UInt16 myInvocationId, UInt16 startAddress, Byte[] values, ref Byte[] result) { UInt16 numBytes = Convert.ToUInt16(values.Length); if (numBytes % 2 > 0) { numBytes++; } var _ModbusPDU = ModbusProtocol.CreateWriteHeader(myInvocationId, startAddress, Convert.ToUInt16(numBytes / 2), Convert.ToByte(numBytes + 2), FunctionCode.WriteMultipleRegister); _ModbusPDU.Write(values, 13); result = WriteSyncData(_ModbusPDU, myInvocationId); }
// ------------------------------------------------------------------------ /// <summary>Read holding registers from slave asynchronous. The result is given in the response function.</summary> /// <param name="id">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="startAddress">Address from where the data read begins.</param> /// <param name="numInputs">Length of data.</param> public void ReadHoldingRegister(UInt16 myInvocationId, UInt16 startAddress, UInt16 numInputs) { WriteAsyncData(ModbusProtocol.CreateReadHeader(myInvocationId, startAddress, numInputs, FunctionCode.ReadHoldingRegister), myInvocationId); }
// ------------------------------------------------------------------------ /// <summary>Read discrete inputs from slave synchronous.</summary> /// <param name="id">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="startAddress">Address from where the data read begins.</param> /// <param name="numInputs">Length of data.</param> /// <param name="values">Contains the result of function.</param> public void ReadDiscreteInputs(UInt16 myInvocationId, UInt16 startAddress, UInt16 numInputs, ref Byte[] values) { values = WriteSyncData(ModbusProtocol.CreateReadHeader(myInvocationId, startAddress, numInputs, FunctionCode.ReadDiscreteInputs), myInvocationId); }
// ------------------------------------------------------------------------ /// <summary>Read coils from slave synchronous.</summary> /// <param name="myInvocationId">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="myStartAddress">Address from where the data read begins.</param> /// <param name="myNumberOfCoils">Length of data.</param> /// <param name="values">Contains the result of function.</param> public void ReadCoils(UInt16 myInvocationId, UInt16 myStartAddress, UInt16 myNumberOfCoils, ref Byte[] values) { values = WriteSyncData(ModbusProtocol.CreateReadHeader(myInvocationId, myStartAddress, myNumberOfCoils, FunctionCode.ReadCoils), myInvocationId); }
// ------------------------------------------------------------------------ /// <summary>Read input registers from slave asynchronous. The result is given in the response function.</summary> /// <param name="id">Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.</param> /// <param name="startAddress">Address from where the data read begins.</param> /// <param name="numInputs">Length of data.</param> public void ReadInputRegister(UInt16 myReqestNumber, UInt16 myStartAddress, UInt16 myNumberOfInputs) { WriteAsyncData(ModbusProtocol.CreateReadHeader(myReqestNumber, myStartAddress, myNumberOfInputs, FunctionCode.ReadInputRegister), myReqestNumber); }