Solved

Can the program change the number of the COM port?

Posted on 2004-08-11
9
549 Views
Last Modified: 2012-05-05
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?

0
Comment
Question by:judico
  • 5
  • 4
9 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11780403
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;
0
 

Author Comment

by:judico
ID: 11784395
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 ?
0
 
LVL 19

Accepted Solution

by:
drichards earned 125 total points
ID: 11785862
From within your program, you don't have any control over which serial port the device is using.  All you can do is control which port you open in the program.

The real question here is why does the device create a competing COM1?  Usually devices like that create a COM3 or COM4.  You indicated somewhere that the device is actually a USB device that connects to a virtual serial port.  There must be a driver to do that.  Is there a driver setting you can use to control the COM port number?  Then you can move the device to a new port and change your program to open the correct port.

And there probably is a way to disable or remove the real COM1 from inside your program, but I couldn't tell you offhand how to do it.  Try a Google search or an Experts Exchange search.  Someone has probably wanted to do that before.
0
 

Author Comment

by:judico
ID: 11786582
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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Expert Comment

by:drichards
ID: 11787579
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...
0
 

Author Comment

by:judico
ID: 11787892
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.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11788245
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.
0
 

Author Comment

by:judico
ID: 11788897
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:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21067826.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.
0
 

Author Comment

by:judico
ID: 11789019
I just posted a question, seen in the following link:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VisualC_PLUS_PLUS_DOT_NET/Q_21092168.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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now