Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /// <summary>Initializes a new instance of the <see cref="GPS2Click" /> class.</summary>
        /// <param name="socket">The socket on which the module is plugged</param>
        public GPS2Click(Hardware.Socket socket)
        {
            _sl = new SerialListener('$', '\n');
            _sl.MessageAvailable += Sl_MessageAvailable;

            _wakeUp = GpioController.GetDefault().OpenPin(socket.AnPin);
            _wakeUp.SetDriveMode(GpioPinDriveMode.Input);

            _onOff = GpioController.GetDefault().OpenPin(socket.PwmPin);
            _onOff.SetDriveMode(GpioPinDriveMode.Output);
            // Force full mode
            _onOff.Write(GpioPinValue.Low);
            Thread.Sleep(100);
            _onOff.Write(GpioPinValue.High);
            Thread.Sleep(100);
            _onOff.Write(GpioPinValue.Low);
            Thread.Sleep(1);
            _onOff.Write(GpioPinValue.High);
            Thread.Sleep(100);

            _gps2 = UartController.FromName(socket.ComPort); // Socket #1
            _gps2.SetActiveSettings(new UartSetting()
            {
                BaudRate = 4800, DataBits = 8, Parity = UartParity.None, StopBits = UartStopBitCount.One, Handshaking = UartHandshake.None
            });
            _gps2.DataReceived += Gps2_DataReceived;
            _gps2.Enable();
        }
Exemplo n.º 3
0
        // TODO: if we want to make this public then we're going to have to add
        // a bunch of checks around baud rate, 8n1, etc.
        protected Mt3339(string serialPortControllerName)
        {
            serialPort = UartController.FromName(serialPortControllerName);

            var uartSetting = new UartSetting()
            {
                BaudRate    = 115200,
                DataBits    = 8,
                Parity      = UartParity.None,
                StopBits    = UartStopBitCount.One,
                Handshaking = UartHandshake.None,
            };

            serialPort.SetActiveSettings(uartSetting);

            serialPort.Enable();

            serialPort.DataReceived += SerialPort_DataReceived;

            //this.serialPort = serialPort;

            //this.serialPort.MessageReceived += SerialPort_MessageReceived;

            Init();
        }
Exemplo n.º 4
0
        protected void OpenComPort(string port = "COM1", int baudRate = 38400)
        {
            CloseComPort();

            //ToDo  serialPort = new SerialPort(port, baudRate, Parity.None, 8, StopBits.One);

            //serialPort.ReadTimeout = 10 * 2;
            vc0706.ReadTimeout = 10 * 2;
            // serialPort.WriteTimeout = 10 * 2;

            //serialPort.Enable();
            serialPort = UartController.FromName(port);
            var uartSetting = new UartSetting()
            {
                BaudRate    = baudRate,
                DataBits    = 8,
                Parity      = UartParity.None,
                StopBits    = UartStopBitCount.One,
                Handshaking = UartHandshake.None,
            };

            vc0706.BaudRate = baudRate;
            serialPort.SetActiveSettings(uartSetting);

            serialPort.Enable();
        }
Exemplo n.º 5
0
        public CommunicationsBus(string uartPort, int baudRate = 9600)
        {
            this.disposed    = false;
            this.commandFile = null;
            this.protocal    = CommunicationsProtocal.Uart;
            this.uartPort    = uartPort;

            this.serialPort = UartController.FromName(uartPort);

            var uartSetting = new UartSetting()
            {
                BaudRate    = baudRate,
                DataBits    = 8,
                Parity      = UartParity.None,
                StopBits    = UartStopBitCount.One,
                Handshaking = UartHandshake.None //UartHandshake.RequestToSend
            };

            this.serialPort.SetActiveSettings(uartSetting);

            this.serialPort.WriteBufferSize = 16 * 1024;
            this.serialPort.ReadBufferSize  = 16 * 1024;

            this.serialPort.Enable();
        }
