In the previous article, I have described how we can find the Com Port Id of a USB Serial device (Arduino) with the help of VID and PID. If you have not seen this article you can read it, how to get Com Port Id of a USB Serial device.
In this article, I will describe how we can get com port properties using the win32 API. In windows, all Com Port properties (Baud rate, stop bit etc) is stored in the window registry. So to get the com properties you just need to read the windows registry.
I have already written an article, how we can read and write the Windows registry using the win32 API. If you are new and don’t know how to access the window registry using the win32 API. You can see this article, Access windows registry using the win32 API.
If you want to learn more about the Windows Internals or another technology, here 10 Free days trial for you.
So let us come on the topic and see the example code. In this example, I am reading the com port setting (baud rate, stop bit ..etc) using the win32 API.
For better indentation double click on the code,
#include "stdafx.h" #include <initguid.h> #include <windows.h> #include <Setupapi.h> #define TOTAL_BYTES_READ 48 #define OFFSET_BYTES 48 BOOL ReadComConfiguration(HKEY hKeyParent, PWCHAR subkey, PWCHAR valueName, PWCHAR* readData) { HKEY hKey; DWORD len = TOTAL_BYTES_READ; DWORD readDataLen = len; PWCHAR readBuffer = (PWCHAR)malloc(sizeof(PWCHAR) * len); if (readBuffer == NULL) { return FALSE; } // Check if the registry exists DWORD Ret = RegOpenKeyEx(hKeyParent, subkey, 0, KEY_READ, &hKey); if (Ret == ERROR_SUCCESS) { Ret = RegQueryValueEx(hKey, valueName, NULL, NULL, (BYTE*)readBuffer, &readDataLen); while (Ret == ERROR_MORE_DATA) { // Get a buffer that is big enough. len += OFFSET_BYTES; readBuffer = (PWCHAR)realloc(readBuffer, len); readDataLen = len; Ret = RegQueryValueEx(hKey, valueName, NULL, NULL, (BYTE*)readBuffer, &readDataLen); } if (Ret != ERROR_SUCCESS) { // close registry RegCloseKey(hKey); return false; } // copy read data *readData = readBuffer; // close registry RegCloseKey(hKey); return true; } else { return false; } } int _tmain(int argc, _TCHAR* argv[]) { // Com port TCHAR PortNo[] = L"COM1:"; int Status = 0; PWCHAR readMessage = nullptr; // Read com port configuration from the registry Status = ReadComConfiguration( HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Ports", PortNo, &readMessage); if (Status != TRUE) { return FALSE; } if (readMessage != nullptr) { // Display com status printf(" COM Status = %S\n", readMessage); free(readMessage); readMessage = nullptr; } return 0; }
See the below image if you will run the above code, you will get the all the setting.
After running the code,
After running the code,
- Best 5 C Books.
- Best programming mouse for developers.
- Get COM PORT of USB Serial Device using the VID and PID.
- Reading And Writing Windows Registry Using WinAPI
- Serial port programming using Win32 API.
- Install the port monitor silently without user interaction.
- C++ Interview Questions with Answers.
- 100 C interview questions, your interviewer might ask.
- C-Sharp Interview Questions.
- Python Interview Questions with Answer.
- Memory Layout in C.
- 100 C interview questions, your interviewer might ask.
- C Interview Questions for the experience.
- 10 questions about dynamic memory allocation
- File handling in C, in few hours