@Avra , Thanks for jumping in here. I'm glad to get your take on this.
If you know that your users will always use some specific USB<>RS485 or similar adapter to connect to PLC, then you might query WMI to ask for manufacturer info of all serial ports and search for a match. Or you might ask the user to choose only the first time and then compare and match that info on all later attempts.
It will always be USB or RS485, no TCP. This is what I'm doing in the above code, searching all serial ports using PnPDeviceID to ID the device. The reason only one device shows in the screen shot is because the ShowMessage Dialog pops up immediately when found, and my device happened to be the first one found. after clicking OK, two more devices are listed in the Memo but they don't match the criteria, so it works as intended.
But there might be a better way if you only have one PLC, and that is to get a list of all COM ports, try to open them one by one, and for those that can be opened try to send manually a specific message that only PLC should be able to understand and reply (like MODBUS query - but do it before Pascal SCADA communication driver initialization), and see if an expected answer shows up.
That way you could identify COM port of a PLC without user's assistance, whatever serial adapter is used.
I like this way, and I thought about it myself, but wish I knew a couple things for sure... Maybe you can help answer?
(1) Is the PnPDeviceID subject to change at any time by the manufacturer eg with driver updates? If so, the current code is not ideal, as it could break. If it's written in stone for this manufacturer and every device of this make/model, then I'm good with code as is (as long as I also ID the serial number to ensure it's my product and not another unit of the same type.)
(2) If I choose the Modbus query way of ID'ing the device, that would just be a 03 Read register for a 1-byte device ID # and another a pair of 03 reads of the two 4-digit serial number locations. I could ship the units with the serial # in an inf or have user enter the number upon installation.
I assume that unexpectedly querying any device with a read request it may or may not understand can do no harm. Is that assumption correct? (I expect you'll answer in the affirmative or else you would not have suggested it!
) But in the case of a second unexpected device of the same type, it will answer, and so I must ascertain it's mine, or a different one, and serial number is the only way to know. It's unlikely, but probably should consider whether querying some other device while it's busy with a task (not mine) poses any potential problem.
(3) Must I ensure that I set the device's Modbus address to one that's not being used already, or does that matter only with TCP? I expect it's only a TCP issue, not serial?