0

我有一个串行设备,旨在接收和回复来自 VT100 型终端的命令。我可以使用任意数量的“终端仿真器”来操作这个设备。但我想要做的是从 .NET Windows 应用程序发送一系列命令,并从同一个应用程序接收回复,这样我就可以驱动一个对操作员来说“非常简单”的 GUI。我不想要的是另一个终端仿真器。看来使用“System.IO.Serial.SerialPort”我可以打开一个连接,发送一条消息,分配一个代表来接收一条消息,实际上接收一条消息......但我发送的消息(命令)不是按照我的预期解释(当我使用终端仿真器检查设备的状态时,状态与通过 .

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

(我在 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(显示所有输入的状态

4

1 回答 1

2

您的换行定义设置错误。你有NewLine = "/N",但你几乎肯定需要它是"\r\n"or "\n"

于 2014-03-25T20:11:20.297 回答