Solved

GetCommState() problem

Posted on 1997-07-03
3
567 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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
fizzArray  challenge 1 109
either24  challenge 19 105
EvenOdd challenge 10 148
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 187
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

696 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