why is access to com port denied?

后端 未结 5 1671
小蘑菇
小蘑菇 2020-12-06 17:13

the code:

static void Main(string[] args)
{
    Console.WriteLine(\"Memory mapped file reader started\");

    using (var file = MemoryMappedFile.OpenExistin         


        
5条回答
  •  无人及你
    2020-12-06 17:22

    You can open a serial port only once. But your code has the Open() call inside the while loop. That will only work for the first pass through the loop, kaboom on the 2nd pass. @cdhowie's solution doesn't work either, SerialPort has a quirk (aka bug) that the documentation warns about. It needs time to let a worker thread exit after the Dispose() or Close() call. The amount of time is unspecified and unpredictable.

    The real solution is simple, just move the Open() call before the while loop.

提交回复
热议问题