System.IO.Ports.SerialPort and Multithreading

后端 未结 4 733
清酒与你
清酒与你 2021-02-02 04:42

I have some SerialPort code that constantly needs to read data from a serial interface (for example COM1). But this seems to be very CPU intensive and if the user moves the wind

4条回答
  •  青春惊慌失措
    2021-02-02 05:20

    Your last conclusion, that the event runs on the Main thread, may not be true for Windows App. Don't test this in a Console.

    The proper way to tune this is:

    • set a large enough buffer, although the minimum 4096 is usually Ok

    • set the ReceivedBytesThreshold as high as tolerable (and do it before the Open())

    • do as little as possible in the Received event, pass the data to a Queue or MemoryStream if you need more time

提交回复
热议问题