C # Serial Port Check if device is connected

I have been working a lot with the SerialPort class lately. I am currently trying to find the correct way to check if the device is connected to the communication port used by my application. Is there any suitable way to check if the device is connected to the communication port? My current method is as follows:

while (isReading == true) { try { received += serialPort.ReadExisting(); if (received.Contains('>')) isReading = false; } catch (Exception e) { } if (tick == 10000) if (received == "") { Console.WriteLine("No Data Received. Device isn't connected."); isReading = false; } tick++; } Console.WriteLine(received); 

It works, but I feel a little hacked and unreliable. I can save it if necessary, but I would like it to have a suitable alternative to this.

Edit: I really need to set the tick value to about 10,000 to ensure its reliability. Otherwise, I sometimes do not receive data. Even setting it to 1000 or 5000 is unreliable. Even then, it is not guaranteed to be reliable on multiple machines.

+6
source share
3 answers

I also need to work with serial ports, and believe me, they hurt. My device connection verification method usually revolves around issuing a polling command. Although the method may work, I cannot help, but I do not want to use the while loop when there is enough event.

The .NET serial port class offers several useful events:

Serial.DataReceived Serial.ErrorReceived and Serial.Write

Usually I did a polling command at a given interval to make sure that the device is connected. When the device responds, it DataReceived event, and you can process the response accordingly (along with any other necessary data). This can be used in conjunction with a simple timer or an added variable during a response. Note that you will need to set the ReadTimeout and WriteTimeout values โ€‹โ€‹accordingly. This, along with the ReadExisting and / or ReadLine , may be useful in your DataReceived event handler.

So, we summarize (in pseudo-code)

 Send Polling command, Start Timer Timer to CountDown for a specified time If Timer fires, then assume no response If DataRecieved fires (and expected response) assume connection (of course handle any specific Exceptions (eg TimeOutException, InvalidOperationException) 
+8
source

Unfortunately, with serial ports there is no suitable way to determine if a specific device is connected. You can write a magic message that will only respond to your device, but as described in this answer , this method can cause problems for other connected devices.

Ultimately, you just have to depend on the user who selects the correct port.

In addition, if you lose connection with the device, you will only know when you cannot read / write it. In this case, just drop the LostConnection event.

+2
source

I would agree that this is hacked, because any device can be connected and sent '>'; but that does not mean its your device.

Instead, be dynamic and use something like SerialPort.GetPortNames and WMI Queries to poll devices connected to COM ports.

You can use this example as a starting point.

After reading the documentation and examples, you can create a list of all information about the device, which registers the drivers on the computer and their connected COM port.

Edit:

Since the device does not register, consider the product drivers for Visual Studio, which can make your work a lot easier.

0
source

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


All Articles