public void ReadExample() { Console.WriteLine("FYI: ReadExample"); // Create the connection IPAddress iPAddress = new IPAddress(SETTING_MODBUS_SERVER_TCP_IP); this.tcpClient = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Attempt to connect to the Modbus slave. this.tcpClient.Connect(new IPEndPoint(iPAddress, SETTING_MODBUS_SERVER_TCP_PORT)); // We are connected. Console.WriteLine("FYI: Connected to IP address=[{0}]", this.tcpClient.RemoteEndPoint.ToString()); System.UInt16[] data = new System.UInt16[SETTING_MODBUS_CLIENT_LENGTH]; System.Byte exceptionCode; unsafe { // Set some unmanaged memory up. IntPtr unmanagedPointer = Marshal.AllocHGlobal(SETTING_MODBUS_CLIENT_LENGTH); // Send the message Console.WriteLine("FYI: Sending ReadRegisters message"); uint resultReadRegisters = CASModbusAdapter.ReadRegisters(1, SETTING_MODBUS_CLIENT_DEVICE_ID, CASModbusAdapter.FUNCTION_03_READ_HOLDING_REGISTERS, SETTING_MODBUS_CLIENT_ADDRESS, SETTING_MODBUS_CLIENT_LENGTH, unmanagedPointer, (ushort)(data.Length * sizeof(System.UInt16)), &exceptionCode); // 1 success if (resultReadRegisters == CASModbusAdapter.STATUS_SUCCESS) { // Extract the data from the unmannged memory into a managed buffer. CopyBytesIntoPointer(unmanagedPointer, data, 0, SETTING_MODBUS_CLIENT_LENGTH); Marshal.FreeHGlobal(unmanagedPointer); this.tcpClient.Close(); // Print the data to the screen. Console.Write("FYI: Data: "); foreach (System.UInt16 value in data) { Console.Write(value); Console.Write(", "); } Console.WriteLine(""); } else if (resultReadRegisters == CASModbusAdapter.STATUS_ERROR_MODBUS_EXCEPTION) { Console.WriteLine("Modbus.Exception={0}", exceptionCode); } else { Console.WriteLine("ModbusStack.Error={0}", resultReadRegisters); } } }
public void ReadExample() { Console.WriteLine("FYI: ReadExample"); System.UInt16[] data = new System.UInt16[SETTING_MODBUS_CLIENT_LENGTH]; System.Byte exceptionCode; unsafe { // Set some unmanaged memory up. IntPtr unmanagedPointer = Marshal.AllocHGlobal(SETTING_MODBUS_CLIENT_LENGTH); // Send the message Console.WriteLine("FYI: Sending ReadRegisters message"); uint resultReadRegisters = CASModbusAdapter.ReadRegisters(1, SETTING_MODBUS_CLIENT_DEVICE_ID, CASModbusAdapter.FUNCTION_03_READ_HOLDING_REGISTERS, SETTING_MODBUS_CLIENT_ADDRESS, SETTING_MODBUS_CLIENT_LENGTH, unmanagedPointer, (ushort)(data.Length * sizeof(System.UInt16)), &exceptionCode); // 1 success if (resultReadRegisters == CASModbusAdapter.STATUS_SUCCESS) { // Extract the data from the unmannged memory into a managed buffer. CopyBytesIntoPointer(unmanagedPointer, data, 0, SETTING_MODBUS_CLIENT_LENGTH); Marshal.FreeHGlobal(unmanagedPointer); // Print the data to the screen. Console.Write("FYI: Data: "); foreach (System.UInt16 value in data) { Console.Write(value); Console.Write(", "); } Console.WriteLine(""); } else if (resultReadRegisters == CASModbusAdapter.STATUS_ERROR_MODBUS_EXCEPTION) { Console.WriteLine("Modbus.Exception={0}", exceptionCode); } else { Console.WriteLine("ModbusStack.Error={0}", resultReadRegisters); } } }