如何在线程函数中使用serialport类成员作为参数 [英] How to use serialport class member as parameter in a thread function

查看:75
本文介绍了如何在线程函数中使用serialport类成员作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个Thread函数可以从串口连续读取数据,如下所示:

Hi,
I have a Thread Function that reads data continuously from the Serial Port like this:

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





我需要使用相同的线程函数从六个串口读取数据。 />
如何创建接受SerialPort作为参数的Thread函数并使用该参数从该特定串行端口读取。



谢谢



我尝试过:



将SerialPort类成员作为参数传递给线程函数



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

推荐答案

我知道Jon Skeet已经在这里 .NET中的多线程:简介和建议 [ ^ ]



一般的要点,我原以为是这样的,



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


这篇关于如何在线程函数中使用serialport类成员作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