Beispiel #1
0
        public string ReadLineFromDevice()
        {
            //Console.WriteLine ("Reading a line of the output from the device...");

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

            FullDeviceOutput += output;

            ConsoleWriteSerialOutput(output);
            return(output);
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public void Run()
        {
            DevicesDirectory = Path.GetFullPath(DevicesDirectory);

            Console.WriteLine("Devices directory:");
            Console.WriteLine(DevicesDirectory);

            Console.WriteLine("Device Name: " + DeviceName);
            Console.WriteLine("MQTT Host: " + MqttHost);
            Console.WriteLine("MQTT Username: "******"MQTT Port: " + MqttPort);

            LoadDeviceList();

            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: " + DeviceName);
            Console.WriteLine("Serial port name: " + SerialPortName);

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

                Client = new SerialClient(port);

                EnsurePortIsOpen();

                SetupMQTT();

                // Wait until the first line arrives
                Client.ReadLine();

                var isRunning = true;
                while (isRunning)
                {
                    try {
                        RunLoop();

                        Thread.Sleep(10);
                    } catch (Exception ex) {
                        Console.WriteLine("An error occurred:");
                        Console.WriteLine(ex.ToString());
                        Console.WriteLine();
                        Console.WriteLine("Waiting for 30 seconds then retrying");

                        SendErrorEmail(ex, DeviceName, SmtpServer, EmailAddress);

                        Thread.Sleep(30 * 1000);

                        Run();
                    }
                }
            }
        }