Solved

GetCommState() problem

Posted on 1997-07-03
3
551 Views
Last Modified: 2013-11-19
After changing DCB info using CommConfigDialog(),I then call SetCommState to take effect these changes to the serial COM device.By closely monitoring(watching) the DCB var., I can see that these changes took effect by calling GetCommState().However, when I close the device(CloseHandle) and then re-opened it (CreateFile), a call to GetCommState() shows that  the changes I`ve made earlier with the CommConfigDialog() and SetCommState() are reset to default values with only the BaudRate element being retained correct.It seems that after closing the COM device, the settings made by the SetCommState() to the DCB (before closing it) is nullified except the BaudRate element. Is this an expected behavior? The documentation says that the a COM device, when re-opened, should inherit the values set up from the previous open operation which includes the config settins of the device control block(DCB)...etc.     Can somebody from the 'serial' world help me,please. Thanks in advance.
0
Comment
Question by:booker74
  • 2
3 Comments
 

Accepted Solution

by:
mueller earned 100 total points
ID: 1302451
Booker74,

   Having just recently worked with serial port communication and using the DCB structure, I think I can answer your question.  You are correct in saying that the baud rate gets saved after closing the handle to the comm port and re-opening it.  However, in my application, I was also able to have the other settings stay the same as well, such as the parity, byte size, and stop bits, just to mention a few.  So yes, the documentation is correct in saying that the settings are restored/inherited when a COM device is re-opened.  As for why your program is not following the expected behavior, in my program I usually did something along these lines to get and modify the DCB...

---------------------------------------------------------------------------------
HANDLE hCommPort;
DCB dcb;

// get the current comm state
BOOL bStatus = GetCommState( hCommPort, &dcb );

// set the baud rate
dcb.BaudRate = (DWORD) 9600;

// update the comm state with my new change to the baud rate
bStatus = SetCommState( hCommPort, &dcb );
-----------------------------------------------------------------------------------

Again, that's an easy example of the code I did for this that would reset the baud rate.  In your situation, try setting the DCB manually and then making your call to SetCommState().  Hopefully that will help you out.


                                                    Steve
0
 

Author Comment

by:booker74
ID: 1302452
Thanks,Steve! Have you already tried the CommConfigDialog() API? I am using VisualC++ 4.2 and my code looks roughly like the ff. (it allows the user to set COM device settings offered by the CommConfigDialog() API)COMMCONFIG m_stCommConfig;HANDLE hCurCommDev;// Open port here .   //nothing fancy, I just folowed what is said in the doc...// Initialize some COMMCONFIG structure's necessary elements       m_stCommConfig.dwSize = sizeof(m_stCommConfig);
m_stCommConfig.wVersion =1;
m_stCommConfig.dwProviderSubType = PST_RS232;            
// just to make sure it would be an RS_232 device
// Get the current DCB settings of the selected deviceif (!GetCommState(hCurCommDev, &m_stCommConfig.dcb))
{  // handle error here
}
// make the current active window the parent of CommConfigDialogHWND hConnectionWin;
hConnectionWin=::GetActiveWindow();
ASSERT(hConnectionWin!=NULL);

// call CommConfigDialog to allow user settings
if (!CommConfigDialog(sCurCommPort,                      hConnectionWin,&m_stCommConfig))
{  //handle error here}
/* NOTE: When I watch the m_stCommConfig.dcb variable here,   it shows the correct values that have been set by the   user (BaudRate, Parity, StopBits, ByteSize etc...)   This is true anywhere below this code, before   closing the device by calling the CloseHandle();
// call SetCommState to set the new user settings
if (!SetCommState(hCurCommDev,&m_stCommConfig.dcb))
{  //handle error here}
/* NOTE AGAIN:If I tried GetCommState() here, it still shows user-set values  done in  the CommConfigDialog() */
// close the device hereCloseHandle(hCurCommDev);/* UPPS: After this, and I tried to call the function above(re-open the port), the value returned by the GetCommState(...) shows only the user-set BaudRate element being restored correctly. Others (i.e. Parity, StopBits,etc..) are back to their default values. */I just don't see why is it so. And why manual setting of DCB elements would make any difference than using the CommConfigDialog() API?Thanks in advance,Booker
0
 

Author Comment

by:booker74
ID: 1302453
Thanks,Steve! Have you already tried the CommConfigDialog() API? I am using VisualC++ 4.2 and my code looks roughly like the ff. (it allows the user to set COM device settings offered by the CommConfigDialog() API)COMMCONFIG m_stCommConfig;HANDLE hCurCommDev;// Open port here .   //nothing fancy, I just folowed what is said in the doc...// Initialize some COMMCONFIG structure's necessary elements       m_stCommConfig.dwSize = sizeof(m_stCommConfig);
m_stCommConfig.wVersion =1;
m_stCommConfig.dwProviderSubType = PST_RS232;            
// just to make sure it would be an RS_232 device
// Get the current DCB settings of the selected deviceif (!GetCommState(hCurCommDev, &m_stCommConfig.dcb))
{  // handle error here
}
// make the current active window the parent of CommConfigDialogHWND hConnectionWin;
hConnectionWin=::GetActiveWindow();
ASSERT(hConnectionWin!=NULL);

// call CommConfigDialog to allow user settings
if (!CommConfigDialog(sCurCommPort,                      hConnectionWin,&m_stCommConfig))
{  //handle error here}
/* NOTE: When I watch the m_stCommConfig.dcb variable here,   it shows the correct values that have been set by the   user (BaudRate, Parity, StopBits, ByteSize etc...)   This is true anywhere below this code, before   closing the device by calling the CloseHandle();
// call SetCommState to set the new user settings
if (!SetCommState(hCurCommDev,&m_stCommConfig.dcb))
{  //handle error here}
/* NOTE AGAIN:If I tried GetCommState() here, it still shows user-set values  done in  the CommConfigDialog() */
// close the device hereCloseHandle(hCurCommDev);/* UPPS: After this, and I tried to call the function above(re-open the port), the value returned by the GetCommState(...) shows only the user-set BaudRate element being restored correctly. Others (i.e. Parity, StopBits,etc..) are back to their default values. */I just don't see why is it so. And why manual setting of DCB elements would make any difference than using the CommConfigDialog() API?Thanks in advance,Booker
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

8 Experts available now in Live!

Get 1:1 Help Now