public unsafe int DirectWriteRegisterRW(int index, Single val) { CheckContext(); CheckMapping(); if (index >= mapping->nb_registers) { Exception ex = new Exception("Trying to access outside the registers memory space."); throw ex; } ushort[] buffer = new ushort[2]; fixed(ushort *p = &buffer[0]) { ModbusPinvoke.SetFloat(val, p); } int ret = ModbusPinvoke.WriteRegisterRW(modbusContext, index, buffer [1]); if (ret != 0) { ret = ModbusPinvoke.WriteRegisterRW(modbusContext, index + 1, buffer [0]); } if (ret < 0) { CheckForModbusError(); } return(ret); }
unsafe public void AcceptTcp() { int ret = 0; // Check for context CheckContext(); CheckMapping(); // Check for opening socket if (socket == 0) { Exception ex = new Exception("Start TCP listener before accept connection"); throw ex; } // Wainting for incoming tcp connection fixed(int *ptr = &socket) { ret = ModbusPinvoke.AcceptTcp(modbusContext, ptr); } // Check about the success of the incoming tcp connection if (ret < 0) { CheckForModbusError(); } }
unsafe public byte[] Receive() { int res; byte[] query = new byte[ModbusCore.MODBUS_TCP_MAX_ADU_LENGTH]; CheckContext(); if (socket == 0) { Exception ex = new Exception("Socket is not open."); throw ex; } fixed(byte *queryArray = &query[0]) { res = ModbusPinvoke.Receive(modbusContext, queryArray); } if (res < 0) { CheckForModbusError(); } Array.Resize <byte>(ref query, res); return(query); }
public ModbusCore(string target, int baud, char pariry, int nBits, int stopBits) { // Create modbus tcpip context modbusContext = ModbusPinvoke.ModbusNewRtu(target, baud, pariry, nBits, stopBits); Initialize(); }
public void Dispose() { // Check if mapping still to exist try { Close(); MappingDispose(); } catch {} // Check if context still to exist if (modbusContext != IntPtr.Zero) { ModbusPinvoke.Free(modbusContext); } if (modbusByteTimeout != IntPtr.Zero) { Marshal.FreeHGlobal(modbusByteTimeout); modbusByteTimeout = IntPtr.Zero; } if (modbusResponseTimeout != IntPtr.Zero) { Marshal.FreeHGlobal(modbusResponseTimeout); modbusResponseTimeout = IntPtr.Zero; } modbusContext = IntPtr.Zero; }
public ModbusCore(string AddressTarget, int port) { this.AddressTarget = AddressTarget; // Create modbus tcpip context modbusContext = ModbusPinvoke.ModbusNewTcp(AddressTarget, port); Initialize(); }
public unsafe int SetRsuRts(int flag) { int ret = ModbusPinvoke.RtuSetRts(modbusContext, flag); if (ret < 0) { CheckForModbusError(); } return(ret); }
unsafe public bool Connect() { int ret; ret = ModbusPinvoke.Connect(modbusContext); if (ret != 0) { Close(); } return(ret == 0); }
unsafe public void MappingDispose() { // Check for exsisting mappings if (mapping == null) { Exception ex = new Exception("Mapping is not created."); throw ex; } // Dispose mappings ModbusPinvoke.MappingDispose(mapping); mapping = null; }
unsafe public bool MappingNew(int NBit, int NInputBit, int NHoldingRegisters, int NInputRegister) { // Check for duplicate mappings if (mapping != null) { Exception ex = new Exception("Duplicate mapping creation."); throw ex; } // Create new mapping mapping = ModbusPinvoke.MappingNew(NBit, NInputBit, NHoldingRegisters, NInputRegister); return(mapping == null); }
public unsafe float GetRegisterRWAsFloat(int index) { CheckMapping(); if (index + 1 > mapping->nb_registers) { Exception ex = new Exception("Trying to access outside the registers memory space."); throw ex; } ushort[] buffer = new ushort[2]; buffer[1] = mapping->tab_registers[index]; buffer[0] = mapping->tab_registers[index + 1]; fixed(ushort *p = &buffer[0]) { return(ModbusPinvoke.GetFloat(p)); } }
public unsafe int DirectWriteRegisterRW(int index, short val) { CheckContext(); CheckMapping(); if (index >= mapping->nb_registers) { Exception ex = new Exception("Trying to access outside the registers memory space."); throw ex; } int ret = ModbusPinvoke.WriteRegisterRW(modbusContext, index, val); if (ret < 0) { CheckForModbusError(); } return(ret); }
unsafe public int DirectWriteBitRW(int index, bool val) { CheckContext(); CheckMapping(); if (index >= mapping->nb_bits) { Exception ex = new Exception("Trying to access outside the registers memory space."); throw ex; } int ret = ModbusPinvoke.WriteBit(modbusContext, index, (val ? 1 : 0)); if (ret < 0) { CheckForModbusError(); } return(ret); }
public unsafe int BitsRWWrite(int index, int length) { CheckContext(); CheckMapping(); if (index + length > mapping->nb_bits) { Exception ex = new Exception("Trying to access outside the bits memory space."); throw ex; } int ret = ModbusPinvoke.BitsRWWrite(modbusContext, index, length, &mapping->tab_bits[index]); if (ret < 0) { CheckForModbusError(); } return(ret); }
public unsafe void SetRegisterRWAsFloat(int index, float val) { CheckMapping(); if (index + 1 > mapping->nb_registers) { Exception ex = new Exception("Trying to access outside the registers memory space."); throw ex; } ushort[] buffer = new ushort[2]; fixed(ushort *p = &buffer[0]) { ModbusPinvoke.SetFloat(val, p); } mapping->tab_registers[index] = buffer[1]; mapping->tab_registers[index + 1] = buffer[0]; }
unsafe public int BitsInputRead(int start, int length) { int res = 0; CheckMapping(); if (start + length > mapping->nb_bits) { Exception ex = new Exception("Trying to access outside defined memory space for bits"); throw ex; } res = ModbusPinvoke.BitsInputRead(modbusContext, start, length, &mapping->tab_input_bits[start]); if (res < 0) { CheckForModbusError(); } return(res); }
public void SetByteTimeout(int usecTimeout) { CheckContext(); if (modbusByteTimeout == IntPtr.Zero) { // At the moment I not know how to get the size of timeval struct so use a value big enought to contains timeval struct modbusByteTimeout = Marshal.AllocHGlobal(32); } Timeval timeout = new Timeval(); timeout.tv_usec = usecTimeout % 1000000; timeout.tv_sec = usecTimeout / 1000000; Mono.Unix.Native.NativeConvert.TryCopy(ref timeout, modbusByteTimeout); ModbusPinvoke.SetByteTimeout(modbusContext, modbusByteTimeout); }
unsafe public int RegistersRead(int start, int length) { int res = 0; CheckMapping(); if (start + length > mapping->nb_input_registers) { Exception ex = new Exception("Trying to access outside defined memory space for input_registers"); throw ex; } res = ModbusPinvoke.modbus_read_registers(modbusContext, start, length, (short *)&mapping->tab_registers[start]); if (res < 0) { CheckForModbusError(); } return(res); }
unsafe public int Reply(byte[] query) { int res; CheckContext(); CheckMapping(); if (socket == 0) { Exception ex = new Exception("The socket is not open."); throw ex; } fixed(byte *queryArray = &query[0]) { res = ModbusPinvoke.Reply(modbusContext, queryArray, query.Length, (IntPtr)mapping); } if (res < 0) { CheckForModbusError(); } return(res); }
public unsafe int RegistersRWWriteAndRead(int readIndex, int readLength, int writeIndex, int writeLength) { CheckContext(); CheckMapping(); if (readIndex + readLength > mapping->nb_input_registers) { Exception ex = new Exception("Trying to access outside the input_registers memory space."); throw ex; } if (writeIndex + writeLength > mapping->nb_registers) { Exception ex = new Exception("Trying to access outside the registers memory space."); throw ex; } int ret = ModbusPinvoke.RegistersRWWriteAndRead(modbusContext, readIndex, readLength, &mapping->tab_registers[readIndex], writeIndex, writeLength, &mapping->tab_registers[writeIndex]); if (ret < 0) { CheckForModbusError(); } return(ret); }
unsafe public void ListenTcp(int maxWaitingQueue) { CheckContext(); socket = ModbusPinvoke.ListenTcp(modbusContext, maxWaitingQueue); }
public unsafe void SetSlave(int slave) { ModbusPinvoke.SetSlave(modbusContext, slave); }
public unsafe string Error(int errno) { IntPtr prt = ModbusPinvoke.Error(errno); return(Marshal.PtrToStringAnsi(prt)); }
public void Close() { CheckContext(); ModbusPinvoke.Close(modbusContext); }