从.NET发送和接收到串行设备“喜欢”VT 100

时间:2014-03-25 19:42:33

标签: c# .net vt100

我有一个串行设备,用于接收和回复来自VT100类型终端的命令。我可以使用任意数量的终端模拟器'操作此设备。但我想要做的是从.NET Windows应用程序发送一系列命令并接收回复相同的应用程序,以便我可以驱动一个简单的GUI。对于运营商。我不想要的是另一个终端仿真器。似乎使用" System.IO.Serial.SerialPort"我可以打开一个连接,发送一条消息,分配一个代表来接收一条消息,实际收到一条消息......但是我发送的消息(一个命令)没有被解释为我所期望的(当我检查设备的状态时)使用终端仿真器,状态与通过.Net发送的命令不匹配,我收到的消息不是我可以解密的格式。

我认为我需要做的是正确编码命令并正确解码响应...但我找不到明确说明如何执行此操作的文档。

(我在C#工作,但任何.Net示例都会受到赞赏)

我正在使用的代码原型是:

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
                                        };
        }


    }


}
  

该设备是专有的,但它接受的命令是:InX OutY   (其中X和Y是设备上的端口,并创建连接   在入口和出口之间)Status All(显示所有的状态   输入

1 个答案:

答案 0 :(得分:2)

您的换行符定义设置错误。您有NewLine = "/N",但几乎可以肯定需要"\r\n""\n"