Exemplo n.º 6
0
        static void Main()
        {
            RtcController rtc = RtcController.GetDefault();

            try
            {
                var t = rtc.GetTime();
                if (t.Year == 1980)
                {
                    rtc.SetTime(RtcDateTime.FromDateTime(new DateTime(2018, 10, 12, 17, 51, 00)));
                }
            }
            catch (Exception)
            {
                rtc.SetTime(RtcDateTime.FromDateTime(new DateTime(2018, 10, 11, 11, 51, 00)));
            }

            //string COM = "GHIElectronics.TinyCLR.NativeApis.STM32F7.UartController\\2";
            UartController ser = UartController.FromName(STM32F7.UartPort.Usart1); // DISCO-F769

            //UartController ser = UartController.FromName(STM32F7.UartPort.Usart3); // NUCLEO-F767
            ser.SetActiveSettings(38400, 8, UartParity.None, UartStopBitCount.One, UartHandshake.None);
            Debug.WriteLine("Starting Program ....");
            BlinkLed blink = new BlinkLed(STM32F7.GpioPin.PJ5, 500); // Disco 769
            //BlinkLed blink = new BlinkLed(STM32F7.GpioPin.PB7, 500); // Nucleo 144 - F767
            Thread run = new Thread(blink.Blink);

            run.Start();
            var r = new HeapAllocTest(10);

            r.Allocate();
            //var d = new DisplayTest();
            string sallocated = "";
            //Debug.WriteLine("Allocated TOTAL Memory:" + GC.GetTotalMemory(false).ToString() + " bytes!");
            //d.DrawSomething("Test String...", 50,50);

            int x = 10, y = 10;

            //SDTest sd = new SDTest();

            sallocated = "Memory:" + GC.GetTotalMemory(true).ToString() + " bytes!";
            while (true)
            {
                x += 2;
                y += 2;
                if (x > (800 - 160) || y > 480 - 160)
                {
                    x = 10;
                    y = 10;
                    GC.Collect();
                }
                Thread.Sleep(1000);
                //d.DrawSomething(sallocated, x, y);
                var    dt = rtc.Now;
                byte[] b  = System.Text.Encoding.UTF8.GetBytes("Program Running !! .." + dt.ToString("dd/MM/yyyy HH:mm:ss") + "\r\n");
                ser.Write(b);
                ser.Flush();
            }
        }
Exemplo n.º 7
0
        private void ThreadTest()
        {
            this.isRuning = true;


            using (var uart1 = UartController.FromName(SC20260.UartPort.Uart5)) {
                var setting = new UartSetting()
                {
                    BaudRate = 115200
                };

                uart1.SetActiveSettings(setting);
                uart1.Enable();

                var totalReceived = 0;
                var totalSent     = 0;

                while (this.isRuning)
                {
                    this.UpdateStatusText("Total received: " + totalReceived, true);
                    this.UpdateStatusText("Total sent: " + totalSent, false);
                    this.UpdateStatusText(WaitForMessage, false);

                    while (uart1.BytesToRead == 0)
                    {
                        Thread.Sleep(10);
                    }

                    var byteToRead = uart1.BytesToRead > uart1.ReadBufferSize ? uart1.ReadBufferSize : uart1.BytesToRead;

                    var read = new byte[byteToRead];


                    this.UpdateStatusText("Receiving... " + byteToRead + " byte(s)", false);
                    totalReceived += uart1.Read(read);



                    for (var i = 0; i < read.Length; i++)
                    {
                        var write = new byte[1] {
                            (byte)(read[i] + 1)
                        };
                        totalSent += uart1.Write(write);

                        uart1.Flush();
                    }


                    this.UpdateStatusText("Writing back... " + byteToRead + " byte(s)", false);
                }
            }


            this.isRuning = false;

            return;
        }
