Esempio n. 1
0
        public void ConnectDevice()
        {
            if (String.IsNullOrEmpty(DevicePort))
            {
                throw new Exception("The 'DevicePort' property has not been set.");
            }

            if (DeviceBaudRate == 0)
            {
                throw new Exception("The 'DeviceBaudRate' property has not been set.");
            }

            Console.WriteLine("Enabling target hardware device...");

            DeviceClient = new SerialClient(DevicePort, DeviceBaudRate);

            try {
                DeviceClient.Open();
            } catch (IOException ex) {
                HandleConnectionIOException("target", DevicePort, DeviceBaudRate, ex);
            }

            DeviceIsEnabled = true;

            Console.WriteLine("");
        }
        public void SignRequestSerial(SignRequest request)
        {
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedPort))
            {
                throw new Exception("Port can not be null.");
            }
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedDataBits))
            {
                throw new Exception("DataBits can not be null.");
            }
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedBaudRate))
            {
                throw new Exception("BaudRate can not be null.");
            }
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedParity))
            {
                throw new Exception("Parity can not be null.");
            }
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedStopBits))
            {
                throw new Exception("StopBits can not be null.");
            }
            string requestString = JsonConvert.SerializeObject(request);

            SerialClient _client = new SerialClient(Const.Locator.ParameterSetting.SelectedPort,
                                                    int.Parse(Const.Locator.ParameterSetting.SelectedBaudRate),
                                                    (Parity)Enum.Parse(typeof(Parity), Const.Locator.ParameterSetting.SelectedParity),
                                                    int.Parse(Const.Locator.ParameterSetting.SelectedDataBits),
                                                    (StopBits)Enum.Parse(typeof(StopBits), Const.Locator.ParameterSetting.SelectedStopBits));

            _client.Open();
            _client.Complated -= _signTcpClient_Complated;
            _client.Complated += _signTcpClient_Complated;
            _client.Send(0x02, requestString);
        }
Esempio n. 3
0
        public void Run_Then_Response_N_Rounds()
        {
            IClient cli        = new SerialClient(COM.Port101);
            IComm   serialComm = new WrappedComm(cli);

            string sets = @"CmdType,Sent
response,[12 34 56]
response,[34 56 78]";

            serialComm.Setup(sets);
            serialComm.Open();
            Task.Run(() => serialComm.Run());

            IClient client = new SerialClient(COM.Port102);

            client.Open();
            client.Send(Conv.StrHexToBytes("12 34"));
            byte[] rev0 = client.Rev();
            client.Send(Conv.StrHexToBytes("45 67"));
            byte[] rev1 = client.Rev();
            client.Send(Conv.StrHexToBytes("78 90"));
            client.ReadTimeOut = 10;
            byte[] rev2 = client.Rev();
            client.Close();

            serialComm.Stop();
            serialComm.Close();
            Assert.AreEqual("12 34 56", Conv.BytesToStrHex(rev0));
            Assert.AreEqual("34 56 78", Conv.BytesToStrHex(rev1));
            Assert.AreEqual(0, rev2.Length);
        }
 public virtual void Open()
 {
     try {
         Client.Open();
     } catch (IOException ex) {
         Console.WriteLine("Failed to connect to the device at port: " + Client.Port.PortName);
         Console.WriteLine(ex.ToString());
     }
 }
Esempio n. 5
0
 public void EnsurePortIsOpen()
 {
     if (!Client.Port.IsOpen)
     {
         Client.Open();
         Thread.Sleep(1000);
         Client.ReadLine();
     }
 }
Esempio n. 6
0
        public void Connect()
        {
            Client.Open();

            IsConnected = true;

            // Read the first line (the title) to clear the buffer
            Client.ReadLine();
        }
Esempio n. 7
0
        public void Rev_out_of_time()
        {
            IClient cli = new SerialClient(COM.Port101);

            cli.Open();
            cli.ReadTimeOut = 10;
            byte[] rev = cli.Rev();
            cli.Close();

            Assert.AreEqual(0, rev.Length);
        }
Esempio n. 8
0
        public string Send(string port, string value)
        {
            var serialPort = new SerialPort(port, 9600);
            var client     = new SerialClient(serialPort);

            client.Open();

            Thread.Sleep(2000);

            client.WriteLine(value);

            Thread.Sleep(2000);

            var response = client.Read();

            client.Close();

            return(response);
        }
