Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to read a serial port using .NET framework?

I've read a lot of questions here about how to read data from serial ports using the .NET SerialPort class but none of the recommanded approaches have proven completely efficient for me.

Here is the code I am using for now:

SerialPort port = new SerialPort("COM1"); port.DataReceived += new SerialDataReceivedEventHandler(MyDataReceivedHandler); 

And the event handler:

void MyDataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {     int count = port.BytesToRead;     byte[] ByteArray = new byte[count];     port.Read(ByteArray, 0, count); } 

But I am still missing some data sometimes. I've tried different way of reading the data in the event handler but with no luck.

As the .NET 4.5 brings new possibilities to do some asynchronous tasks, like with the ReadAsync method that seems to be useable on a SerialPort stream, I'm curious to see what would be the recommended approach to handle those cases.

like image 634
Yannick Blondeau Avatar asked Dec 06 '12 23:12

Yannick Blondeau


People also ask

How do you read a serial port?

In Serial Port Reader go to the “Main menu”, choose “Session -> New session”. Alternately, you can click on the “New” icon on the main toolbar or press “Ctrl + N”. This invokes the “New monitoring session” screen. Terminal view – all received data is displayed in ASCII characters on a text console.

What is serial port in C#?

Serial port in C# is very useful for interfacing Arduino or other microcontrollers systems with a PC. Most of these ones communicate with computers using a FTDI chip, or an equivalent converting a serial port to a virtual USB Communication Device Class (CDC).

What is serial port in VB net?

ActiveXperts Serial Port Component is a software development kit (SDK) that enables the user to communicate to a device over a serial interface. Such a device can be: a weight indicator, a modem, a scanner, or any other device that is equiped with a serial port.

How do I read a COM port in Windows?

1) Click Start. 2) Click Control Panel in the Start menu. 3) Click Device Manager in the Control Panel. 4) Click + next to Port in the Device Manager to display the port list.


1 Answers

Could you try something like this for example I think what you are wanting to utilize is the port.ReadExisting() Method

 using System;  using System.IO.Ports;   class SerialPortProgram   {    // Create the serial port with basic settings      private SerialPort port = new   SerialPort("COM1",       9600, Parity.None, 8, StopBits.One);      [STAThread]      static void Main(string[] args)      {        // Instatiate this        SerialPortProgram();      }       private static void SerialPortProgram()      {          Console.WriteLine("Incoming Data:");          // Attach a method to be called when there          // is data waiting in the port's buffer          port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);          // Begin communications          port.Open();          // Enter an application loop to keep this thread alive          Console.ReadLine();      }       private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)      {         // Show all the incoming data in the port's buffer        Console.WriteLine(port.ReadExisting());      }  } 

Or is you want to do it based on what you were trying to do , you can try this

public class MySerialReader : IDisposable {   private SerialPort serialPort;   private Queue<byte> recievedData = new Queue<byte>();    public MySerialReader()   {     serialPort = new SerialPort();     serialPort.Open();     serialPort.DataReceived += serialPort_DataReceived;   }    void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)   {     byte[] data = new byte[serialPort.BytesToRead];     serialPort.Read(data, 0, data.Length);     data.ToList().ForEach(b => recievedData.Enqueue(b));     processData();   }    void processData()   {     // Determine if we have a "packet" in the queue     if (recievedData.Count > 50)     {         var packet = Enumerable.Range(0, 50).Select(i => recievedData.Dequeue());     }   }    public void Dispose()   {         if (serialPort != null)         {             serialPort.Dispose();         }   } 
like image 114
MethodMan Avatar answered Sep 22 '22 18:09

MethodMan