示例#1
0
        public GaugeDriver(GaugeDevice device)
        {
            SerialNumber = device.SerialNumber;

            serialTransport = new SerialTransport()
            {
                CurrentSerialSettings = { PortName = device.PortName, BaudRate = BAUD_RATE, DtrEnable = false }
            };

            cmdMessenger = new CmdMessenger(serialTransport)
            {
                BoardType = BoardType.Bit16
            };

            cmdMessenger.StartListening();
        }
示例#2
0
        private static GaugeDevice DetectArduinoGaugeDevice(string portname)
        {
            SerialTransport serialTransport = new SerialTransport()
            {
                CurrentSerialSettings = { PortName = portname, BaudRate = BAUD_RATE, DtrEnable = false }
            };
            CmdMessenger cmdMessenger = new CmdMessenger(serialTransport)
            {
                BoardType = BoardType.Bit16
            };

            try
            {
                cmdMessenger.StartListening();

                var command = new SendCommand((int)Command.HandshakeRequest, (int)Command.HandshakeResponse, 1000);
                var handshakeResultCommand = cmdMessenger.SendCommand(command);

                if (handshakeResultCommand.Ok)
                {
                    // Read response
                    var software     = handshakeResultCommand.ReadStringArg();
                    var serialNumber = handshakeResultCommand.ReadStringArg();

                    if (software.Contains("ArduinoGauge"))
                    {
                        // Create ArduinoGaugeDevice
                        GaugeDevice device = new GaugeDevice()
                        {
                            PortName     = portname,
                            SerialNumber = serialNumber
                        };

                        return(device);
                    }
                    else
                    {
                        Debug.WriteLine("Connected to Arduino, but not an ArduinoGauge device.");
                        return(null);
                    }
                }
                else
                {
                    Debug.WriteLine("Handshake FAILED");
                    return(null);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return(null);
            }
            finally
            {
                cmdMessenger.StopListening();
                cmdMessenger.Dispose();
                cmdMessenger = null;
                serialTransport.Dispose();
                serialTransport = null;
            }
        }