예제 #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="config">Configuration structure</param>
 public ModbusLinkRtu(ModbusConfiguration config)
 {
     // Init serial port driver
     PHY = new ModbusSerialPort(config);
     // Init link layer thread
     LinkThread = new Thread(ModbusRtuLinkTask);
     // Configure frame timing and set handler
     ConfigureTiming(config.BaudRate);
     FramingTimer.OnTimeout += FramingTimeoutHandler;
 }
예제 #2
0
파일: ModbusWindow.cs 프로젝트: dozack/rps
        private void bttnConnection_Click(object sender, EventArgs e)
        {
            if (ProtocolHandler == null || !ProtocolHandler.Connected)
            {
                // ProtocolHandler not initalized or disconnected
                ModbusConfiguration config = new ModbusConfiguration()
                {
                    PortName      = cmbPorts.SelectedItem.ToString(),
                    BaudRate      = baudrates[cmbBaud.SelectedItem.ToString()],
                    ClientAddress = 0x1,
                    IsServer      = true,
                    ProtocolType  = ModbusProtocolType.ASCII,
                };
                // Init new instance of application layer
                ProtocolHandler = new Modbus(config);

                ProtocolHandler.Coils.OnValueUpdated            += Coils_OnValueUpdated;
                ProtocolHandler.HoldingRegisters.OnValueUpdated += HoldingRegisters_OnValueUpdated;

                ProtocolHandler.Coils.Write(0x0000, false, true);
                ProtocolHandler.HoldingRegisters.Write(0x0000, 0x0000, true);

                // Try to connect to bus
                if (!ProtocolHandler.Connect())
                {
                    MessageBox.Show("Error during connection, try again.");
                    return;
                }

                if (ProtocolHandler.IsServer)
                {
                    UpdateTimer.Interval = 200;
                    UpdateTimer.Tick    += UpdateTimer_Tick;
                    UpdateTimer.Start();
                }

                // Update controls
                bttnConnection.Text = "Disconnect";
                cmbPorts.Enabled    = false;
                cmbBaud.Enabled     = false;
                grpData.Enabled     = true;
            }
            else
            {
                UpdateTimer.Stop();
                // Disconnect from bus
                ProtocolHandler.Disconnect();

                // Update controls
                bttnConnection.Text = "Connect";
                cmbPorts.Enabled    = true;
                cmbBaud.Enabled     = true;
                grpData.Enabled     = false;
            }
        }
예제 #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="config">Configuration structure</param>
 public ModbusSerialPort(ModbusConfiguration config)
 {
     Port = new SerialPort()
     {
         PortName     = config.PortName,
         BaudRate     = config.BaudRate,
         DataBits     = 8,
         Parity       = Parity.None,
         StopBits     = StopBits.Two,
         WriteTimeout = 100,
         ReadTimeout  = 100
     };
     Port.DataReceived += Port_DataReceivedHandler;
 }
예제 #4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="config">Configuration structure</param>
 public ModbusLinkAscii(ModbusConfiguration config)
 {
     // Init serial port driver
     PHY = new ModbusSerialPort(config);
     // Init link layer thread
     LinkThread = new Thread(ModbusAsciiLinkTask)
     {
         Name     = "Modbus Link Layer Thread",
         Priority = ThreadPriority.Normal
     };
     // Configure timeout timer
     TimeoutCounter.Interval  = 1000;
     TimeoutCounter.AutoReset = false;
     TimeoutCounter.Elapsed  += TimeoutHandler;
 }
예제 #5
0
파일: Modbus.cs 프로젝트: dozack/rps
        public Modbus(ModbusConfiguration config)
        {
            switch (config.ProtocolType)
            {
            case ModbusProtocolType.ASCII:
                link = new ModbusLinkAscii(config);
                break;

            case ModbusProtocolType.RTU:
                link = new ModbusLinkRtu(config);
                break;

            default:
                throw new ArgumentException("INVALID_CONFIG: ProtocolType");
            }
            IsServer                = config.IsServer;
            ClientAddress           = config.ClientAddress;
            link.OnMessageReceived += ModbusLink_MessageReceivedHandler;
            link.OnTimeoutOccured  += ModbusLink_TimeoutHandler;
        }