Thread: Serial Port Comms - How to "argument" the COM Port I want

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    3

    Serial Port Comms - How to "argument" the COM Port I want

    ...I've tried in a lot of ways (windows.... GOD)......

    ......the whole program only send a specified character through the serial port at a specified speed - I just want to be able to specify the COM Port as well through the command line as a parameter (using argv[] .....

    (otherwise I'll have to change the source code of the small program to the specified port whenever I change computer's and/or serial Port and/or usb Port (in the latest if using an adapter that's being not enumerated through its serial number) or whatever.. evertime the port needs to be changed....)

    This is the only line of the program that specifies the Port to be used.....any ideias on changing it?

    hSerial = CreateFile(
    "\\\\.\\COM10", GENERIC_READ|GENERIC_WRITE, 0, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );



    thabks take care!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,994
    How are strings in general represented in your program?

    char
    wchar_t
    TCHAR

    Is it being compiled with _UNICODE defined?

    Assuming char say, then
    Code:
    int main ( int argc, char *argv[] ) {
        char portname[100] = "\\\\.\\";
        if ( argc >= 2 ) {
          // command line arg is just say COM10
          strcat(portname,argv[1]);
        }
        hSerial = CreateFile(
    portname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
    
    }

    If your program begins with WinMain() and not main(), then start with GetCommandLine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    3
    It worked great thanks


    BUT.. Can you explain me why this didn't:
    Code:
    {
    char portname[20]=arv[1];
    
    hSerial = CreateFile("\\\\.\\%s", portname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
    }
    just because I think that "thecnicaly" it's the same you've done (but maybe it isn0't since it didn't work and your's did XD )


    EDITED:
    maybe it should be like this:
    hSerial = CreateFile("\\\\.\\%s", GENERIC_READ|GENERIC_WRITE, 0, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL , portname);

    thanks!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,994
    Because %s isn't something you can throw into any string in any function.

    Substituting one string in another with %s only works with printf / fprintf / sprintf.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    3
    FOR THOSE WHO JUST READ THIS IT WORKED PERFECTLY AS Salem SAID!



    thanks a lot! take care bro!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2009, 11:02 AM
  2. COM Port not accepting "11" bytes
    By LowlyIntern in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2008, 09:04 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. USB port "monitor"
    By Victor in forum Windows Programming
    Replies: 1
    Last Post: 04-13-2005, 05:45 AM

Tags for this Thread