Exemplo n.º 8
0
        public static void Main()
        {
            Debug.WriteLine("devMobile.IoT.Rak811.ShieldSerial starting");

            try
            {
                serialDevice = UartController.FromName(SerialPortId);

                serialDevice.SetActiveSettings(new UartSetting()
                {
                    BaudRate    = 9600,
                    Parity      = UartParity.None,
                    StopBits    = UartStopBitCount.One,
                    Handshaking = UartHandshake.None,
                    DataBits    = 8
                });

                serialDevice.Enable();

#if SERIAL_ASYNC_READ
                serialDevice.DataReceived += SerialDevice_DataReceived;
#endif

                while (true)
                {
                    byte[] txBuffer = UTF8Encoding.UTF8.GetBytes(ATCommand);

                    int txByteCount = serialDevice.Write(txBuffer);
                    Debug.WriteLine($"TX: {txByteCount} bytes");

#if SERIAL_SYNC_READ
                    while (serialDevice.BytesToWrite > 0)
                    {
                        Debug.WriteLine($" BytesToWrite {serialDevice.BytesToWrite}");
                        Thread.Sleep(100);
                    }

                    int rxByteCount = serialDevice.BytesToRead;
                    if (rxByteCount > 0)
                    {
                        byte[] rxBuffer = new byte[rxByteCount];

                        serialDevice.Read(rxBuffer);

                        Debug.WriteLine($"RX sync:{rxByteCount} bytes read");
                        String response = UTF8Encoding.UTF8.GetString(rxBuffer);
                        Debug.WriteLine($"RX sync:{response}");
                    }
#endif

                    Thread.Sleep(20000);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Exemplo n.º 9
0
        private bool DoTestUart()
        {
            this.UpdateStatusText("Testing VCOM Uart5.", true);

            this.UpdateStatusText(" - Connect VCOM Uart5 to PC.", false);
            this.UpdateStatusText(" - Open Tera Term. Baudrate: 9600, Data: 8, Parity: None, StopBit: One.", false);
            this.UpdateStatusText(" - Type 'A' or 'a'", false);
            this.UpdateStatusText(" - The test is waiting any character on Tera Term screen.", false);

            var result = false;

            using (var uart5 = UartController.FromName(SC20260.UartPort.Uart5)) {
                var setting = new UartSetting()
                {
                    BaudRate = 9600
                };

                uart5.SetActiveSettings(setting);
                uart5.Enable();

                this.AddNextButton();

                while (this.doNext == false && this.isRunning)
                {
                    if (uart5.BytesToRead == 0)
                    {
                        Thread.Sleep(100);

                        uart5.Write(new byte[] { (byte)('a') });
                        continue;
                    }

                    var byteToRead = uart5.BytesToRead > uart5.ReadBufferSize ? uart5.ReadBufferSize : uart5.BytesToRead;

                    var read = new byte[byteToRead];

                    uart5.Read(read);

                    for (var i = 0; i < read.Length; i++)
                    {
                        if (read[i] == 'a')
                        {
                            result = true;
                            break;
                        }
                    }

                    if (result == true)
                    {
                        break;
                    }
                }
            }

            this.RemoveNextButton();

            return(result);
        }
Exemplo n.º 10
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(new UartSetting()
     {
         BaudRate = baudRate, DataBits = dataBits, Parity = parity, StopBits = stopBits, Handshaking = UartHandshake.None
     });
     _sp.Enable();
 }
Exemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpeakUpClick"/> class.
 /// </summary>
 /// <param name="socket">The socket on which the SpeakUpClick module is plugged on MikroBus.Net board</param>
 public SpeakUpClick(Hardware.Socket socket)
 {
     _sp = UartController.FromName(socket.ComPort);
     _sp.SetActiveSettings(new UartSetting()
     {
         BaudRate = 115200, DataBits = 8, Parity = UartParity.None, StopBits = UartStopBitCount.One, Handshaking = UartHandshake.None
     });
     _sp.Enable();
 }
Exemplo n.º 12
0
        public MusicalInstrumentShield(string uart)
        {
            this.uart = UartController.FromName(uart);
            this.uart.SetActiveSettings((int)(31250), 8,
                                        UartParity.None,
                                        UartStopBitCount.One,
                                        UartHandshake.None);

            this.uart.Enable();
        }
Exemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DevantechLcd03"/> class using serial communication (UART)
 /// </summary>
 /// <param name="socket">The socket on MBN board where the Lcd is connected</param>
 public DevantechLcd03(Hardware.Socket socket)
 {
     try
     {
         _lcdSerial = UartController.FromName(socket.ComPort);
         _lcdSerial.SetActiveSettings(9600, 8, UartParity.None, UartStopBitCount.Two, UartHandshake.None);
         _lcdSerial.Enable();
         _isUart = true;
         Init();
     }
     catch { }
 }
Exemplo n.º 14
0
        /// <summary>Initializes a new instance of the <see cref="GNSSClick" /> class.</summary>
        /// <param name="socket">The socket on which the module is plugged</param>
        public GNSSClick(Hardware.Socket socket)
        {
            _sl = new SerialListener('$', '\n');
            _sl.MessageAvailable += Sl_MessageAvailable;

            _gnss = UartController.FromName(socket.ComPort);
            _gnss.SetActiveSettings(9600, 8, UartParity.None, UartStopBitCount.One, UartHandshake.None);
            _gnss.DataReceived += Gnss_DataReceived;
            _gnss.Enable();
            // It seems that we need to send a command so that the module starts sending its data.
            // So we send a command to request firmware version
            SendCommand("PMTK605");
        }
Exemplo n.º 15
0
        /// <summary>Initializes a new instance of the <see cref="Gnss4Click" /> class.</summary>
        /// <param name="socket">The socket on which the module is plugged</param>
        public Gnss4Click(Hardware.Socket socket)
        {
            _sl = new SerialListener('$', '\n');
            _sl.MessageAvailable += Sl_MessageAvailable;

            _gnss = UartController.FromName(socket.ComPort);
            _gnss.SetActiveSettings(new UartSetting()
            {
                BaudRate = 9600, DataBits = 8, Parity = UartParity.None, StopBits = UartStopBitCount.One, Handshaking = UartHandshake.None
            });
            _gnss.DataReceived += Gnss_DataReceived;
            _gnss.Enable();
        }
Exemplo n.º 16
0
        public CommunicationsBus(string uartPort, int baudRate = 115200)
        {
            this.disposed    = false;
            this.commandFile = null;
            this.protocal    = CommunicationsProtocal.Uart;

            this.serialPort = UartController.FromName(uartPort);

            this.serialPort.SetActiveSettings(baudRate, 8, UartParity.None, UartStopBitCount.One, UartHandshake.None);

            this.serialPort.Enable();

            this.serialPort.DataReceived += this.SerialPort_DataReceived;
        }
Exemplo n.º 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DevantechLcd03"/> class using serial communication (UART)
 /// </summary>
 /// <param name="socket">The socket on MBN board where the Lcd is connected</param>
 public DevantechLcd03(Hardware.Socket socket)
 {
     try
     {
         _lcdSerial = UartController.FromName(socket.ComPort);
         _lcdSerial.SetActiveSettings(new UartSetting()
         {
             BaudRate = 9600, DataBits = 8, Parity = UartParity.None, StopBits = UartStopBitCount.Two, Handshaking = UartHandshake.None
         });
         _lcdSerial.Enable();
         _isUart = true;
         Init();
     }
     catch { }
 }
        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);
        }