Esempio n. 9
0
        public DeviceInfo ReadDeviceInfoFromSerial(string port)
        {
            Console.WriteLine("Reading device info from serial port...");

            var serialPort = new SerialPort(port, 9600);
            var client     = new SerialClient(serialPort);

            if (!client.Port.IsOpen)
            {
                try
                {
                    client.Open();
                }
                catch (Exception ex)
                {
                    return(null);
                }

                Console.WriteLine("Opened serial port");

                Thread.Sleep(2000);
            }

            var builder = new StringBuilder();

            Console.WriteLine("----- Start Output -----");

            while (client.Port.BytesToRead > 0 && builder.ToString().IndexOf(Extractor.EndDeviceInfoText) == -1)
            {
                var output = builder.ToString();
                var line   = client.ReadLine().Trim();

                Console.WriteLine(line);

                builder.AppendLine(line);
            }

            Console.WriteLine("----- End Output -----");

            var deviceInfo = Extractor.ExtractInfo(port, builder.ToString());

            return(deviceInfo);
        }
Esempio n. 10
0
        public static void StatueRequestSerial(string request)
        {
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedPort))
            {
                throw new Exception("Port can not be null.");
            }
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedDataBits))
            {
                throw new Exception("DataBits can not be null.");
            }
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedBaudRate))
            {
                throw new Exception("BaudRate can not be null.");
            }
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedParity))
            {
                throw new Exception("Parity can not be null.");
            }
            if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedStopBits))
            {
                throw new Exception("StopBits can not be null.");
            }
            if (string.IsNullOrWhiteSpace(request))
            {
                throw new Exception("Request can not be null.");
            }

            StopBits ss = (StopBits)Enum.Parse(typeof(StopBits), Const.Locator.ParameterSetting.SelectedStopBits);


            SerialClient _client = new SerialClient(Const.Locator.ParameterSetting.SelectedPort,
                                                    int.Parse(Const.Locator.ParameterSetting.SelectedBaudRate),
                                                    (Parity)Enum.Parse(typeof(Parity), Const.Locator.ParameterSetting.SelectedParity),
                                                    int.Parse(Const.Locator.ParameterSetting.SelectedDataBits),
                                                    (StopBits)Enum.Parse(typeof(StopBits), Const.Locator.ParameterSetting.SelectedStopBits));

            _client.Open();
            _client.Complated += _statusTcpClient_Complated;
            _client.Send(0x01, request);
        }
Esempio n. 11
0
        public void InitByPort()
        {
            IClient cli        = new SerialClient(COM.Port101);
            IComm   serialComm = new WrappedComm(cli);
            string  sets       = @"CmdType,Sent
response,[34 56 78]";

            serialComm.Setup(sets);
            serialComm.Open();
            Task.Run(() => serialComm.Run());

            IClient client = new SerialClient(COM.Port102);

            client.Open();
            client.Send(Conv.StrHexToBytes("12 34"));
            byte[] rev0 = client.Rev();
            client.Close();

            serialComm.Stop();
            serialComm.Close();
            Assert.AreEqual("34 56 78", Conv.BytesToStrHex(rev0));
        }
