Open Serial port and read : COM Port SerialPort « Development « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;

class Serial
{
 public static void Main()
 {
      byte[] buffer = new byte[256];
      using (SerialPort sp = new SerialPort("COM1", 19200))
      {
           sp.Open();
           //read directly
           sp.Read(buffer, 0, (int)buffer.Length);
           //read using a Stream
           sp.BaseStream.Read(buffer, 0, (int)buffer.Length);
      }
 }
}
Unhandled Exception: System.IO.IOException: The port 'COM1' does not exist.
   at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
   at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataB
its, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnabl
e, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
   at System.IO.Ports.SerialPort.Open()
   at Serial.Main()








14.32.COM Port SerialPort
14.32.1.Open Serial port and read
14.32.2.Set up serial port and write string to it