Exemplo n.º 19
0
        /// <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;
        }
Exemplo n.º 20
0
        /// <summary>
        /// Create an IDxxLA RFID reader
        /// </summary>
        /// <param name="device">Device to use</param>
        /// <param name="serialPortName">Port name to use</param>
        public IDxxLA(string serialPortName)

        {
            var myUart = UartController.FromName(serialPortName);

            var uartSetting = new UartSetting()
            {
                BaudRate    = 115200,
                DataBits    = 8,
                Parity      = UartParity.None,
                StopBits    = UartStopBitCount.One,
                Handshaking = UartHandshake.None,
            };

            myUart.SetActiveSettings(uartSetting);


            Setup(myUart);
            //device.CreateSerialPort(serialPortName, BaudRate, DataBits))
        }
Exemplo n.º 21
0
        static void Main()
        {
            var serial = UartController.FromName("GHIElectronics.TinyCLR.NativeApis.STM32H7.UartController\\2");

            var resetPin = 3 * 16 + 15;

            var gps = new Gps(serial, resetPin);

            gps.InvalidPositionReceived += Gps_InvalidPositionReceived;
            gps.NmeaSentenceReceived    += Gps_NmeaSentenceReceived;
            gps.PositionReceived        += Gps_PositionReceived;

            Debug.WriteLine("staring");

            gps.Enabled = true; // Start Gps

            Debug.WriteLine("Enabled");

            Thread.Sleep(-1);
        }
Exemplo n.º 22
0
        /// <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;
        }
Exemplo n.º 23
0
        public Vc0706(string serialPortName, int baudRate)
        {
            //serialPort = device.CreateSerialPort(serialPortName, baudRate);
            serialPort = UartController.FromName(serialPortName);

            var uartSetting = new UartSetting()
            {
                BaudRate    = baudRate,
                DataBits    = 8,
                Parity      = UartParity.None,
                StopBits    = UartStopBitCount.One,
                Handshaking = UartHandshake.None,
            };

            serialPort.SetActiveSettings(uartSetting);

            serialPort.Enable();

            vc0706          = new Vc0706Core();
            vc0706.BaudRate = baudRate;
            vc0706.PortName = serialPortName;
        }
