09-01-2011 06:20 AM - edited 09-01-2011 06:20 AM
Hi all,
I do not understand how OpenCom[Config] works:
this routine takes 8 parameters. The first two are very confusing:
param_1: port number
param_2: com port name
Why do I have to specify both? I understand that port 1 is not necessarily COM1, but on my PC COM1 is broken and I have a USB emulator with 4 ports that have got names COM4 COM5 COM6 and COM7. I seem to only be able to make this OpenComConfig work with port Num=1 and name="COM4". For example, using OpenComConfig(4, NULL, ...) does not work. If I write a DLL that should be deployed on potentially different PCs, how do I know what port is associated with which COM# name? On my PC it seems that port 1 is COM4, but on a PC with a working COM1 I suppose COM4 would be port 2... Very confusing! Why on Earth doesn't OpenComConfig just take a COM port name??
Thanks
Marco
Solved! Go to Solution.
09-01-2011 10:13 AM
Ok,
this was simpler than I thought! Passing a port number of X is "asking" OpenComConfig to associate the "COM#" name with the handle X. This port is not the port number that the OS associates with the port name, but a "file descriptor" to be used for later calls. As if the current process had a table of "COM ports open" and we were requesting to associate the handle to that COM port with the entry X into that table. This is a "return" parameter, definitely not an "input parameter"! The documentation is a little confusing, but I was not helped by the fact that there was a bug in my code making it working only when apparently associating the "first available port" (COM4 in my case) with the "handle" 1.
For example, one may do
int comPort = 0;
openCom(name)
{
...
OpenComConfig(comPort++, name, ...);
return comPort-1;
}
and then
{
...
int com = openCom("COM4");
ComWrt(com, buf, len);
}
Regards
Marco