Solved

GetCommState() problem

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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