Validating USB Serial Port Name

Asked

Viewed 394 times

4

People, I am doing the approval of a scale in c#, I can make the communication via code all right, however when making the connection, I need to specify the name of the port, I have other connected devices and the name of the port varies from machine to machine.

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

For now I left the static name as COM15, but the door name may change.

I’m opening a connection like this, it works, but the name can’t be statistical.

//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 the connected ports, but I can’t tell which door is the scale.

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

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

Obs: I use a serial to USB converter cable.

  • It would not be the case to list the ports and try to verify which device is connected in the same?

  • I think there is not much to do in this case, in general should stay as configuration for the user, unless you try to connect port by port and test send some command to see if it is the correct device

  • And if instead of mapping the ports, use the VID and PID identifications of the USB device?

  • 2

    Hey, guys, thanks for your help. I managed to solve the problem as follows: I created a parameter in the system configuration, that is every time I install the system it defines the correct port name

  • 1

    I did this using a combobox, so the user only needs to select the port once

1 answer

1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.