/// <summary>
        /// Creates a new Modbus RTU interface using an existing and fully initialized <see cref="UartController"/>.
        /// This interface can be used with <see cref="ModbusMaster"/> or <see cref="ModbusDevice"/>.
        /// </summary>
        /// <param name="serial">Fully initialized <see cref="UartController"/>.</param>
        /// <param name="maxDataLength">Maximum number of data bytes</param>
        public ModbusRtuInterface(UartController serial, int baudRate, int dataBits, UartStopBitCount stopBits, UartParity parity, short maxDataLength = 252)
        {
            this.dataBits = dataBits;
            this.baudRate = baudRate;
            this.stopBits = stopBits;
            this.parity   = parity;

            if (this.dataBits < 8)
            {
                throw new ArgumentException("serial.DataBits must be >= 8");
            }

            this.serial            = serial;
            this.MaxDataLength     = maxDataLength;
            this.MaxTelegramLength = (short)(maxDataLength + 4);

            // calc char length in µs
            if (this.baudRate > 19200)
            {
                // use a fixed value for high baudrates (recommended by Modbus spec.)
                this.halfCharLength = 500;
            }
            else
            {
                var bitCnt = (short)this.dataBits;
                switch (this.stopBits)
                {
                case UartStopBitCount.One:
                    ++bitCnt;
                    break;

                case UartStopBitCount.OnePointFive:
                case UartStopBitCount.Two:
                    bitCnt += 2;
                    break;
                }
                if (this.parity != UartParity.None)
                {
                    ++bitCnt;
                }
                this.halfCharLength = (short)((bitCnt * 1000 * 10000) / this.baudRate) >> 1;
            }
        }
Beispiel #2
0
 public extern void SetActiveSettings(int baudRate, int dataBits, UartParity parity, UartStopBitCount stopBits, UartHandshake handshaking);
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FTDIClick" /> class.
 /// </summary>
 /// <param name="socket">The socket on which the FTDIClick module is plugged on MikroBus.Net board</param>
 /// <param name="baudRate">The baud rate.</param>
 /// <param name="parity">The parity.</param>
 /// <param name="dataBits">The data bits.</param>
 /// <param name="stopBits">The stop bits.</param>
 public FTDIClick(Hardware.Socket socket, Int32 baudRate = 9600, UartParity parity = UartParity.None, Int32 dataBits = 8, UartStopBitCount stopBits = UartStopBitCount.One)
 {
     _sp = UartController.FromName(socket.ComPort);
     _sp.SetActiveSettings(baudRate, dataBits, parity, stopBits, UartHandshake.None);
     _sp.Enable();
 }
        public Result Initialise(string serialPortId, int baudRate, UartParity serialParity, int dataBits, UartStopBitCount stopBitCount)
        {
            Result result;

            if ((serialPortId == null) || (serialPortId == ""))
            {
                throw new ArgumentException("Invalid SerialPortId", "serialPortId");
            }
            if ((baudRate < BaudRateMinimum) || (baudRate > BaudRateMaximum))
            {
                throw new ArgumentException("Invalid BaudRate", "baudRate");
            }

            serialDevice = UartController.FromName(serialPortId);

            // set parameters
            serialDevice.SetActiveSettings(new UartSetting()
            {
                BaudRate    = baudRate,
                Parity      = serialParity,
                StopBits    = stopBitCount,
                Handshaking = UartHandshake.None,
                DataBits    = dataBits
            });

            serialDevice.Enable();

            atCommandExpectedResponse = string.Empty;

            serialDevice.DataReceived += SerialDevice_DataReceived;

            // Set the Working mode to LoRaWAN
#if DIAGNOSTICS
            Debug.WriteLine($" {DateTime.UtcNow:hh:mm:ss} lora:work_mode LoRaWAN");
#endif
            result = SendCommand("Initialization OK", "at+set_config=lora:work_mode:0", CommandTimeoutDefault);
            if (result != Result.Success)
            {
#if DIAGNOSTICS
                Debug.WriteLine($" {DateTime.UtcNow:hh:mm:ss} lora:work_mode failed {result}");
#endif
                return(result);
            }

            return(Result.Success);
        }
        /// <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 UartControllerName, int baudRate, UartParity parity, int dataBits, UartStopBitCount stopBits,
                              string endOfLine)
        {
            serialPort = UartController.FromName(UartControllerName);
            var uartSetting = new UartSetting()
            {
                BaudRate    = baudRate,
                DataBits    = dataBits,
                Parity      = parity,
                StopBits    = stopBits,
                Handshaking = UartHandshake.None,
            };

            serialPort.SetActiveSettings(uartSetting);

            serialPort.Enable();
            IsEnabled = true;
            LINE_END  = endOfLine;
            serialPort.DataReceived += SerialPort_DataReceived;
        }
