Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a Thread Function that reads data continuously from the Serial Port like this:
C#
SerialPort _serialPort;
_serialPort = new SerialPort();
_serialPort.Open();
Thread ReceiveDataFromSerial = new Thread(ReadData)
Thread.Start()
byte[] buffer = new byte[20];
public void ReadData()
{

  while(true)
   {
     _serialPort.Read(buffer,0,20);
   }
}


I need to read data from Six Serial ports using the same Thread Function.
How can I make Thread function that accept SerialPort as parameter and use that parameter to read from that particular Serial Port.

Thanks

What I have tried:

Passing SerialPort Class member as parameter to a thread function
Posted
Updated 21-Mar-17 20:53pm
v2

1 solution

I know that's been covered by Jon Skeet in here Multi-threading in .NET: Introduction and suggestions[^]

The general gist, I would have thought was something like this,

SerialPort _serialPort = ...
Thread thread = new Thread(() => ReadData(_serialPort, buffer));
thread.Start();
...
public void ReadData(SerialPort _serialPort, byte[] buffer)
{
  while(true)
  {
    _serialPort.Read(buffer, 0, 20);
  }
}


although I think the passing/handling of the buffer may require more thought/attention than I've paid to it
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900