Header
#pragma once
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <WinBase.h>
using namespace std;
class SerialPort {
public:
BOOL COM_exists(int port);
};
cpp
#include "stdafx.h"
#include <string.h>
#include "SerialPort.h"
using namespace std;
BOOL SerialPort::COM_exists(int port)
{
char buffer[7];
COMMCONFIG CommConfig;
DWORD size;
if (!(1 <= port && port <= 255))
{
return FALSE;
}
snprintf(buffer, sizeof buffer, "COM%d", port);
size = sizeof CommConfig;
// COM port exists if GetDefaultCommConfig returns TRUE
// or changes <size> to indicate COMMCONFIG buffer too small.
return (GetDefaultCommConfig(L"buffer", &CommConfig, &size)
|| size > sizeof CommConfig);
}
main
#include "stdafx.h"
#include <iostream>
#include "SerialPort.h"
using namespace std;
if(num==1)
{
int i;
for (i = 1; i < 256; ++i)
{
if (COM_exists(i))
{
cout <<"COM%d \n";
}
}
}
I want listing serial ports on Windows ,but I'm having a hard time. Help me list the serial ports. error code = c3861 ,e0020
I want listing serial ports on windows. The end of the road ,Select a serial port that can be connected to enable communication. please help me