c# – How to validate USB Serial port name

Question:

Guys, I'm doing the approval of a scale in C#, I can communicate via code, all right, but when making the connection, I need to specify the port name, I have other devices connected and the port name varies from machine to machine .

Can someone help me validate the port where the scale is connected?

For now I left the name static as COM15, but the port name can change.

I am opening a connection like this, it works but the name cannot be static.

//Definindo a porta
 SerialPort port = new SerialPort("COM15", 2400, Parity.None, 8,StopBits.One);
//Abrindo a porta
 port.Open();

I tried to get the names of all connected ports, but I can't distinguish which port on the scale.

//Pegando o nomes das portas conectadas
 string[] Portas = SerialPort.GetPortNames();

Is there any way to know if the connected port is on the scale?

Note: I use a serial to USB converter cable.

Answer:

Normally the scales have bidirectional communication, you send a command and it responds to you, it can be a weight request, a ping, anything like that, so you should check all active COM ports, that way you can identify, but the ideal is to do this whenever you open the system and not just at installation, because the serial port can change COM if the user changes the USB port, after some windows update or things like that.

Scroll to Top