Solved

GetCommState() problem

Posted on 1997-07-03
3
563 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 114
NotAlone Challenge 20 84
scoreUp challenge 14 63
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 103
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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