Exemplo n.º 24
0
        /// <summary>
        /// Construct Bluetooth module
        /// </summary>
        /// <param name="pinStatus">GpioPin Pin 3 of U Socket</param>
        /// <param name="pinReset">GpioPin Pin 6 of U Socket</param>
        /// <param name="uartId">Uart Id of U Socket</param>
        public Bluetooth(int pinStatus, int pinReset, string uartId)
        {
            // This finds the Socket instance from the user-specified socket number. This will generate user-friendly error messages if the socket is invalid. If there is more than one socket on this
            // module, then instead of "null" for the last parameter, put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
            // Socket socket = Socket.GetSocket(socketNumber, true, this, null);
            rxBuffer = new byte[128];
            //this.reset = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Six, false, this);
            _reset = GpioController.GetDefault().OpenPin(pinReset);
            _reset.SetDriveMode(GpioPinDriveMode.Output);
            _reset.Write(GpioPinValue.Low);

            // this.statusInt = GTI.InterruptInputFactory.Create(socket, Socket.Pin.Three, GTI.GlitchFilterMode.Off, GTI.ResistorMode.Disabled, GTI.InterruptMode.RisingAndFallingEdge, this);
            _statusInt = GpioController.GetDefault().OpenPin(pinStatus);
            _statusInt.SetDriveMode(GpioPinDriveMode.Input);
            _statusInt.DebounceTimeout = TimeSpan.FromMilliseconds(1);

            // this.serialPort = GTI.SerialFactory.Create(socket, 38400, GTI.SerialParity.None, GTI.SerialStopBits.One, 8, GTI.HardwareFlowControl.NotRequired, this);
            _serialPort = UartController.FromName(uartId);
            _serialPort.SetActiveSettings(38400, 8, UartParity.None, UartStopBitCount.One, UartHandshake.None);
            _serialPort.Enable();

            //_serialPort.DataReceived += _serialPort_DataReceived;

            //this.statusInt.Interrupt += GTI.InterruptInputFactory.Create.InterruptEventHandler(statusInt_Interrupt);
            //           this.serialPort.ReadTimeout = Timeout.Infinite;
            //           this.serialPort.Open();

            //_dataReader = new DataReader(_serialPort.InputStream);
            //_dataWriter = new DataWriter(_serialPort.OutputStream);

            Thread.Sleep(5);
            _reset.Write(GpioPinValue.High);

            _readerThread = new Thread(RunReaderThread);
            _readerThread.Start();
            Thread.Sleep(500);
        }
Exemplo n.º 25
0
        public static void Main()
        {
            // Get the display ready
            var spi    = SpiController.FromName(G80.SpiBus.Spi2);
            var gpio   = GpioController.GetDefault();
            var st7735 = new ST7735Controller(spi.GetDevice(ST7735Controller.GetConnectionSettings(SpiChipSelectType.Gpio, G80.GpioPin.PD10)), gpio.OpenPin(G80.GpioPin.PE10), gpio.OpenPin(G80.GpioPin.PE12));

            st7735.SetDataAccessControl(true, true, false, false);
            st7735.Enable();
            var disp = DisplayController.FromProvider(st7735);

            disp.SetConfiguration(new SpiDisplayControllerSettings {
                Width = 160, Height = 128
            });
            var bl = gpio.OpenPin(G80.GpioPin.PC7);

            bl.Write(GpioPinValue.High);
            bl.SetDriveMode(GpioPinDriveMode.Output);
            var hdc = GraphicsManager.RegisterDrawTarget(new DrawTarget(disp));

            screen = Graphics.FromHdc(hdc);
            screen.Clear(Color.Black);

            var btn1 = gpio.OpenPin(G80.GpioPin.PE0);

            btn1.SetDriveMode(GpioPinDriveMode.InputPullUp);
            btn1.ValueChanged += Btn1_ValueChanged;

            // Serial port
            serial = UartController.FromName(G80.UartPort.Usart1);
            serial.SetActiveSettings(9600, 8, UartParity.None, UartStopBitCount.One, UartHandshake.None);
            serial.Enable();
            serial.DataReceived += Serial_DataReceived;

            Thread.Sleep(Timeout.Infinite);
        }
