SerialPort testing... Help?

I have been trying to get a serial port working with little success.

The code below is the code I am using.

The board has UARTS 1…8 with corresponding port names of /dev/ttySTM0…7.

To start testing I put a jumper wire between PE8<->PF6.

I left all of the SerialPort parameters, except for the port name, at the defaults. I used port name “/dev/ttySTM6”.

I verified port names to pin mapping by looking at the domino Pinout map and the Endpoint library code for SerialPort.

If I check the serial port bytes to write immediately after writing, I can see bytes in the output buffer. Actually, a larger buffer needs to be written to catch queued bytes to write before they are gone.

So, I seem to be writing but I never get any input.

I checked the jumper for continuity.

Anyone got a serial port working?

Suggestions?

using System.IO.Ports;

namespace SerialGPS_Test
{
    internal class Program
    {
        static SerialPort uart = new SerialPort();

        static void Main(string[] args)
        {
            uart.PortName = "/dev/ttySTM6";
            uart.Open();

            byte[] testBuffer = [ 1, 2, 3, 4, 5 ];
            uart.Write(testBuffer, 0, testBuffer.Length);
            Thread.Sleep(1000);
            Console.WriteLine($"bytes to read {uart.BytesToRead}");
   
            byte c = (byte)uart.ReadByte();
            Console.WriteLine("*** DATA ****");

            Thread.Sleep(Timeout.Infinite);
        }
    }
}

You need initialize the pins first

Something like

EPM815.SerialPort.Initialize(...)
static SerialPort uart = new SerialPort();

I should have seen that when looking through the library code.

I have attached a copy of my working code. You might want to add it to the documentation.

using GHIElectronics.Endpoint.Core;
using System.IO.Ports;
using System.Text;

namespace Serial_Test
{
    internal class Program
    {

        static SerialPort? comPort;
        static void Main(string[] args)
        {
            EPM815.SerialPort.Initialize("/dev/ttySTM6");

            comPort = new SerialPort();
            comPort.PortName = "/dev/ttySTM6";
            // specify other parameters as required.

            comPort.DataReceived += comPort_DataReceived;
            comPort.Open();

            for (int i = 0; i < 10; I++)
            { 
                byte[] testBuffer = Encoding.ASCII.GetBytes($"Hello Serial World {i}");
                comPort.Write(testBuffer, 0, testBuffer.Length);
                Thread.Sleep(1000);
                Console.WriteLine("------------------------");
            }

            Thread.Sleep(Timeout.Infinite);
        }

        static byte[] inputBuffer = new byte[1024];
        private static void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (comPort!.BytesToRead > 0) 
            { 
                int bytesRead = comPort.Read(inputBuffer, 0, comPort.BytesToRead);
                string ts = Encoding.ASCII.GetString(inputBuffer, 0, bytesRead);

                Console.WriteLine($"<{ts}>");
            }
        }
    }
}
2 Likes

Thks. We just added example to docs.

1 Like