예제 #1
0
        static void Main(string[] args)
        {
            var settings = ConfigurationManager.AppSettings;
            var port = settings["COM_PORT"];
            var speed = Convert.ToInt32(settings["COM_SPEED"]);
            var tcpPort = Convert.ToInt32(settings["TCP_PORT"]);
            serial = new SerialManager(port, speed);
            Console.WriteLine("UART: " + port + " - " + speed);
            server = new HardwareTcpServer(tcpPort);
            server.OnServerState += server_OnServerState;
            server.OnClientState += server_OnClientState;
            server.OnData += server_OnData;
            serial.OnReceive += serial_OnReceive;
            serial.OnStateChange += serial_OnStateChange;
            serial.Connect();

           // bridge = new HttpToUartBridge(6200);

            TcpListener listener = new TcpListener(IPAddress.Parse("188.127.233.35"), tcpPort);
            listener.Start();
            Console.WriteLine("TCP: " + tcpPort);
            while (Thread.CurrentThread.ThreadState == ThreadState.Running)
            {
                Console.WriteLine("Listening " + tcpPort);
                while (!listener.Pending())
                {
                    Thread.Sleep(300);
                }
                server.AcceptTcpClient(listener.AcceptSocket());
            }
            Console.WriteLine("Stopped");
            listener.Stop();
            server.Close();
            serial.Close();
        }
예제 #2
0
        protected void OpenDevice(SerialConfig cfg)
        {
            if (cfg == null) return;
            if (device != null)
            {
                device.Close();
            }
            try
            {
                switch (cfg.RxPacketType)
                {
                    case PacketType.Raw:
                        device = new SerialManager(cfg);
                        break;
/*case PacketType.Simple:
                    case PacketType.SimpleCoded:
                    case PacketType.SimpleCRC:
                    case PacketType.Sized:                    
                    case PacketType.SizedOld:
                    case PacketType.SizedCRC:
                    case PacketType.SizedCRC_old:
                    case PacketType.PacketInvariant:
                        device = new SerialPacketManager(cfg);
                        break;*/
                    case PacketType.Addressed:
                    case PacketType.AddressedOld:
                        device = new SerialAddressedManager(cfg);
                        break;
                    case PacketType.XRouting:
                        device = new XRoutingManager(cfg);
                        break;
                    default: 
                        device = new SerialPacketManager(cfg);
                        break;
                }
                if (device.Connect())
                {
                    device.OnStateChange += SelectedPort_Disposed;
                    ShowMessage(cfg.PortName + "  " + "\nConnected!");
                    ShowMessage(JsonConvert.SerializeObject(cfg));
                    EnableItems();
                }
                else
                {
                    DisableItems();
                    device.Close();
                    device = null;
                    ShowError(cfg.PortName + "Connecting error: " + device.LastError);
                }
            }
            catch (Exception err)
            {
                ShowError(cfg.PortName + "Connecting error: ", err);
            }
        }