Missing something using FezTerm

TT settings

Putty Settings

Proof that the serial port is being used by TT. I held the port by leaving TT still connected and then opened FT and tried to connect.

Thanks William, think those replies got crossed in the post.

Should it still work with TT?? If you don’t mind answering, what would I need to change to get it to work sorry?

Thanks to all for you your help so far!

Andy

Where is the simple code we are using to test this?

Hi Thor. If you want to use TT, you will need to make some kind of telnet host on the fez side for the TT to talk to. Even then, you would need to roll some kind of command processor. FezTerm is more of simple/quick command processor effort then just another tty. hth

Gus: Now that I know that what I thought was working code has a few items to iron out then I will be going down the simple code route.

Thanks,

//A

Thanks William. Will get cracking on this now! :slight_smile:

//A

“Should it still work with TT?? If you don’t mind answering, what would I need to change to get it to work sorry?”

Probably not too well with TT as was not designed for. I can take another look, but think it would be *far easier to update FezTerm Win client to meat your needs then update the backend as most of the work is in the backend. If I may ask, what is the requirement to use TT? What front end features are you looking for?

The requirement of my project was to develop a generic USB_CDC device that could connect to any terminal client be that HyperTerminal, Tera Term, Putty or their home rolled version on any platform. All I needed it to do was to accept specific serial commands and provide associated functionality and responses back to the client.

My main problem was that I couldn’t get the read side to work so I had a look round for a working set of code to dissect and learn from. Hence I found yours and got to work. It would have really helped if there had been a working example for the .Read as well as the .Write method in the GHI Electronics NETMF Library.

Don’t worry from your side, I will dig into this and get it working but thank you for the kind offer to take another look.

Kind regards,

Andy

Here is a sample to get you started. See the MainSample() method for usage. hth

using System;
using Microsoft.SPOT;
using System.IO.Ports;
using GHIElectronics.NETMF.USBClient;
using System.Threading;
using System.Collections;
using System.Text;

namespace Fez.IO
{
    /// <summary>
    /// Simple Telnet "like" host for MF.
    /// Set TeraTerm to:
    ///   Receive: CR+LF
    ///   Transmit: CR+LF
    /// </summary>
    public class TTYHost
    {
        bool isStarted;
        bool isDisposed;
        USBC_CDC sPort;
        int delimChar = 10; //LF = \n = 0A = 10 dec
        ManualResetEvent mre;

        public TTYHost()
        {
            mre = new ManualResetEvent(false);
        }

        public void Open()
        {
            if (isStarted||isDisposed) return;
            isStarted = true;
            sPort = InitCDC();
            new Thread(ReadLoop).Start();
        }

        public void Close()
        {
            isDisposed = true;
        }

        public void Wait()
        {
            mre.WaitOne();
        }

        private void ReadLoop()
        {
            while (!isDisposed)
            {
                string line = ReadString(delimChar);
                line = line.Trim().ToLower();
                
                // Command processor.
                switch(line)
                {
                    case "datetime":
                        line = DateTime.Now.ToString();
                        break;
                    case "exit":
                        line = "bye";
                        Close();
                        break;
                    default:
                        line = "Echo: " + line;
                        break;
                }

                // Send reply back.
                byte[] ba = Encoding.UTF8.GetBytes(line + "\r\n");
                sPort.Write(ba, 0, ba.Length);
            }
            Debug.Print("TTY Host Exited");
            mre.Set();
        }

        private string ReadString(int delim)
        {
            ArrayList al = new ArrayList();
            byte[] readBuf = new byte[1];
            int received = 0;
            while (true)
            {
                int toRead = readBuf.Length - received;
                int read = sPort.Read(readBuf, 0, 1);
                if (read == 0)
                    break;
                al.Add(readBuf[0]);
                if (readBuf[0] == delim)
                    break;
            }
            byte[] bytes = al.ToArray(typeof(byte)) as byte[];
            char[] chars = Encoding.UTF8.GetChars(bytes);
            return new string(chars);
        }

        private static USBC_CDC InitCDC()
        {
            Debug.Print("Hit Break All in VS to reattach debugger to port, then F5/Continue.");
            USBC_CDC sPort = USBClientController.StandardDevices.StartCDC_WithDebugging();
            sPort.ReadTimeout = -1; // Block waiting for chars.
            
            while (true)
            {
                if (USBClientController.GetState() != USBClientController.State.Running)
                {
                    Debug.Print("Waiting to connect.");
                    Thread.Sleep(600);
                }
                else
                {
                    Debug.Print("CDC comm port connected: ");
                    break;
                }
            }
            return sPort;
        }

        // Usage Sample:
        public static void MainSample()
        {
            TTYHost tty = new TTYHost();
            tty.Open();

            // Do other stuff here.

            // Wait for tty exit.
            tty.Wait();
            return;
        }
    }
}

Thanks for the code. Now on fezzer?

Good idea. Here it is on Fezzer
[url]http://www.fezzer.com/project/191/telnet-type-class-for-netmf/[/url]

Sorry been away for a couple of days and didn’t get the chance to say a big thanks to everyone for their help. It is really appreciated. William your new sample works perfectly! :slight_smile:

Kind regards,

Andy