c# 读取串口——如何全部读取

public byte[] Read() { byte[] inbuffer = null; if (serialPort.IsOpen && serialPort.BytesToRead > 0) { try { inbuffer = new byte[serialPort.BytesToRead]; serialPort.Read(inbuffer, 0, serialPort.BytesToRead); string strRaad = ASCIIEncoding.ASCII.GetString(inbuffer, 0, inbuffer.Length); while (strRaad.Contains(serialPort.NewLine)) { Thread.Sleep(800); if (serialPort.BytesToRead == 0) break; byte[] temp = inbuffer; byte[] inbuffer2 = new byte[serialPort.BytesToRead]; serialPort.Read(inbuffer2, 0, serialPort.BytesToRead); inbuffer = new byte[inbuffer.Length + inbuffer2.Length]; temp.CopyTo(inbuffer, 0); inbuffer2.CopyTo(inbuffer, temp.Length); strRaad = ASCIIEncoding.ASCII.GetString(inbuffer, 0, inbuffer.Length); } } catch { } } return inbuffer; }

你可能感兴趣的:(c#,串口通信)