简体   繁体   中英

c# serial port read byte array from serial port

Im trying to read data from serial port and to compare it, but i cant get it working, data that i read isnt that i need to get and sometimes its incomplete basicly what i want when data from serial port comes and if data data is equal to an array to write some data to serial port

    private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        var Serial1 = (SerialPort)sender;
        Serial1.DtrEnable = true;
        Serial1.RtsEnable = true;
        int bytes = Serial1.BytesToRead;
        byte[] buffer = new byte[bytes];
        Serial1.Read(buffer, 0, bytes);
        string buffer1 = System.Text.Encoding.UTF8.GetString(buffer);
        newform(buffer1);
        showinwindow(buffer);

    }

    private void showinwindow(byte[] buffer)
    {
        byte[] array1 = { 0x03, 0x2F, 0x2C };
        bool a = array1.SequenceEqual(buffer);
        if (a == true)
        {
            byte[] upisipodatak = { 0x03, 0x20, 0x23 };
            serialPort1.Write(upisipodatak, 0, upisipodatak.Length);
        }
    }

    private void newform(string buffer1)
    {
        BeginInvoke(new EventHandler(delegate
        {
                textBox1.AppendText(buffer1);
        }));
    }

I think your problem is that when you start the read, not all bytes are available so only a partial amount is returned. You might want to try a blocking read instead, along these lines:

/// <summary>
/// Attempts to read <paramref name="count"/> bytes into <paramref name="buffer"/> starting at offset <paramref name="offset"/>.
/// If any individual port read times out, a <see cref="TimeoutException"/> will be thrown.
/// </summary>

public void BlockingRead(SerialPort port, byte[] buffer, int offset, int count)
{
    while (count > 0)
    {
        // SerialPort.Read() blocks until at least one byte has been read, or SerialPort.ReadTimeout milliseconds
        // have elapsed. If a timeout occurs a TimeoutException will be thrown.
        // Because SerialPort.Read() blocks until some data is available this is not a busy loop,
        // and we do NOT need to issue any calls to Thread.Sleep().

        int bytesRead = port.Read(buffer, offset, count);
        offset += bytesRead;
        count -= bytesRead;
    }
}

Note that this will throw an exception on timeout (and you can configure the timeout for the serial port using SerialPort.ReadTimeout .)

However, be aware that the .Net SerialPort implementation has some flaws. See this article for details .

In particular, SerialPort.Read() is a blocking call, which you would normally want to avoid, but doing so will mean that you will have to do some reading up yourself!

I found a solution that works for me like 90 of 100 times just removed

Serial1.DtrEnable = true;
Serial1.RtsEnable = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM