How can I listen on multiple serial ports asynchronously in C #

I have an application that listens for a piece of hardware on a USB-Serial converter. My application must monitor several serial ports at the same time.

I will loop the serial ports I need to listen to and create a thread for each port. In the stream, I have a data processing procedure.

When I assign one port, it works flawlessly. When I listen to another, it also works. However, when I open both ports, the second port always throws UnauthorizedAccessException when calling serialPort.Open() . No matter in what order I open the ports, the second always fails.

I am listening to ports using serialPort.ReadLine() in a while loop.

Can .NET open multiple ports at the same time? Can i listen to both? Or should I use a different way (thread safe?) To access serial port events?

+4
source share
1 answer

The exception has a very specific meaning; it tells you that someone has already opened the port. Who could it be? Check four times three times that you are really using a different port name when opening the second one.

The next step is to take the USB emulator to the parking lot and drive it several times with the car so that it can no longer control the programmer's nuts. Get one from another manufacturer that uses a different device driver provider.

+3
source

Source: https://habr.com/ru/post/1306826/


All Articles