Open Serial port and read : COM Port SerialPort : Development C# Examples


C# Examples » Development » COM Port SerialPort »

 

Open Serial port and read









    
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);
            }
  }
}
    
   
  
   



Output

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()


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Development
» COM Port SerialPort