UART issue

I’m playing with Endpoint and particularly with UART port (@Greg_Norris I use a click GPS Module on click 2 socket :wink:).

I can’t read any data on UART port. (I try a TinyClr program and data is correctly received).

I discover that the number of UARTs is not label as in linux (as I can’t verify UART port, it is just a guess for now !) - maybe it could be written in doc:

Linux Pinout Remarks
/dev/ttySTM0 UART1
/dev/ttySTM1 UART2
/dev/ttySTM2 UART3
/dev/ttySTM3 UART4
/dev/ttySTM4 UART5 Can’t be access but it does exist on pinout (BUG ?)
/dev/ttySTM5 UART6 Doesn’t exist but I think it is normal as it doesn’t exist on pinout
/dev/ttySTM6 UART7
/dev/ttySTM7 UART8

Here is code to test:

using System.IO.Ports;
using System.Device.Gpio.Drivers;
using System.Device.Gpio;
using GHIElectronics.Endpoint.Core;
using System.Diagnostics;
using System.Text;

namespace testEndpointGPSSimple
{
    internal class Program
    {
        static byte[] buffer = new byte[64];
        static void Main(string[] args)
        {
            Console.WriteLine("Start...");

            // Reset pin
            var pinNumber = EPM815.Gpio.Pin.PF4;
            var port = pinNumber / 16;
            var pin = pinNumber % 16;
            var gpioDriver = new LibGpiodDriver(port);
            var gpio = new GpioController(PinNumberingScheme.Logical, gpioDriver);
            gpio.OpenPin(pin);
            gpio.SetPinMode(pin, PinMode.Output);

            var _serial = new SerialPort
            {
                PortName = "/dev/ttySTM2",
                BaudRate = 9600,
                DataBits = 8,
                Parity = Parity.None,
                StopBits = StopBits.One,
            };

            // Reset GPS Click
            Console.WriteLine("Reset GPS");
            gpio.Write(pin, PinValue.High);
            Thread.Sleep(100);
            gpio.Write(pin, PinValue.Low);
            Thread.Sleep(200);
            gpio.Write(pin, PinValue.High);
            Thread.Sleep(100);

            _serial.Open();

            Console.WriteLine("Awaiting data...");
            while (true)
            {
                // Read data if any
                if (_serial.BytesToRead > 0)
                {
                    var received = _serial.Read(buffer, 0, _serial.BytesToRead);
                    Debug.Write(Encoding.UTF8.GetString(buffer, 0, received));
                }
                Thread.Sleep(20);
            }
        }
    }
}

You are right, we just saw these issue yesterday :).

UART6 does not exist, and UART5 has wrong alternate so it doesn’t work.

We will remove UART6, and correct UART5 in next release

:wink:

But it won’t explain why I can’t read on UART3 !

add EPM815.SerialPort.Initialize(EPM815.SerialPort.Uart3); before opening serial

These four: SPI, I2C, PWM, UART you need “EPM815.xxxx.Initialize” before use them.

I begin to discover Endpoint with … UART ! The only one for which it’s not write in doc :rofl:

:astonished:…I’ll fix the docs!

2 Likes

Does it explain why you can’t read UART3 yet ?

1 Like

Yes, of course.

And I can read what GPS send.

2 Likes

Which GPS click module did you use? We’ll have to get one if we don’t already have it. Can’t wait to try it with the Google Maps project!

All modules should be compatible or very close

1 Like

image
PID: MIKROE-1032

1 Like

My attempt to get longitutde and latitude (Parser is in TinyClr Library :wink:):

using System.IO.Ports;
using System.Device.Gpio.Drivers;
using System.Device.Gpio;
using GHIElectronics.Endpoint.Core;
using System.Diagnostics;
using System.Text;

namespace testEndpointGPSSimple
{
    internal class Program
    {
        static byte[]? _lastSentence;
        static byte[] reader = new byte[512];
        static int idxReader = 0;
        static byte[] buffer = new byte[64];
        static bool started = false;

        static double _latitude;
        static double _longitude;
        static char _latitudeHemisphere;
        static char _longitudeOrientation;
        static void Main(string[] args)
        {
            Console.WriteLine("Start...");

            // Reset pin
            var pinNumber = EPM815.Gpio.Pin.PF4;
            var port = pinNumber / 16;
            var pin = pinNumber % 16;
            var gpioDriver = new LibGpiodDriver(port);
            var gpio = new GpioController(PinNumberingScheme.Logical, gpioDriver);
            gpio.OpenPin(pin);
            gpio.SetPinMode(pin, PinMode.Output);

            var _serial = new SerialPort
            {
                PortName = "/dev/ttySTM2",
                BaudRate = 9600,
                DataBits = 8,
                Parity = Parity.None,
                StopBits = StopBits.One,
            };
            EPM815.SerialPort.Initialize(EPM815.SerialPort.Uart3);

            // Reset GPS Click
            Console.WriteLine("Reset GPS");
            gpio.Write(pin, PinValue.High);
            Thread.Sleep(100);
            gpio.Write(pin, PinValue.Low);
            Thread.Sleep(200);
            gpio.Write(pin, PinValue.High);
            Thread.Sleep(100);

            _serial.Open();

            Console.WriteLine("Awaiting data...");
            while (true)
            {
                // Read data if any
                if (_serial.BytesToRead > 0)
                {
                    var received = _serial.Read(buffer, 0, buffer.Length);
                    ProcessBuffer(received);
                }
                // Check Parser state
                if (Parser.GLLSentence.Status == 'A')
                {
                    var latitude = Parser.GLLSentence.Latitude;
                    if (latitude != 0)
                    {
                        // Get Data
                        if (_latitude != latitude)
                        {
                            _latitude = latitude;
                            _latitudeHemisphere = Parser.GLLSentence.LatitudeHemisphere;
                            //Display Data
                            Debug.WriteLine("Lat: " + _latitude + _latitudeHemisphere);
                        }
                        if (_longitude != Parser.GLLSentence.Longitude)
                        {
                            _longitude = Parser.GLLSentence.Longitude;
                            _longitudeOrientation = Parser.GLLSentence.LongitudePosition;
                            //Display Data
                            Debug.WriteLine("Lon: " + _longitude + _longitudeOrientation);
                        }

                    }
                }
                Thread.Sleep(20);
            }
        }

        private static void ProcessBuffer(int received)
        {
            for (int i = 0; i < received; i++)
            {
                var ch = Encoding.UTF8.GetString(buffer, i, 1);
                if (!started)
                    if (ch == "$")
                    {
                        idxReader = 0;
                        reader[idxReader] = buffer[i];
                        started = true;
                        idxReader++;
                    }
                    else
                    {
                        // Discard byte
                    }
                else
                {
                    if (ch == "$") // Already started, so restart
                    {
                        idxReader = 0;
                        reader[idxReader] = buffer[i];
                        started = true;
                        idxReader++;
                    }
                    else if (ch == "\r")
                    { // End of sentence
                        started = false;
                        // Get sentence from reader idx 0 to idxReader
                        _lastSentence = new byte[idxReader];
                        Array.Copy(reader, _lastSentence, idxReader);
                        ProcessSentence(_lastSentence);

                    }
                    else
                    { // Keep copying
                        reader[idxReader] = buffer[i];
                        idxReader++;

                    }

                }
            }
        }

        private static void ProcessSentence(byte[] lastSentence)
        {
            //            Debug.WriteLine(Encoding.UTF8.GetString(lastSentence));
            Parser.Parse(lastSentence);
        }
    }
}
1 Like