Exemplo n.º 1
0
 /// <summary>
 /// Test a modbus RTU slave
 /// </summary>
 static void Test_ModbusRTUSlave()
 {
     byte unit_id = 1;
     // Created datastore for unit ID 1
     Datastore ds = new Datastore(unit_id);
     // Crete instance of modbus serial RTU (replace COMx with a free serial port - ex. COM5)
     ModbusSlaveSerial ms = new ModbusSlaveSerial(new Datastore[] { ds }, ModbusSerialType.RTU, "COM7", 19200, 8, Parity.Even, StopBits.One, Handshake.None);
     // Start listen
     ms.StartListen();
     // Print and write some registers...
     Random rnd = new Random();
     while (true)
     {
         Console.Write(
             "---------------------- READING ----------------------" + Environment.NewLine +
             "Input register n.1 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).InputRegisters[30001].ToString("D5") + Environment.NewLine +
             "Input register n.1 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).InputRegisters[30002].ToString("D5") + Environment.NewLine +
             "Input register n.2 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).InputRegisters[30003].ToString("D5") + Environment.NewLine);
             //"Coil register    n.32 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[31].ToString() + Environment.NewLine +
             //"---------------------- WRITING ----------------------" + Environment.NewLine);
         //ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[1] = (ushort)rnd.Next(ushort.MinValue, ushort.MaxValue);
         //Console.WriteLine(
         //    "Holding register n.2  : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).HoldingRegisters[1].ToString("D5"));
         //ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[15] = Convert.ToBoolean(rnd.Next(0, 1));
         //Console.WriteLine(
         //    "Coil register    n.16 : " + ms.ModbusDB.Single(x => x.UnitID == unit_id).Coils[15].ToString());
         // Exec the cicle each 2 seconds
         Thread.Sleep(2000);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="modbus_db">Modbus databases array</param>
 /// <param name="local_address">Local listening IP addresses</param>
 /// <param name="port">Listening TCP port</param>
 public ModbusSlaveUDP(Datastore[] modbus_db, IPAddress local_address, int port)
     : base(modbus_db)
 {
     // Set device states
     connection_type = ConnectionType.UDP_IP;
     // Create UDP listener
     udpl = new UdpClient(new IPEndPoint(local_address, port));
 }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="ds">Database Modbus</param>
 public ModbusSlave(Datastore[] ds)
 {
     // Device status
     device_type = DeviceType.SLAVE;
     // Assign modbus database
     modbus_db = ds;
 }
Exemplo n.º 4
-1
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="modbus_db">Modbus database array</param>
 /// <param name="local_address">Local listening IP addresses</param>
 /// <param name="port">Listening TCP port</param>
 public ModbusSlaveTCP(Datastore[] modbus_db, IPAddress local_address, int port)
     : base(modbus_db)
 {
     // Set device states
     connection_type = ConnectionType.TCP_IP;
     // Crete TCP listener
     tcpl = new TcpListener(local_address, port);
 }
Exemplo n.º 5
-1
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="modbus_db">Modbus database</param>
        /// <param name="type">Type of serial modbus protocol (RTU or ASCII)</param>
        /// <param name="port">Serial port name</param>
        /// <param name="baudrate">Baudrate</param>
        /// <param name="databits">Data bits</param>
        /// <param name="parity">Parity</param>
        /// <param name="stopbits">Stop bits</param>
        /// <param name="handshake">Control flux</param>
        public ModbusSlaveSerial(Datastore[] modbus_db, ModbusSerialType type, string port, int baudrate, int databits, Parity parity, StopBits stopbits, Handshake handshake)
            : base(modbus_db)
        {
            // Set modbus serial protocol type
            switch (type)
            {
                case ModbusSerialType.ASCII:
                    connection_type = ConnectionType.SERIAL_ASCII;
                    break;

                case ModbusSerialType.RTU:
                    connection_type = ConnectionType.SERIAL_RTU;
                    break;
            }
            // Set serial port instance
            sp = new SerialPort(port, baudrate, parity, databits, stopbits);
            sp.Handshake = handshake;
            // Calc interframe delay
            interframe_delay = GetInterframeDelay(sp);
            // Calc interchar delay
            interchar_delay = GetIntercharDelay(sp);
        }
Exemplo n.º 6
-41
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="ds">Database Modbus</param>
 public ModbusSlave(Datastore[] ds)
 {
     // Device status
     device_type = DeviceType.SLAVE;
     // Assign modbus database
     modbus_db = ds;
     // Initialize timer
     sw_ch = new Stopwatch();
 }