CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2002
    Posts
    3

    Unhappy More then 16 serial Ports

    Hi,

    I've more then 16 serial Ports in my PC (COM1+2 and COM5 to COM20). I work with MSVC++ 6.0 under WIN2000. With MSCOMM I cant see the ports 17 to 20. If I use CSerial I cant see Ports higher 9. What's wrong?

    Here a little CODE with CSerial:

    CString text;
    CEdit* pEdit = (CEdit*) GetDlgItem(IDC_EDIT1);
    CSerial Serial;
    for (int i=1;i<25;i++)
    {
    if (Serial.Open( i, 9600))
    text.Format("Der COM-Port: %i ist vorhanden", i);
    else
    text.Format("Der COM-Port: %i ist NICHT vorhanden", i);
    pEdit->SetWindowText(text);
    pEdit->RedrawWindow(NULL,NULL,RDW_UPDATENOW);
    Sleep(500);
    Serial.Close();
    }

    Who can help me?

    Greetings Walter

  2. #2
    Join Date
    Jun 2002
    Location
    Cambridge, UK
    Posts
    28

    Smile

    Comm port names >COM9 are recognised with a long file naming system, look in the MSDN help, it is there her is a code fragment/func

    // ----------------------------------- FixComPortName -----------------------
    // fix com port names for port names above com9
    // IN/OUT: portName - user displayable name
    //
    // RETURN: portName - is modified on return.
    // NOTES: This function will accept a port name in the format "\\.\COMnn"
    // without lengthening it in-correctly.
    CHAR * FixComPortName(CHAR *portName)
    {
    CHAR tempPortName[MAX_PORT_NAME];

    if (strlen(portName) > strlen("COM1"))
    {
    if (0==strncmp(portName, "COM",3))
    {
    sprintf(tempPortName, "\\\\.\\%s", portName);
    strcpy(portName, tempPortName);
    }
    }
    return (portName);
    } // FixComPortName

    notice how I have added a "\\.\" to the front of the name, that is all there is to it, you only need to do this if the port name is longer than 1 char (go figure).

    Enjoy.

  3. #3
    Join Date
    Apr 2001
    Posts
    222
    Hey - I just looked at my PC and guess what I found...i've got 41 serial ports! And I can't access any of them! :-)

    Seriously though Intel wanted to remove all serial port, parallel port and floppy drive support from their mother boards and good for them I say - it's about time we killed off such ancient devices in favour of USB, 1394, gigabit ethernet and wireless WAN.

    Still some people need to support these legacy devices so I expect we will be seeing them around for a while yet! :-)

    Adrian

  4. #4
    Join Date
    Jun 2002
    Location
    Cambridge, UK
    Posts
    28

    Unhappy ripper

    I dunno who sold you the USB gospel, but I do not buy it. as u said many people will still have serious applications for Serial and printer ports.
    Once USB gets some serious standardization going, we can talk again. Serious H/Ware applications only accept stable standards and USB is probably the next to be accepted as it is only now comming of age. Interresting how software systems are doing just the same thing as is hardware, evolving to meet our needs, bowing down to us programmer gods/gurus like humble little servants ##SNAP## Oops dream world.

    Ultimately wireless may rule the day. Untill then we can only play with the toys we are given.

  5. #5
    Join Date
    Apr 2001
    Posts
    222
    Interesting to note that the Apple no longer supports serial ports and floppies.

    Anyway it is Intel who want to kill them off - I think if people want to use these legacy devices they can buy a special plugin card. The 5.25 inch floppy is dead and hopefully the other legacy devices will follow soon.

    Anyway - this is where I read it - there are articles around elsewhere too.

    http://www.theregister.co.uk/content/archive/22034.html

    Adrian

  6. #6
    Join Date
    Aug 2002
    Posts
    3

    Arrow

    Hi Conrad,

    Serial.Open(i, 9600) <== i is an INTEGER as COM-Port in my CODE!
    I can't use your example.

    In CSerial the following is defined (but I don't understand the hole CODE, I'm a beginner:

    BOOL CSerial::Open( int nPort, int nBaud )
    {

    if( m_bOpened ) return( TRUE );

    char szPort[15];
    char szComParams[50];
    DCB dcb;

    wsprintf( szPort, "COM%d", nPort );
    m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
    if( m_hIDComDev == NULL ) return( FALSE );

    memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
    memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

    COMMTIMEOUTS CommTimeOuts;
    CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
    CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
    CommTimeOuts.ReadTotalTimeoutConstant = 0;
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
    CommTimeOuts.WriteTotalTimeoutConstant = 5000;
    SetCommTimeouts( m_hIDComDev, &CommTimeOuts );

    wsprintf( szComParams, "COM%d:%d,n,8,1", nPort, nBaud );

    m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
    m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

    dcb.DCBlength = sizeof( DCB );
    GetCommState( m_hIDComDev, &dcb );
    dcb.BaudRate = nBaud;
    dcb.ByteSize = 8;
    unsigned char ucSet;
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
    if( !SetCommState( m_hIDComDev, &dcb ) ||
    !SetupComm( m_hIDComDev, 10000, 10000 ) ||
    m_OverlappedRead.hEvent == NULL ||
    m_OverlappedWrite.hEvent == NULL ){
    DWORD dwError = GetLastError();
    if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
    if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
    CloseHandle( m_hIDComDev );
    return( FALSE );
    }

    m_bOpened = TRUE;

    return( m_bOpened );

    }

    Please excuse my bad english!

    Thanks Walter
    Last edited by edvmesstec; August 23rd, 2002 at 05:39 AM.

  7. #7
    Join Date
    Jun 2002
    Location
    Cambridge, UK
    Posts
    28

    Smile bug in the class is fixed here...

    Quite simply you need to change the code in CSerial::Open, to
    if (nPort < 10)
    wsprintf( szPort, "COM%d", nPort );
    else
    wsprintf( szPort, "\\\\.\\COM%d", nPort );

    That should be self-explanatory from my sample code, which accepts the string "COM1" etc and "fixes" it.

    The cool code with overlapped I/O is all very fancy, and if U read all of the overlapped I/O references this work-around is also documented there. It is called a UNC filename thingy?

    Enjoy

  8. #8
    Join Date
    Aug 2002
    Posts
    3

    Question

    Hi Conrad,

    I'm back from hollydays.
    I've made this changes. But Ports > 9 are not enabled. I've looked at my system and the Ports 1,2,5-20 are ready for use. My little testprogramm ist showing this:
    Port 1 ist open
    Port 2 ist open
    Port 3 ist not open
    Port 4 ist not open
    Port 5 ist open
    .....
    Port 9 ist open
    Port 10 ist not open
    Port 11 ist not open
    .....
    Port 25 ist not open

    Do you have a idea?

    Greate thanks

    Walter

  9. #9
    Join Date
    Jun 2002
    Location
    Cambridge, UK
    Posts
    28
    Yep, like I said, if the port # is grater than 9, U need to use the\\..\ format for the name, search youir MSDN for
    HOWTO: Specify Serial Ports Larger than COM9 the answer in techincal terms is there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured