Quintic
C# and DotNet

Introduction

I have been developing with C# / DotNet now since 2002. My initial introduction was in 2001 when, as a Java developer, I worked along side a team developing with C# under Microsoft’s Early Adoption Program. The project that was under development continued well beyond the 12 month EAP, and at the start of 2002 I was lucky enough to move onto the team.

Xamarin and VS 2019

April 24th 2019 and VS 2019 for Windows and MAC released. For me that’s the good news. The bad news: To download VS 2019 on the MAC you need, as a minimum, MAC OS 10.12. My two Macs, a Macbook Pro and Macbook Air, both 2014 and both full spec, are stuck at 10.10. Thank you Apple

Some good news Apple has released MAC OS 10.12 for my PCs. I have upgraded and successfully downloaded VS2019. I then ported a small C# console app from Windows to the MAC. The app was a telnet client that opened a telnet connection to a device on my network and awaited data.

And it compiled and worked out of the box, first time. How neat is that. Thank you Microsoft and Xamarin. The code:

C# Telnet Client

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Diagnostics;

namespace Telnet
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> dataPoints = new List<int>();
            byte[] data = new byte[640];
            byte asc = 33;
            int bytes = 0;
            for (int i = 0; i < 32; i++)
            {
                data[i] = asc++;
            }
            Stopwatch stopWatch = new Stopwatch();
            if (args.Length == 1)
            {
                string ip = args[0];
                TcpClient tcpClient = new TcpClient(ip, 23);
                tcpClient.ReceiveTimeout = 1000;
 
                var stream = tcpClient.GetStream();
                while (true)
                {
                    stopWatch.Start();
                    bytes = stream.Read(data, 0, data.Length);
                    // Convert bytes back to ints. Note: Number of bytes should always be divisible by 5.

                    if ((bytes % 5) == 0)
                    {
                        int number;
                        int byteIndex = 0;
                        int numInts = bytes / 5;

                        dataPoints.Clear();
                        for (int i = 0; i < numInts; i++)
                        {
                            number = 0;
                            for (int j = 4; j >= 0; j--)
                            {
                                number = number * 85 + (data[byteIndex + j] - 33);
                            }
                            dataPoints.Add(number);
                            byteIndex += 5;
                        }
                        stopWatch.Stop();
                        Console.WriteLine("Received {0} bytes: ", bytes);

                    }
                    else
                    {
                        Console.WriteLine("Number of bytes {0} must be divisible by 5, otherwise something has gone wrong", bytes);
                        Console.ReadLine();
                    }
                    Console.Write("Converted as");
                    foreach (int dataPoint in dataPoints)
                    {
                        Console.Write(" {0}", dataPoint);
                    }
                    Console.WriteLine("");
                    Console.WriteLine("In {0} mSecs", stopWatch.ElapsedMilliseconds);
                    stopWatch.Reset();
                }
            }
            else
            {
                Console.WriteLine("No IP Address");
                Console.Read();
            }
        }
    }
}

Not exactly state of the art programming, and it is only a PoC to test trasmission of data from an ESP32 over Telnet via WiFi, but the impressive thing is it works on both Windows and Mac – No Code Change.

In case you are wondering about the “Convert bytes back to int” comment, Telnet is an ASCII protocol, which means all bytes have to be printable characters. The data that I wanted to transmit from the ESP32 is integer (ie binary). So:

  • On the ESP32, prior to transmission, we convert the four byte integers into five bytes, using ASCII85 encoding
  • Transmit this ASCII85 encoded data via Telnet
  • Reconstruct the integers on reciept.

Hence the comment that the number of bytes received must be multiples of five.