Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6942023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:57:42+00:00 2026-05-27T12:57:42+00:00

After I run Hyper Terminal or Docklight, my program works (writes and reads OK).

  • 0

After I run Hyper Terminal or Docklight, my program works (writes and reads OK). But if I never run any of those, all my reads fail.

So I guess I must be missing something in my serial port initialization code, but I can’t figure out what. Here it is:

Serial port initialization:

bool CSerialPortDrivenHardware::InitSerialPort(){
    m_SerialPort.Config( m_SerialPortConfig );
    bool success = ( m_SerialPort.Open( m_SerialPortName ) == TRUE );
    if( !success )
       Log( LOG_TYPE_ERROR, "CSerialPortDrivenHardware", "InitSerialPort", "Could not establish serial port connection" );
    else{
        m_SerialPort.SetControlBit( DTR_CONTROL_BIT );
        m_SerialPort.SetControlBit( RTS_CONTROL_BIT );      
    }
    return success;
}

Serial port class (relevant code):

BOOL CSerialPort::Config( SConfig sConfig ) {
    if( !m_hFile ) return FALSE;
    DCB dcb;
    if ( !::GetCommState( m_hFile, &dcb ) ) {
        m_dwLastError = ::GetLastError();
        return FALSE;
    }
    dcb.BaudRate = sConfig.dwBaudRate;
    dcb.Parity = sConfig.bParity;
    dcb.StopBits = sConfig.bStopBits;
    dcb.ByteSize = sConfig.bDataBits;
    if ( !::SetCommState( m_hFile, &dcb ) ) {
        m_dwLastError = ::GetLastError();
        return FALSE;
    }
    ::Sleep(200);
    return TRUE;
}

BOOL CSerialPort::Open( LPCSTR cszCOM ) {
    CHAR sPortName[256] = "\\\\.\\";
    strcat_s( sPortName, cszCOM );
    m_strPortName = sPortName;
    if( m_hFile )
        return FALSE;
    m_hFile = ::CreateFile( m_strPortName.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    if( m_hFile == INVALID_HANDLE_VALUE )
        m_hFile = NULL;
    if( !m_hFile )
        return FALSE;
    SetFlags( 0 );
    return TRUE;
}

void CSerialPort::SetFlags( DWORD dwFlags ) {
    if( dwFlags & READMODE_BLOCKING )
        SetTimeout( INFINITE );
    else
        SetTimeout( 0 );
}

void CSerialPort::SetTimeout( DWORD dwTimeout ) {
    COMMTIMEOUTS sCommTimeouts;
    if( !::GetCommTimeouts( m_hFile, &sCommTimeouts ) ) {
        m_dwLastError = ::GetLastError();
        return;
    }
    sCommTimeouts.ReadIntervalTimeout = 0;
    sCommTimeouts.ReadTotalTimeoutMultiplier = 0;
    sCommTimeouts.ReadTotalTimeoutConstant = dwTimeout;
    if ( !::SetCommTimeouts( m_hFile, &sCommTimeouts ) )
        m_dwLastError = ::GetLastError();
}

BOOL CSerialPort::SetControlBit( ESerialPortControlBit eControlBit ) {
    if( !m_hFile )
        return FALSE;
    BOOL bResult = FALSE;
    switch( eControlBit ) {
        case DTR_CONTROL_BIT:
            bResult = ::EscapeCommFunction( m_hFile, SETDTR );
            break;
        case RTS_CONTROL_BIT:
            bResult = ::EscapeCommFunction( m_hFile, SETRTS );
            break;
    }
    return bResult;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:57:43+00:00Added an answer on May 27, 2026 at 12:57 pm

    You should first open port, than configure. And your code does it in opposite direction. That’s why your configure code does not work and port just opens with default settings.

    Just look your code:

    BOOL CSerialPort::Config( SConfig sConfig ) {
        if( !m_hFile ) return FALSE;
     ....
    

    And m_hFile is set in CSerialPort::Open

    BOOL CSerialPort::Open( LPCSTR cszCOM ) {
        CHAR sPortName[256] = "\\\\.\\";
        strcat_s( sPortName, cszCOM );
        m_strPortName = sPortName;
        if( m_hFile )
            return FALSE;
        m_hFile = ::CreateFile( m_strPortName.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
     .....
    

    It seems your hyper terminal program adjust default settings and that’s why when you open port after using `hyper terminal it opens with correct settings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to run one program in myeclipse where after creating one web project
I have written a program which I run after connecting to the box over
Two related questions After I run a program how can I see what machine
I got build error after run in Snow Leopard (MacPort v.1.8.0) sudo port install
I have a script that needs to run after tomcat has finished starting up
I'm following this tutorial (seems good) for Rails. After I run ruby script/generate scaffold
I am using Struts application while running welcome page is run successfully after that
After our Ruby on Rails application has run for a while, it starts throwing
How to run function 5 seconds after pygtk widget is shown?
After getting a helpful answer here , I have run into yet another problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.