Serielle Kommunikation und CRLF C#

ich Habe eine Frage über CRLF nach dem senden einer Zeichenkette an die serielle Schnittstelle.
Lassen Sie mich erklären, was ich versuche zu tun, Unter hier gibt es ein Beispiel.

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

Das ist, was ich versuche zu tun, aber ich kann nicht überall zu bekommen, Kann mir jemand helfen mit diesem.
ich glaube, ich habe zu tun, das CRLF starten, um ein newline, aber wenn Sie noch andere Vorschläge werden Sie mehr als willkommen
Dies ist, was ich getan habe:

Code:

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; }
        }
    }
}
InformationsquelleAutor | 2013-09-10
Schreibe einen Kommentar