Exemple #1
0
        public void ReadFromDeviceAndOutputToConsole()
        {
            //Console.WriteLine ("");
            //Console.WriteLine ("Reading the output from the device...");
            //Console.WriteLine ("");

            // Read the output
            var output = DeviceClient.Read();

            FullDeviceOutput += output;

            ConsoleWriteSerialOutput(output);
            Console.WriteLine("");
        }
        public bool DigitalRead(int pinNumber)
        {
            var cmd = String.Format("D{0}:R", pinNumber);

            Client.WriteLine(cmd);

            Thread.Sleep(1000);

            var output = Client.Read();

            var digitalValue = Convert.ToInt32(output.Trim()) == 1;

            return(digitalValue);
        }
Exemple #3
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);
        }
Exemple #4
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);
                }
            }
        }