/// <summary> /// Create a new SerialLcd object. /// </summary> /// <param name="config">TextDisplayConfig object defining the Lcd dimension (null will default to 16x2).</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(IIODevice device, SerialPortName port, TextDisplayConfig config = null, int baudRate = 9600, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One) { if (config == null) { // assume a 16x2 Lcd. DisplayConfig = new TextDisplayConfig() { Height = 2, Width = 16 }; } else { DisplayConfig = config; } comPort = device.CreateSerialPort(port, baudRate, dataBits, parity, 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); }
/// <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); }