SerialPort and C#

What you need to know before you plan to work on Serial port communication:

0. Create a good test environment. You may need com0com (earlier) which can simulate a loopback between 2 ports.

1. Carefully choose between non-blocking and blocking. You save yourself a lot of pain when you thought using non-blocking was going to make work a lot easier. If you’re not familiar with the blocking vs. non-blocking approach, consider the table below the fold. (More on blocking vs. non-blocking). Holy wars have been fought between proponents of either form.

2. While doing your serial port stuff, also note that ReadLine has a BLOCKING nature. This is not mentioned in the original FrameWork SDK help files, but yes, it is mentioned in the SDK online. There’s no problem using ReadLine in your OnDataReceived event, but you must clear the port’s/socket buffer first before you can close your port/application (you’ll get specific errors).

3. There is no 4.

Description Typically, you read the data whenever an event is fired, pass the data on to secondary functions/methods that process and handle your variables.
Pro In easy cases, you just read whatever is available and process everything right from within the ‘received event’ of a port/socket
Con Tracking variables, states and others can be extremely hard, particularly when data needs to be received, processed constantly.
private void fired_event
    string s = serialPort.ReadExisting();
private ParseAndProcessData(string data)
    // For convenience sake...
    this.fpdata = data.SubString(10, 10);
    SomeText.BeginInvoke(new handler, 
       etc, etc);
Description Typically, you read the data at the moment you need it. If there’s no data available yet, the port (or socket) will wait until it has the data you requested for.
Pro You can do calls to read data from a port or socket right from within your functions, procedures or methods.
Con The moment you start reading/sending data, the port/socket will ‘block’ until it finds the data you requested for. This may result in ‘GUI freezes’ if not handled properly. Only use this when you’re threading skills are strong.
private ParseAndProcessData()
   switch (fstate)
      case TmyState.Nothing :
           // Stay right there
      case TmyState.ReadingSocket :
           string s = serialPort.ReadLine();  
      case TmyState.WritingSocket :
               this, 404, 405, e");
      case TmyState.Pending :
           fstate = tmyState.Nothing
This entry was posted in Programming and tagged , , . Bookmark the permalink.

2 Responses to SerialPort and C#

  1. Raymen says:


    How do i reset the communication device (serial port) by sending a 0xC1 byte?

    //Reset Byte to reset the serial communication device (Driver Key Tag)
    reset_bytes = Convert.ToSByte("0xC1"); 
    reset_buffer = new byte[reset_bytes];
    _serialPort.Write(reset_buffer, 0, reset_bytes);

    Gives me an incorrect string format exception.

    Please help



  2. Arthur says:
    Byte[] st = {193};
    this.serialPort1.Write(st, 0, 1);

Comments are closed.