Esempio n. 12
0
        public void Connect()
        {
            Client.Open();

            IsConnected = true;
        }
        private void Send()
        {
            switch (Const.Locator.ParameterSetting.CommModel)
            {
            case Commom.CommModel.NetPort:    //网口通讯
                if (string.IsNullOrWhiteSpace(Cmd))
                {
                    throw new Exception("Request cmd can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Request))
                {
                    throw new Exception("Request can not be null.");
                }

                _netClient = new TcpHelper();
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SdcUrl))
                {
                    MessageBoxEx.Show("EFD URL can not be null.", MessageBoxButton.OK);
                    return;
                }
                string[] sdc = Const.Locator.ParameterSetting.SdcUrl.Split(':');
                if (sdc != null && sdc.Count() != 2)
                {
                    MessageBoxEx.Show("EFD URL is not in the right format.", MessageBoxButton.OK);
                    return;
                }
                bool isConn = _netClient.Connect(IPAddress.Parse(sdc[0]), int.Parse(sdc[1]));
                if (!isConn)
                {
                    MessageBoxEx.Show("Failed to connect to EFD.", MessageBoxButton.OK);
                    return;
                }

                _netClient.Complated -= _client_Complated;
                _netClient.Complated += _client_Complated;
                switch (Cmd)
                {
                case "Status":
                    _netClient.Send(0x01, Request);
                    break;

                case "Sign":
                    _netClient.Send(0x02, Request);
                    break;

                case "Report":
                    _netClient.Send(0x04, Request);
                    break;

                default:
                    break;
                }
                _netClient.ReciveAsync();
                break;

            case Commom.CommModel.SerialPort:    //串口通讯
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedPort))
                {
                    throw new Exception("Port can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedDataBits))
                {
                    throw new Exception("DataBits can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedBaudRate))
                {
                    throw new Exception("BaudRate can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedParity))
                {
                    throw new Exception("Parity can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Const.Locator.ParameterSetting.SelectedStopBits))
                {
                    throw new Exception("StopBits can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Cmd))
                {
                    throw new Exception("Request cmd can not be null.");
                }
                if (string.IsNullOrWhiteSpace(Request))
                {
                    throw new Exception("Request can not be null.");
                }
                _client = new SerialClient(Const.Locator.ParameterSetting.SelectedPort,
                                           int.Parse(Const.Locator.ParameterSetting.SelectedBaudRate),
                                           (Parity)Enum.Parse(typeof(Parity), Const.Locator.ParameterSetting.SelectedParity),
                                           int.Parse(Const.Locator.ParameterSetting.SelectedDataBits),
                                           (StopBits)Enum.Parse(typeof(StopBits), Const.Locator.ParameterSetting.SelectedStopBits));
                _client.Open();
                _isCanSend         = false;
                _client.Complated += _client_Complated;
                switch (Cmd)
                {
                case "Status":
                    _client.Send(0x01, Request);
                    break;

                case "Sign":
                    _client.Send(0x02, Request);
                    break;

                case "Report":
                    _client.Send(0x04, Request);
                    break;

                default:
                    break;
                }
                break;

            default:
                break;
            }
        }
Esempio n. 14
0
        public static void Main(string[] args)
        {
            var arguments = new Arguments(args);

            IsVerbose = arguments.Contains("v");
            var userId         = GetConfigValue(arguments, "UserId");
            var pass           = GetConfigValue(arguments, "Password");
            var host           = GetConfigValue(arguments, "Host");
            var deviceName     = GetConfigValue(arguments, "DeviceName");
            var serialPortName = GetConfigValue(arguments, "SerialPort");
            var serialBaudRate = Convert.ToInt32(GetConfigValue(arguments, "SerialBaudRate"));
            var topicPrefix    = "/" + userId;
            var useTopicPrefix = Convert.ToBoolean(ConfigurationSettings.AppSettings["UseTopicPrefix"]);

            SerialPort port = null;

            if (String.IsNullOrEmpty(serialPortName))
            {
                Console.WriteLine("Serial port not specified. Detecting.");
                var detector = new SerialPortDetector();
                port           = detector.Detect();
                serialPortName = port.PortName;
            }
            else
            {
                Console.WriteLine("Serial port specified");
                port = new SerialPort(serialPortName, serialBaudRate);
            }

            Console.WriteLine("Device name: " + GetConfigValue(arguments, "DeviceName"));
            Console.WriteLine("Port name: " + serialPortName);

            var deviceTopic = "/" + deviceName;

            if (useTopicPrefix)
            {
                deviceTopic = topicPrefix + deviceTopic;
            }

            Console.WriteLine(deviceTopic + "/[Key]");

            if (port == null)
            {
                Console.WriteLine("Error: Device port not found.");
            }
            else
            {
                Console.WriteLine("Serial port: " + port.PortName);

                Client = new SerialClient(port);

                try {
                    Client.Open();

                    Console.WriteLine(Client.Read());

                    var isRunning = true;

                    var mqttClient = new MqttClient(host);

                    var clientId = Guid.NewGuid().ToString();

                    mqttClient.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
                    mqttClient.Connect(clientId, userId, pass);


                    var subscribeTopics = GetSubscribeTopics(arguments);
                    foreach (var topic in subscribeTopics)
                    {
                        mqttClient.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
                    }


                    while (isRunning)
                    {
                        var output = "";

                        /*while (!output.Contains(";;"))
                         * {
                         * //	Thread.Sleep(1);
                         *      output += Client.Read ();
                         * }*/

                        //Console.WriteLine("----- Serial output");
                        //Console.WriteLine(output);
                        //Console.WriteLine("-----");

                        var topics = new List <string>();

                        //Publish (arguments, mqttClient, output, topics);

                        //Thread.Sleep(10);
                    }

                    Client.Close();
                } catch (IOException ex) {
                    Console.WriteLine("Error: Please ensure device is connected to port '" + serialPortName + "' and not already in use.\n\n" + ex.Message);
                }
            }
        }