Question

I have a serial device that is designed to receive and reply to commands from a VT100 type terminal. I can use any number of 'terminal emulators' to operate this device. But what I WANT to do is send a series of commands from a .NET Windows application and receive the replies back the same application so that I can drive a GUI that will be 'dirt simple' for the operators. What I don't want is another Terminal Emulator. It appears that using "System.IO.Serial.SerialPort" I can open a connection, send a message, assign a delegate to receive a message, actually receive a message...but the message that I send (a command) is not interpreted as I expect (when I inspect the status of the device with a terminal emulator the status does not match the command sent via .Net) and the message I receive back is not in a format I can decipher.

What I believe I need to do is properly encode the command and properly decode the response...but I can find no documentation that clearly explains how to do this.

(I'm working in C#, but any .Net example would be appreciated)

The code I am working with to prototype this is:

using System;
using System.IO.Ports;
using System.Text;

namespace busmanager
{
    public class buslink
    {
        SerialPort _serialPort;
        Boolean _echo;
        Delegate _receiver;

        //Defaut Ctor:
        public buslink(Delegate Receiver)
        {
            String[] SerialPorts = System.IO.Ports.SerialPort.GetPortNames();
            if (SerialPort.GetPortNames().Length > 0)
            {
                SetChannel(PortDefinitions.GetDefaults(SerialPort.GetPortNames()[0]), Receiver);
            }
            else
            {
                throw new Exception("Unable to connect to serial port");
            }
        }

        //Custom Ctor: for externally defined PortDefinitions
        public buslink(PortDefinitions PortDefinitions, Delegate Receiver)
        {
            SetChannel(PortDefinitions, Receiver);
        }

        private void SetChannel(PortDefinitions PortDefinitions, Delegate Receiver)
        {
            _serialPort = new SerialPort()
            {
                PortName = PortDefinitions.PortName,
                //BaudRate = PortDefinitions.BaudRate,
                Parity = PortDefinitions.Parity,
                StopBits = PortDefinitions.StopBits,
                DataBits = PortDefinitions.DataBits,
                Handshake = PortDefinitions.Handshake,
                ReadTimeout = PortDefinitions.ReadTimeout,
                WriteTimeout = PortDefinitions.WriteTimeout,
                NewLine = "\n",
                Encoding = System.Text.Encoding.ASCII,
                DtrEnable = true,


            };
            _echo = PortDefinitions.Echo;
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
            _serialPort.Open();
            _receiver = Receiver;

        }

        void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] c = new byte[1000];
           int x = _serialPort.Read(c,0,1000);
            string d = c.ToString();

            _receiver.DynamicInvoke(_serialPort.ReadExisting());
        }

        public string Send(string Cmd)
        {
            Cmd = Cmd.ToUpper();
            byte[] sendBytes = Encoding.ASCII.GetBytes(Cmd);
            try
            {
                if (ValidateCmd(Cmd))
                {
                    _serialPort.WriteLine(Cmd);
                    if (_echo)
                    {
                        return "ECHO: " + Cmd;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return string.Empty;

        }

        private bool ValidateCmd(string Cmd)
        {
            return true;
        }



        public void Dispose()
        {
            _serialPort.Close();
            _serialPort.Dispose();
        }
    }

    public struct PortDefinitions
    {
        public string PortName { get; set; }
        public int BaudRate { get; set; }
        public Parity Parity { get; set; }
        public StopBits StopBits { get; set; }
        public int DataBits { get; set; }
        public Handshake Handshake { get; set; }
        public int ReadTimeout { get; set; }
        public int WriteTimeout { get; set; }
        public bool Echo { get; set; }

        public static PortDefinitions GetDefaults(string PortName)
        {
            return new PortDefinitions()
                                        {
                                            PortName = PortName.ToUpper(),
                                            BaudRate = 9600,
                                            Parity = Parity.None,
                                            StopBits = StopBits.One,
                                            DataBits = 8,
                                            Handshake = Handshake.None,
                                            ReadTimeout = 1000,
                                            WriteTimeout = 1000,
                                            Echo = true
                                        };
        }


    }


}

The device is proprietary, but the commands it accepts are: InX OutY (where X and Y are ports on the device and a connection is created between the In and the Out Ports) Status All (shows the status of all the inputs

Was it helpful?

Solution

You have your newline definition set wrong. You have NewLine = "/N", but you almost certainly need it to be "\r\n" or "\n".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top