Example #1
0
 public void DisconnectDevice()
 {
     if (DeviceClient != null)
     {
         DeviceClient.Close();
     }
 }
Example #2
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);
        }
Example #3
0
        private bool disposedValue = false;         // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    ConsoleWriteSerialOutput(FullDeviceOutput);

                    if (DeviceClient != null)
                    {
                        DeviceClient.Close();
                    }

                    if (SimulatorClient != null)
                    {
                        SimulatorClient.Disconnect();
                    }

                    Thread.Sleep(DelayAfterDisconnectingFromHardware);
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                disposedValue = true;
            }
        }
Example #4
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);
        }
Example #5
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);
        }
Example #6
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));
        }
Example #7
0
 public void DisconnectDevice()
 {
     DeviceClient.Close();
 }
 public void Close()
 {
     Client.Close();
 }
        public void Disconnect()
        {
            Client.Close();

            IsConnected = false;
        }
Example #10
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);
                }
            }
        }
Example #11
0
 public void Dispose()
 {
     Client.Close();
 }