Serial port - how to perform a secure search for my device?

I am writing an application that communicates (via a serial port) with an electronic device that I developed myself.

When the PC application starts, it opens the available COM ports one by one and sends a certain line (for example, what are you?).

My device is programmed to answer this "magic question" with its own identifier (for example: "I am an evil device for monitoring employees").

When my PC software receives this โ€œmagic answerโ€, it starts working fine and no longer searches for other ports.

Of course, I do not use silly questions in data transfer, this is just an example.

Question: can I send lines, for example, to all (not busy) ports to open my device?

If I can - what should this question line look like? Are there normalized standards?

This is not the first such project on my desk, but now I am developing a solution for a small industry, and I need to pay more attention to what my application does, because there may be other devices connected to COM ports, and I donโ€™t know how they are can react.

I'm sorry if I made some language mistakes, English is my third language.

Thanks for answers.

+2
source share
2 answers

No, you would not want to do this.


When you use a serial COM port, you do not know what is connected to the port. It may be nothing, your device, modem, mouse (although I believe that no one else uses it ...) or that you canโ€™t even imagine.

Imagine if it is connected to another computer (possibly a Linux / router), sending data may lead to the execution of "this command", thereby creating unexpected effects (although your program may not notice). Or, if it is an incorrectly programmed device, it can cause an error in the firmware, which will lead to a device failure and malfunction.

In short, in the worst case scenario, you can trigger a catastrophic event .


Serial (RS232 or UART I suppose) communications do not have an assigned transfer protocol (unlike USB, which has one) to identify the device, so it is not fault tolerant. The user must ensure that he has selected the correct port. This is probably one of the reasons manufacturers are choosing USB, and one of the reasons why USB is so popular, even for computer idiots.

+2
source

After Alvin Wong confirmed my concerns, I thought about it again and redesigned the device discovery features because I don't want to give up on this idea.

I will send a unique-complex identifier every 1-5 seconds from the device.

When my computer starts up, it will try to open all available ports in sequence and will listen to this identifier for several seconds.

If the application receives this identifier, it will be confirmed, this port number / name is correct, and I established a connection.

+5
source

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


All Articles