Link to home
Start Free TrialLog in
Avatar of judico
judico

asked on

Can the program change the number of the COM port?

I understood from a previous discussion (drichards) that seting up the proper values of the baud rate, data bits, parity, stop bits and flow control is done from within the program through the method 'ConfigureSerialInterface' and it overrides the settings in the Control Panel. I tried it and it does work. However, it appears that 'ConfigureSerialInterface' does not override the number of the COM port preset in the Control Panel (if COM port is set, say, to COM4, the program will not override it and turn it into COM1).

I'm using a RS232-USB cable and am noticing that every time the cable is connected to the USB port the number of the COM port seems to be changing occasionally. Furthermore, even if I change to COM1 manually the COM port which corresponds to the RS232-USB cable (COM1 is the number which the program requires -- it is hardcoded in the program), there is another, native, COM1 port existing which conflicts with the new one The device will not work until I uninstal that native COM1 port in the Device Manager.

Is there anything that could be done to avoid the manual resetting of the new (RS232-USB) and uninstalling the native COM1 port?

Avatar of drichards
drichards

You can easily modify the program to open whatever serial port you want.  In 'ConfigureSerialInterface', the call to 'CreateFile' takes a parameter that is currently hardcoded to "COM1".  You can have a combobox or something in the UI to let the user select the port number.  Best bet is to pass an integer parameter that is the COM port number and generate a string to pass to CreateFile.  For instance:

----------------------------------------------------------------------------
PASS_FAIL MultiMeter::ConfigureSerialInterface(int aPortNum)
{
    char portName[32];

    // Using stdio.h - need to "#include <stdio.h>" at top of file ...
    ::sprintf(portName, "COM%d", aPortNum);

    // Or using managed StringBuilder and converting to unmanaged char array...
    System::Text::StringBuilder *port = new System::Text::StringBuilder(S"COM");
    port->Append(__box(aPortNum));
    System::Text::ASCIIEncoding *enc = new System::Text::ASCIIEncoding;
    unsigned char portChars __gc[] = enc->GetBytes(port->ToString());
    for ( int ii = 0; ii < portChars->Count; ii++ ) portName[ii] = portChars[ii];
    portName[ii] = 0;
    // Using managed StringBuilder is not necessary here since we are building the
    // string from scratch.  I just show it as an example in case you are starting from
    // a managed string at some point.

    m_hComm = 0;
    m_hComm = CreateFile(portName, GENERIC_READ | GENERIC_WRITE,
                                       0, NULL, OPEN_EXISTING, 0, NULL);
...
-------------------------------------------------------------------------

You need only use one of the two methods shown for building portName.  How you generate the aPortNum parameter is up to you.  If aPortNum is 4, for example, the code will open COM4.  You could also populate a listbox or combobox with port names and pass the managed String as a parameter (call it aPortString here) instead of the int.  Then you would use the ASCIIEncoding as above:

    System::Text::ASCIIEncoding *enc = new System::Text::ASCIIEncoding;
    unsigned char portChars __gc[] = enc->GetBytes(aPortString);  // aPortString is parameter to ConfigureSerialInterface
    for ( int ii = 0; ii < portChars->Count; ii++ ) portName[ii] = portChars[ii];
    portName[ii] = 0;
Avatar of judico

ASKER

I will accept the answer but before that I'd like to see whether I understand it correctly. Is it true that I cannot impose automatically the hardcoded COM1 on the system from within my program and the only way for the COM port to be changed is to have a combobox (or the like) to have the user change it from without?. This I don't want. I don't want the user to have anything to do with changing COM ports.

Aslo, do I understand it correctly -- there is no way to uninstall automatically, from within the program, the competing COM1 port ?
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of judico

ASKER

One concern that I have is that it seems the USB-RS232 changes the port number somewhat randomly and unpredictably. Thus, even if I hardcode the COM port to, say, COM4, it is not always certain that the program will work -- suppose next time I connect the USB-RS232 cable it will set itself up as COM3. See, the thing is, I'd like to make this acquisitoin as user-friendly as possible whereby the user wouldn't need to know anything about how the computer communicates with the device.
Yes, that's what I was asking about a driver for the USB-RS232 cable thingy.  Is there software for it or does it just magically attach itself to a COM port?  If you have no control over which port it attaches to, you have no choice but to have a port selection mechanism in your program.  I find that behavior odd, however.  It seems like there should be some way to force it onto a specific port.  Then again, I've seen stranger things...
Avatar of judico

ASKER

There should probably be a port selection mechanism in my program because the cable does require a special software to be installed before it can function and probably that software does something that causes the randomness in selecting the port. The best way, of course, is to avoid such intermediate cables and have a device that directly connects to the computer via USB protocol. This is for some future project, though ...

By the way, do you know if Java would be better in handling these serial communications. I'm hearing that iunlike the .NET environment Java is truly multiplatform (I'm having problems running VB.NET programs in 98SE environment because 98SE doesn't have, for instance, opacity). Do you know anything more about that? If Java handles serial ports better I should probably start learning it and should abandon the .NET languages. What do you think?


P.S. I was wondering if you could show me the code you mentioned once you have for serial communication.
I'd check the manual for the USB-RS232 cable software.  It may tell you how to lock down the port number.

As for the Java question, that's not easily answered.  Java does have serial port implementation for Windows:

http://java.sun.com/products/javacomm/downloads/index.html

In general, however, if you are targeting Windows, stick with MS stuff - it works better on Windows than Java will.

If you are doing a multi-platform app, however, Java will obviously serve you better.  As far as the serial port access, however, you'll have to look around for your target platforms and see if an imlementation of the communications API is available.  Sun only has Windows/X86 and Solaris/Sparc available at the URL I listed.

I'll see what I can pull out of my code for you.  Is there something specific that you are interested in doing?  I would pull out specific sections for you as the overall code is pretty big - it uses async calls, there are several threads involved, and there's all the API hooks to interface with the rest of my app, and these will all serve to confuse the issue.
Avatar of judico

ASKER

See, there is an older RadioShack multimeter which works differently from the one we are discussing. The multimeter we are discussing seems to be sending data automatically to the port while the older model sends data only if a character is sent to it. I really got stuck when I tried to read data from it using a VB.NET program. See, for instance, this link:

https://www.experts-exchange.com/questions/21067826/Cannot-Concatenate-14-Incoming-Characters-into-One-Compact-14-Character-String.html

(I have abandoned this thread but now when I opened it to copy and paste the link I saw there is a new reply ... will have to see what it is ... although, I'm now on VC++.NET wavelength).

Also, I'd like to understand what approach would be better if I decide to use PIC's.

P.S. Please, let me know when you're going to post that code because I'd like to open a special question so that I can give you points.
Avatar of judico

ASKER

I just posted a question, seen in the following link:

https://www.experts-exchange.com/questions/21092168/Passing-of-values-between-functions.html

I would appreciate it very much if you could take a look at it since it is connected with the code we've been discussing here. Thanks in advance.