Beispiel #6
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="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(string portName, TextDisplayConfig config = null, int baudRate = 9600,
                         UartParity parity = UartParity.None, int dataBits = 8, UartStopBitCount stopBits = UartStopBitCount.One)
        {
            if (config == null)
            {
                // assume a 16x2 Lcd.
                DisplayConfig = new TextDisplayConfig()
                {
                    Height = 2, Width = 16
                };
            }
            else
            {
                DisplayConfig = config;
            }
            var myUart = UartController.FromName(portName);

            var uartSetting = new UartSetting()
            {
                BaudRate    = baudRate,
                DataBits    = dataBits,
                Parity      = parity,
                StopBits    = stopBits,
                Handshaking = UartHandshake.None,
            };

            myUart.SetActiveSettings(uartSetting);

            myUart.Enable();

            // 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);
        }
Beispiel #7
0
        public Result Initialise(string serialPortId, int baudRate, UartParity serialParity, int dataBits, UartStopBitCount stopBitCount)
        {
            if ((serialPortId == null) || (serialPortId == ""))
            {
                throw new ArgumentException("Invalid SerialPortId", "serialPortId");
            }
            if ((baudRate < BaudRateMinimum) || (baudRate > BaudRateMaximum))
            {
                throw new ArgumentException("Invalid BaudRate", "baudRate");
            }

            serialDevice = UartController.FromName(serialPortId);

            // set parameters
            serialDevice.SetActiveSettings(new UartSetting()
            {
                BaudRate    = baudRate,
                Parity      = serialParity,
                StopBits    = stopBitCount,
                Handshaking = UartHandshake.None,
                DataBits    = dataBits
            });

            serialDevice.Enable();

            atCommandExpectedResponse = string.Empty;

            serialDevice.DataReceived += SerialDevice_DataReceived;

            // Ignoring the return from this is intentional
            this.SendCommand("+LOWPOWER: WAKEUP", "AT+LOWPOWER: WAKEUP", SendTimeoutMinimum);

            return(Result.Success);
        }
        /// <summary>
        ///     Default constructor
        /// </summary>
        /// <param name="socket">The socket in which the USB UART click board is inserted into.</param>
        /// <param name="baudRate">The baud rate for serial commumications.</param>
        /// <param name="dataBits"></param>
        /// <param name="parity"></param>
        /// <param name="stopBitCount"></param>
        /// <param name="handShake"></param>
        public USBUARTClick(Hardware.Socket socket, BaudRate baudRate, Int32 dataBits = 8, UartParity parity = UartParity.None, UartStopBitCount stopBitCount = UartStopBitCount.One, UartHandshake handShake = UartHandshake.None)
        {
            _serial = UartController.FromName(socket.ComPort);
            _serial.SetActiveSettings((Int32)baudRate, dataBits, parity, stopBitCount, handShake);
            _serial.Enable();

            _serial.DataReceived  += Serial_DataReceived;
            _serial.ErrorReceived += Serial_ErrorReceived;

            _simpleSerial = new SimpleSerial(_serial);

            _powerPin = GpioController.GetDefault().OpenPin(socket.PwmPin);
            _powerPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            _powerPin.ValueChanged += PowerPin_ValueChanged;

            _sleepPin = GpioController.GetDefault().OpenPin(socket.Cs);
            _sleepPin.SetDriveMode(GpioPinDriveMode.Input);
            _sleepPin.ValueChanged += SleepPin_ValueChanged;
        }
Beispiel #9
0
 public extern void SetActiveSettings(int baudRate, int dataBits, UartParity parity, UartStopBitCount stopBits, UartHandshake handshaking, bool enableDePin, bool invertTxPolarity, bool invertRxPolarity, bool invertBinaryData, bool swapTxRxPin, bool invertDePolarity);