2

向串口发送字符串后,我对 CRLF 有疑问。让我解释一下我想做什么,下面有一个例子。

[ACR]  CRLF
10:90  CRLF
11:20  CRLF
12:2.1 CRLF

这就是我想要做的,但我无法到达任何地方,有人可以帮我解决这个问题。我想我必须做 CRLF 才能开始换行,但如果你有其他建议,他们会更受欢迎这就是我所做的:

代码:

private void SendMessageTest()
        {
            var content = _durCleaningTextbox.Text;

            byte[] array = ComPort.StringToBytes(content);




                _comport.WriteBytes(array);
                _comport.ReadBytes(array, array.Length, 1000);
                string result = Encoding.ASCII.GetString(array);
            MessageBox.Show(result);
        }

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

namespace Communication
{
    public class ComPort
    {
        private readonly SerialPort _serialPort;

        public ComPort(string portName, int baudRate)
        {
            _serialPort = new SerialPort();

            _serialPort.PortName = portName;
            _serialPort.BaudRate = baudRate;

            _serialPort.StopBits = StopBits.One;
            _serialPort.DataBits = 8;
            _serialPort.Parity = Parity.None;

            _serialPort.Handshake = Handshake.None;


            // _serialPort.WriteBufferSize = 1;
            _serialPort.DtrEnable = true;
            _serialPort.RtsEnable = true;

            _serialPort.Open();

            _serialPort.ReadTimeout = 20000;
            _serialPort.WriteTimeout = 20000;
        }

        public void Clear()
        {
            while (ReadByte() != -1)
                continue;
        }

        private byte[] _array = new byte[] {0};

        public void WriteByte(byte value)
        {
            _array[0] = value;
            _serialPort.Write(_array, 0, 1);
            // _serialPort.BaseStream.WriteByte(value);
            _serialPort.BaseStream.Flush();
        }

        public void WriteBytes(byte[] array)
        {
            _serialPort.Write(array, 0, array.Length);
        }

        public void WriteBytes(byte[] array, int index, int length )
        {
            _serialPort.Write(array, index, length);
        }

        private int _readTimeOut = -1;

        public int ReadByte(int timeOut = 200)
        {
            if (timeOut != _readTimeOut)
                _serialPort.ReadTimeout = _readTimeOut = timeOut;
            try
            {
                //return _serialPort.BaseStream.ReadByte();
                return _serialPort.ReadByte();
                // _serialPort.Read(array, 0, 1);
                // return array[0];
            }
            catch (TimeoutException)
            {
                return -1;
            }
        }

        public int ReadBytes(byte[] array, int length, int timeOut = 200)
        {
            if (timeOut != _readTimeOut)
                _serialPort.ReadTimeout = _readTimeOut = timeOut;
            try
            {
                //return _serialPort.BaseStream.ReadByte();

                int bytesRead = 0;
                while ( bytesRead < length )
                    bytesRead += _serialPort.Read(array, bytesRead, length - bytesRead);

                // _serialPort.Read(array, 0, 1);
                // return array[0];
                return bytesRead;
            }
            catch (TimeoutException)
            {
                return -1;
            }
        }

        /// <summary>
        /// sends string followed by CR - LF
        /// </summary>
        /// <param name="line"></param>
        public void WriteLine(String line)
        {
            WriteBytes(StringToBytes(line + "\r\n"));
        }

        public static byte[] StringToBytes(string input)
        {
            return Encoding.ASCII.GetBytes(input);
        }

        public void Close()
        {
            try
            {
                _serialPort.DtrEnable = false;
                _serialPort.RtsEnable = false;
                _serialPort.Close();
            }
            catch(IOException)
            {

            }
        }

        public bool Dtr
        {
            get { return _serialPort.DtrEnable; }
            set { _serialPort.DtrEnable = value; }
        }

        public bool Rts
        {
            get { return _serialPort.RtsEnable; }
            set { _serialPort.RtsEnable = value; }
        }
    }
}
4

2 回答 2

1
byte[] array = ComPort.StringToBytes(content + "\r\n");

或者使用您WriteLine已经这样做的方法。所以:

byte[] array = ComPort.StringToBytes(content);
_comport.WriteBytes(array);

变成:

_comport.WriteLine(content);
于 2013-09-10T11:08:29.187 回答
0

您需要发送命令 CR(回车)和 LF(换行或换行)。

因为这只是像这样发送您的命令以及 CR 和 LF:

string command = "myCommand";

port.write(string.format("{0}\r\n", command));

\r\n = CR + LF -> 在 Windows 中用作换行符

于 2014-01-10T16:34:15.970 回答