How can I Pull Current COM Port settings from Windows OS

Hey,
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. Any advice will be greatly helpful. I have never done any thing like this and am totally confused.

Thanks In Advance
Use CreateFile function to get a handle to the com port.
Then use GetCommState function to get com port current settings.

Use SetCommState function to set com port parameters.

See MSDN reference for the GetCommState function here:
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa363260%28v=vs.85%29.aspx

See MSDN reference for the SetCommState function here:
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa363436%28v=vs.85%29.aspx
Last edited on
Guestgulkan,
Thanks, 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

void PrintCommState(DCB dcb)
{
    //  Print some of the DCB structure values
    _tprintf( TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"), 
              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.\n", 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.\n", 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.\n", GetLastError());
      return (3);
   }

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

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

   PrintCommState(dcb);       //  Output to console

   _tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
   return (0);


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.

Any help will be greatly apprecitated!!!!!!!!!!!!
Tri to Initialize the dcb struct to 0:

DCB dcb = {0};
EssGeEich,
I tried that and it didn't work. It is stating that my current baud rate is 57600 but it is 4800 and it is stating that it changed it to 57600. My settings are not reflecting what the program is displaying. Do you have to have certain privillages on your computer for this to work like admin maybe. I am thinking I may not have rights to do this on my work computer. Do I need to include the SecureZeroMemory() method, I am not sure what it is doing though?

Any help will be greatly apprecitated!!!!!!!!!!!!
I don't think SecureZeroMemory will do anything. Try running your IDE/Program as Administrator anyways, it's no bad.
Ok, found out that I am Admin on my computer. So thats not the reason why it isn't working. Is there another way to retrieve/set current system COM Port settings?

Thanks In Advance
I haven't had a PC with serial ports for 5 years now - so I will have to try it out on ather PC but won't be until monday unfortunately.
Guestgulkan,
By any chance were you able to retrieve/set current system COM Port settings?

Thanks In Advance
Yes, I have done some tests.

Ran the program - the Set and Get CommState says the port particulars have changed but the Control Panel -> system -> Ports display does not update to reflect this ( I think that might be just the way it is)

EDIT:
I'm sure the values in Control Panel -> System ->Ports are just default (on windows startup) settings.
The value you get from GetCommState actually reflect the current port settings
(from the ports itself).

You can see what the actual port settings are by typing mode at a command prompt



Last edited on
Guestgulkan,
After doing some test with the Set and Get CommState method and getting port settings with the mode command, I totally agree with you. Thanks soooo much for giving me advice on this topic.




Topic archived. No new replies allowed.