System.IO.Ports.SerialPort.Read C# (CSharp) Method

Read() public method

public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
return int
		public int Read (byte[] buffer, int offset, int count)
		{
			CheckOpen ();
			if (buffer == null)
				throw new ArgumentNullException ("buffer");
			if (offset < 0 || count < 0)
				throw new ArgumentOutOfRangeException ("offset or count less than zero.");

			if (buffer.Length - offset < count )
				throw new ArgumentException ("offset+count",
							      "The size of the buffer is less than offset + count.");
			
			return stream.Read (buffer, offset, count);
		}

Same methods

SerialPort::Read ( char buffer, int offset, int count ) : int

Usage Example

示例#1
44
 public static void Main()
 {
     SerialPort serialPort = new SerialPort("COM1", 115200, Parity.None);
     serialPort.ReadTimeout = 5000; // milliseconds
     serialPort.Open();
     byte[] outBuffer = Encoding.UTF8.GetBytes("All right?\r\n");
     byte[] inBuffer = new byte[2];
     while (true)
     {
         Debug.Print("Request data");
         serialPort.Write(outBuffer, 0, outBuffer.Length);
         int count = serialPort.Read(inBuffer, 0, 2);
         if (count == 2)
         {
             Debug.Print("Received expected two bytes!");
         }
         else
         {
             if (count == 0)
                 Debug.Print("No response!");
             if (count == 1)
                 Debug.Print("Not enough bytes received!");
         }
         Debug.Print(string.Empty);
     }
 }
All Usage Examples Of System.IO.Ports.SerialPort::Read