Exemplo n.º 26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpeakUpClick"/> class.
 /// </summary>
 /// <param name="socket">The socket on which the SpeakUpClick module is plugged on MikroBus.Net board</param>
 public SpeakUpClick(Hardware.Socket socket)
 {
     _sp = UartController.FromName(socket.ComPort);
     _sp.SetActiveSettings(115200, 8, UartParity.None, UartStopBitCount.One, UartHandshake.None);
     _sp.Enable();
 }
Exemplo n.º 27
0
        private const string payload = "01020304"; // AQIDBA==
        //private const string payload = "04030201"; // BAMCAQ==

        public static void Main()
        {
            UartController serialDevice;
            int            txByteCount;
            int            rxByteCount;

            Debug.WriteLine("devMobile.IoT.SeeedE5.NetworkJoinOTAA starting");

            try
            {
                serialDevice = UartController.FromName(SerialPortId);

                serialDevice.SetActiveSettings(new UartSetting()
                {
                    BaudRate    = 9600,
                    Parity      = UartParity.None,
                    StopBits    = UartStopBitCount.One,
                    Handshaking = UartHandshake.None,
                    DataBits    = 8
                });

                serialDevice.Enable();

                // Set the Region to AS923
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes("AT+DR=AS923\r\n"));
                Debug.WriteLine($"TX: DR {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the Join mode
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes("AT+MODE=LWOTAA\r\n"));
                Debug.WriteLine($"TX: MODE {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the appEUI
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes($"AT+ID=AppEui,\"{AppEui}\"\r\n"));
                Debug.WriteLine($"TX: ID=AppEui {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the appKey
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes($"AT+KEY=APPKEY,{AppKey}\r\n"));
                Debug.WriteLine($"TX: KEY=APPKEY {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the PORT
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes($"AT+PORT={messagePort}\r\n"));
                Debug.WriteLine($"TX: PORT {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Join the network
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes("AT+JOIN\r\n"));
                Debug.WriteLine($"TX: JOIN {txByteCount} bytes");
                Thread.Sleep(10000);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                while (true)
                {
                    // Unconfirmed message
                    txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes($"AT+MSGHEX=\"{payload}\"\r\n"));
                    Debug.WriteLine($"TX: MSGHEX {txByteCount} bytes");

                    // Confirmed message
                    //txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes($"AT+CMSGHEX=\"{payload}\"\r\n"));
                    //Debug.WriteLine($"TX: CMSGHEX {txByteCount} bytes");

                    Thread.Sleep(10000);

                    // Read the response
                    rxByteCount = serialDevice.BytesToRead;
                    if (rxByteCount > 0)
                    {
                        byte[] rxBuffer = new byte[rxByteCount];
                        serialDevice.Read(rxBuffer);
                        Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                    }

                    Thread.Sleep(30000);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Exemplo n.º 28
0
        // In order to create a truly robust long-running solution, you must combine the code below
        //   with proper use of a Watchdog Timer and exception handling on the wifi calls.
        //
        // Hardware note: It has been our experience that to work at 115200 with the ESP8266 and NETMF, you need a 1024 byte serial buffer.
        //   Smaller serial buffers may result in portions of incoming TCP traffic being dropped, which can also break the protocol processing
        //   and result in hangs.

        static void Main()
        {
            _rfPower = GpioController.GetDefault().OpenPin(5);
            _rfPower.SetDriveMode(GpioPinDriveMode.Output);
            _rfPower.Write(GpioPinValue.High);

            _userLed = GpioController.GetDefault().OpenPin(45);
            _userLed.SetDriveMode(GpioPinDriveMode.Output);

            var port = UartController.FromName(SC20100.UartPort.Uart8);

            port.SetActiveSettings(115200, 8, UartParity.None, UartStopBitCount.One, UartHandshake.None);

            var wifi = new Esp8266WifiDevice(port, _rfPower, null);

            // Enable echoing of commands
            wifi.EnableDebugOutput = false;


            Debug.WriteLine("--------------------------------");
            Debug.WriteLine("----Esp 8266 Wifi TinyClr 2 ----");
            Debug.WriteLine("--------------------------------");
            wifi.Connect("valoninet", "valoni1234567");


            Debug.WriteLine("Station IP address : " + wifi.StationIPAddress.ToString());
            Debug.WriteLine("Station MAC address : " + wifi.StationMacAddress);
            Debug.WriteLine("Station Gateway address : " + wifi.StationGateway.ToString());
            Debug.WriteLine("Station netmask : " + wifi.StationNetmask.ToString());
            Debug.WriteLine("------------------------");

            Thread.Sleep(1000);

            var sntp = new SntpClient(wifi, "time1.google.com");

            sntp.SetTime();

            int iCounter = 0;

            bool state = true;

            while (true)
            {
                state = !state;

                if (state)
                {
                    _userLed.Write(GpioPinValue.High);
                }
                else
                {
                    _userLed.Write(GpioPinValue.Low);
                }

                ++iCounter;
                if (iCounter % 10 == 0)
                {
                    Debug.WriteLine("Current UTC time : " + DateTime.UtcNow);
                    Debug.WriteLine("------------------------");
                }
                // Every 15 seconds

                Thread.Sleep(500);
            }
        }
Exemplo n.º 29
0
        private const string payload   = "48656c6c6f204c6f526157414e"; // Hello LoRaWAN

        public static void Main()
        {
            UartController serialDevice;
            int            txByteCount;
            int            rxByteCount;

            Debug.WriteLine("devMobile.IoT.Rak811.NetworkJoinOTAA starting");

            try
            {
                serialDevice = UartController.FromName(SerialPortId);

                serialDevice.SetActiveSettings(new UartSetting()
                {
                    BaudRate    = 9600,
                    Parity      = UartParity.None,
                    StopBits    = UartStopBitCount.One,
                    Handshaking = UartHandshake.None,
                    DataBits    = 8
                });

                serialDevice.Enable();

                // clear out the RX buffer
                rxByteCount = serialDevice.BytesToRead;
                while (rxByteCount > 0)
                {
                    byte[] rxBuffer0 = new byte[rxByteCount];

                    serialDevice.Read(rxBuffer0);
                    if (rxByteCount > 0)
                    {
                        serialDevice.Read(rxBuffer0);

                        Debug.WriteLine($"RX0 :{rxByteCount} bytes read");
                        String response = UTF8Encoding.UTF8.GetString(rxBuffer0);
                        Debug.WriteLine($"RX0 :{response}");
                    }
                }

                // Set the Working mode to LoRaWAN
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes("at+set_config=lora:work_mode:0\r\n"));
                Debug.WriteLine($"TX: work mode {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the Region to AS923
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes("at+set_config=lora:region:AS923\r\n"));
                Debug.WriteLine($"TX: region {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the JoinMode
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes("at+set_config=lora:join_mode:0\r\n"));
                Debug.WriteLine($"TX: join_mode {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the appEUI
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes($"at+set_config=lora:app_eui:{AppEui}\r\n"));
                Debug.WriteLine($"TX: app_eui {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the appKey
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes($"at+set_config=lora:app_key:{AppKey}\r\n"));
                Debug.WriteLine($"TX: app_key {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Set the Confirm flag
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes("at+set_config=lora:confirm:0\r\n"));
                Debug.WriteLine($"TX: confirm {txByteCount} bytes");
                Thread.Sleep(500);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                // Join the network
                txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes("at+join\r\n"));
                Debug.WriteLine($"TX: join {txByteCount} bytes");
                Thread.Sleep(10000);

                // Read the response
                rxByteCount = serialDevice.BytesToRead;
                if (rxByteCount > 0)
                {
                    byte[] rxBuffer = new byte[rxByteCount];
                    serialDevice.Read(rxBuffer);
                    Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                }

                while (true)
                {
                    txByteCount = serialDevice.Write(UTF8Encoding.UTF8.GetBytes($"at+send=lora:{messagePort}:{payload}\r\n"));
                    Debug.WriteLine($"TX: send {txByteCount} bytes");
                    Thread.Sleep(5000);

                    // Read the response
                    rxByteCount = serialDevice.BytesToRead;
                    if (rxByteCount > 0)
                    {
                        byte[] rxBuffer = new byte[rxByteCount];
                        serialDevice.Read(rxBuffer);
                        Debug.WriteLine($"RX :{UTF8Encoding.UTF8.GetString(rxBuffer)}");
                    }

                    Thread.Sleep(20000);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Exemplo n.º 30
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);
        }