Esempio n. 1
0
 /// <summary>
 ///     Create a new NMEA GPS object and attach to the specified serial port.
 /// </summary>
 /// <param name="port">Serial port attached to the GPS.</param>
 /// <param name="baudRate">Baud rate.</param>
 /// <param name="parity">Parity.</param>
 /// <param name="dataBits">Number of data bits.</param>
 /// <param name="stopBits">Number of stop bits.</param>
 public NMEA(string port, int baudRate, ParityType parity, int dataBits, NumberOfStopBits stopBits)
 {
     _gps = new SerialTextFile(port, baudRate, parity, dataBits, stopBits, "\r\n");
     _gps.OnLineReceived += _gps_OnLineReceived;
 }
 /// <summary>
 ///     Create a new SerialTextFile and attach the instance to the specfied serial port.
 /// </summary>
 /// <param name="port">Serial port name.</param>
 /// <param name="baudRate">Baud rate.</param>
 /// <param name="parity">Parity.</param>
 /// <param name="dataBits">Data bits.</param>
 /// <param name="stopBits">Stop bits.</param>
 /// <param name="endOfLine">Text indicating the end of a line of text.</param>
 public SerialTextFile(string port, int baudRate, ParityType parity, int dataBits, NumberOfStopBits stopBits,
                       string endOfLine)
 {
     _serialPort = new SerialPort(port, baudRate, parity, dataBits, stopBits);
     _endOfLine  = endOfLine;
     _serialPort.DataReceived += _serialPort_DataReceived;
 }
Esempio n. 3
0
        /// <summary>
        ///     Create a new SerialLCD object.
        /// </summary>
        /// <param name="config">TextDisplayConfig object defining the LCD dimension (null will default to 16x2).</param>
        /// <param name="port">Com port the display is connected to.</param>
        /// <param name="baudRate">Baud rate to use (default = 9600).</param>
        /// <param name="parity">Parity to use (default is None).</param>
        /// <param name="dataBits">Number of data bits (default is 8 data bits).</param>
        /// <param name="stopBits">Number of stop bits (default is one stop bit).</param>
        public SerialLCD(TextDisplayConfig config = null, string port             = "COM1", int baudRate = 9600,
                         ParityType parity        = ParityType.None, int dataBits = 8, NumberOfStopBits stopBits = NumberOfStopBits.One)
        {
            if (config == null)
            {
                // assume a 16x2 LCD.
                DisplayConfig = new TextDisplayConfig()
                {
                    Height = 2, Width = 16
                };
            }
            else
            {
                DisplayConfig = config;
            }

            _comPort = new SerialPort(port, baudRate, parity, dataBits, stopBits);
            _comPort.Open();

            // configure the LCD controller for the appropriate screen size
            byte lines      = 0;
            byte characters = 0;

            switch (DisplayConfig.Width)
            {
            case 16:
                characters = (byte)LCDDimensions.Characters16Wide;
                break;

            case 20:
                characters = (byte)LCDDimensions.Characters20Wide;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(config.Width), "Display width should be 16 or 20.");
            }
            switch (DisplayConfig.Height)
            {
            case 2:
                lines = (byte)LCDDimensions.Lines2;
                break;

            case 4:
                lines = (byte)LCDDimensions.Lines4;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(config.Height), "Display height should be 2 or 4 lines.");
            }
            Send(new[] { ConfigurationCommandCharacter, characters, ConfigurationCommandCharacter, lines });
            Thread.Sleep(10);
        }