Solved

GetCommState() problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 48
Installing Microsoft R server on REDHAT or SUSE Linux 4 131
pre4 challenge 19 94
Change to event 1 101
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

895 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

13 Experts available now in Live!

Get 1:1 Help Now