Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How can I Pull Current COM Port settings from Windows OS

Hello,
I am working on this project where when a user selects a particular com port via a config dialog, the software needs to retrieve the OS COM Port settings for the particaul port that was selected. For example, If I select COM Port 1 on the config dialog in project, the software needs to retrieve the OS current COM Port 1 settings(Baud Rate, Data bits,Parity, Stop bit, and Flow Control). Once OS settings are retrieved, I neeed to update my project dialog with the OS COM Port Settings. Once dialog settings are updated to OS COM Port settings, users will have an opportuntiy to change current port settings by clicking a button. Once user has changed settings for a port, the OS COM Port current settings needs to be updated to what user input. With this in mind, I have been researching GetCommState and SetCommState. While researching I found the below code, I understand the code for the most part. However, I don't think it is doing what it says. It is suppose to be getting port settings and than changing settings to settings in program. When I view COM Port settings, my settings doesn't reflect what the code should have set it to. Can you please review code and give me input on what is going wrong.This is my first time ever doing something like this!!

Thanks In Adavance

[code]
#include
#include
#include

void PrintCommState(DCB dcb)
{
// Print some of the DCB structure values
_tprintf( TEXT("
BaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d
"),
dcb.BaudRate,
dcb.ByteSize,
dcb.Parity,
dcb.StopBits );
}


int _tmain( int argc, TCHAR *argv[] )
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM1"); // Most systems have a COM1 port

// Open a handle to the specified com port.
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL ); // hTemplate must be NULL for comm devices

if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.
", GetLastError());
return (1);
}

// Initialize the DCB structure.
//SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);

// Build on the current configuration by first retrieving all current
// settings.
fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.
", GetLastError());
return (2);
}

PrintCommState(dcb); // Output to console

// Fill in some DCB values and set the com state:
// 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_57600; // baud rate
dcb.ByteSize = 8; // data size, xmit and rcv
dcb.Parity = NOPARITY; // parity bit
dcb.StopBits = ONESTOPBIT; // stop bit

fSuccess = SetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.
", GetLastError());
return (3);
}

// Get the comm config again.
fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.
", GetLastError());
return (2);
}

PrintCommState(dcb); // Output to console

_tprintf (TEXT("Serial port %s successfully reconfigured.
"), pcCommPort);
return (0);
[/code]

I am programming on Windows XP. Also, I commented out SecureZeroMemory(&dcb, sizeof(DCB)); because I got an error stating that it wasn't declared.


Sign In or Register to comment.