Esempio n. 1
0
        private void BtnConnectClick(object sender, EventArgs e)
        {
            try
            {
                switch (CommunicationMode)
                {
                case CommunicationMode.RTU:
                    _uart = new SerialPort(PortName, Baud, Parity, DataBits, StopBits);
                    _uart.Open();
                    var rtuServer = new ModbusServer(new ModbusRtuCodec())
                    {
                        Address = SlaveId
                    };
                    rtuServer.OutgoingData  += DriverOutgoingData;
                    rtuServer.IncommingData += DriverIncommingData;
                    _listener = _uart.GetListener(rtuServer);
                    _listener.ServeCommand += listener_ServeCommand;
                    _listener.Start();
                    AppendLog(String.Format("Connected using RTU to {0}", PortName));
                    break;

                case CommunicationMode.UDP:
                    _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    _socket.Bind(new IPEndPoint(IPAddress.Any, TCPPort));
                    //create a server driver
                    var udpServer = new ModbusServer(new ModbusTcpCodec())
                    {
                        Address = SlaveId
                    };
                    udpServer.OutgoingData  += DriverOutgoingData;
                    udpServer.IncommingData += DriverIncommingData;
                    //listen for an incoming request
                    _listener = _socket.GetUdpListener(udpServer);
                    _listener.ServeCommand += listener_ServeCommand;
                    _listener.Start();
                    AppendLog(String.Format("Listening to UDP port {0}", TCPPort));
                    break;

                case CommunicationMode.TCP:
                    _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    _socket.Bind(new IPEndPoint(IPAddress.Any, TCPPort));
                    _socket.Listen(10);
                    //create a server driver
                    _thread = new Thread(Worker);
                    _thread.Start();
                    AppendLog(String.Format("Listening to TCP port {0}", TCPPort));

                    // simulate classic
                    broadcasrter = new UDPBroadcaster();
                    broadcasrter.SendDatagrams(TCPPort);
                    break;
                }
            }
            catch (Exception ex)
            {
                AppendLog(ex.Message);
                return;
            }
            btnConnect.Enabled        = false;
            buttonDisconnect.Enabled  = true;
            grpExchange.Enabled       = true;
            groupBoxFunctions.Enabled = false;
            groupBoxTCP.Enabled       = false;
            groupBoxRTU.Enabled       = false;
            groupBoxMode.Enabled      = false;
        }
Esempio n. 2
0
        private void BtnConnectClick(object sender, EventArgs e)
        {
            try
            {
                switch (CommunicationMode)
                {
                    case CommunicationMode.RTU:
                        _uart = new SerialPort(PortName, Baud, Parity, DataBits, StopBits);
                        _uart.Open();
                        var rtuServer = new ModbusServer(new ModbusRtuCodec()) { Address = SlaveId };
                        rtuServer.OutgoingData += DriverOutgoingData;
                        rtuServer.IncommingData += DriverIncommingData;
                        _listener = _uart.GetListener(rtuServer);
                        _listener.ServeCommand += listener_ServeCommand;
                        _listener.Start();
                        AppendLog(String.Format("Connected using RTU to {0}", PortName));
                        break;

                    case CommunicationMode.UDP:
                        _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                        _socket.Bind(new IPEndPoint(IPAddress.Any, TCPPort));
                        //create a server driver
                        var udpServer = new ModbusServer(new ModbusTcpCodec()) { Address = SlaveId };
                        udpServer.OutgoingData += DriverOutgoingData;
                        udpServer.IncommingData += DriverIncommingData;
                        //listen for an incoming request
                        _listener = _socket.GetUdpListener(udpServer);
                        _listener.ServeCommand += listener_ServeCommand;
                        _listener.Start();
                        AppendLog(String.Format("Listening to UDP port {0}", TCPPort));
                        break;

                    case CommunicationMode.TCP:
                        _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        _socket.Bind(new IPEndPoint(IPAddress.Any, TCPPort));
                        _socket.Listen(10);
                        //create a server driver
                        _thread = new Thread(Worker);
                        _thread.Start();
                        AppendLog(String.Format("Listening to TCP port {0}", TCPPort));

                        // simulate classic
                        broadcasrter = new UDPBroadcaster();
                        broadcasrter.SendDatagrams(TCPPort);
                        break;
                }
            }
            catch (Exception ex)
            {
                AppendLog(ex.Message);
                return;
            }
            btnConnect.Enabled = false;
            buttonDisconnect.Enabled = true;
            grpExchange.Enabled = true;
            groupBoxFunctions.Enabled = false;
            groupBoxTCP.Enabled = false;
            groupBoxRTU.Enabled = false;
            groupBoxMode.Enabled = false;
        }