How to write to console and send output to serial port using .NET NanoFramework (ESP32-S2)

120 Views Asked by At

I have installed .NET NanoFramework on a LOLIN S2 mini (ESP32-S2 chip). It is working fine. But - I cannot find a way to send serial output via the USB port to the PC. I am not sure which COM port number I have to use, so I tried COM1 & COM2

My test code:

using System;
using System.Diagnostics;
using System.Threading;
using System.Device.Gpio;
using nanoFramework;
using System.IO.Ports;

namespace NFApp1
{
    public class Program
    {
        private static GpioController s_GpioController;
        public static void Main()
        {
            var port = new SerialPort("COM1", 115200);
            s_GpioController = new GpioController();
            GpioPin led = s_GpioController.OpenPin(15, PinMode.Output);
            led.Write(PinValue.Low);
            port.Open();

            while (true)
            {
                port.Write("Test... ");

                led.Toggle();
                Thread.Sleep(125);
                led.Toggle();
                Thread.Sleep(125);
                led.Toggle();
                Thread.Sleep(125);
                led.Toggle();
                Thread.Sleep(525);
            }
        }
    }
}

When using COM1 my code is running, but "Test... " does not show up. Next issue is that I am not able to deploy solution again. I have to nanoff to reflash firmware.

When using COM2 my code is not executing at all. The deploy issue does not appear.

Any suggestions ?

Best regards, Brian

I tried all that I was able to figure out

2

There are 2 best solutions below

0
José Simões On

You'll see that test output on Visual Studio output pane.

On which COM port you should use, it depends on your hardware exposes. Know that one of the COM ports (usually the 1st one) it's reserved for Wire Protocol (meaning communicating with VS, deploying and debugging) so you CAN NOT use it, otherwise your system get's isolated from the outside world.

In order to add logging to your application, you may want to look at nanoFramework logging library. Choose the flavor that better fits your purpose.

1
Mango On

use putty or equivalent and open the serial port your device is connected to with the baud rate of 921600, this is the baud rate used by Console.WriteLine or Debug.WriteLine and whenever you have exceptions or equivalent raised by the CLR. Detail: https://docs.nanoframework.net/content/getting-started-guides/troubleshooting-device-connection.html