Serial communication (UART) with TinyCLR OS on G80TH

I am trying to communicate with a GHI F20-uSD module via it’s default UART interface, from a GHI G80TH module running the preview TinyCLR OS image instead of the standard netMF one.

I’m not yet experienced with the serial communications libraries of UWP (Universal Windows Platform) that TinyCLR OS is said to use, and I am struggling to find helpful examples.

Does anyone have a simple UWP C#.Net example of enumerating the com ports, opening one (COM1 on the G80TH in this case), and then sending and receiving serial data? One That would work under TinyCLR OS on the G80TH?

Thanks cyberh0me.

I had seen that example - it does have serial communications, but it also has quite a bit of GUI and async code.

I was not certain that the simplest serial communication required setting up async tasks, and doing it the way that example did. I had thought that perhaps this was necessary because of the apps UI thread.

I’m not going to use a UI, or Xaml. I dont really need to do anything asynchronously. But if you think that is still the simplest form of serial communications for TinyCLR OS, I will go that route and use that example.

Using the G80 COM1

Just for fun ;D


using System;
using System.Text;
using GHIElectronics.TinyCLR.Devices.SerialCommunication;
using GHIElectronics.TinyCLR.Storage.Streams;

namespace SimpleSerial
{
    public class Program
    {
        public static void Main()
        {
            SerialDevice _port = SerialDevice.FromId("COM1");
            var _writer = _port.OutputStream;
            var _reader = _port.InputStream;

            string _banner = "Welcome to the Controller\r\nOptions\r\n1 - Engine\r\n2 - Communication\r\n3 - Navigation\r\n4 - All\r\n";
            Buffer _bannerBuffer = new Buffer(Encoding.UTF8.GetBytes(_banner));
            _writer.Write(_bannerBuffer);

            while (true)
            {
                Buffer data = new Buffer(1);

                if (_reader.Read(data, 1, InputStreamOptions.None) > 0)
                {
                    WriteResponse(_writer,data);
                }                  
                System.Threading.Thread.Sleep(250);
                
            }

        }

        private static void WriteResponse(IOutputStream writer, Buffer data)
        {
            string[] _prompts = new string[5];
            _prompts[0] = "Invalid Choice";
            _prompts[1] = "Engine on";
            _prompts[2] = "Communication on";
            _prompts[3] = "Navigation on";
            _prompts[4] = "All systems on ";

            int _selected = 0;

            switch (Convert.ToChar(data.Data[0]))
            {
                case '1':
                    _selected = 1;
                    break;
                case '2':
                    _selected = 2;
                    break;
                case '3':
                    _selected = 3;
                    break;
                case '4':
                    _selected = 4;
                    break;
                default:
                    _selected = 0;
                    break;
            }

            Buffer _promptBuffer = new Buffer(Encoding.UTF8.GetBytes(_prompts[_selected]+"\r\n"));

            writer.Write(_promptBuffer);

            return;
        }
    }
}


2 Likes

@ Designer - Perfect. Exactly what I was looking for!