Here is my current code but trying all the COM Ports from 1-9 seems to only return an error and I'm not sure what's going on.
Code:
#include <string>#include <Windows.h>
#include <conio.h>
#include <iostream>
#include <sstream>
int main(){
//std::string com = "\\\\.\\COM";
std::string com = "COM";
std::string portname;
HANDLE hCom;
for( int i = 1; i<10; i++)
{
std::stringstream sstm;
std::cout << "Trying "<< i<<"\n";
sstm << com << i;
portname = sstm.str();
TCHAR *szPort = (TCHAR*)(portname.c_str());
//TCHAR *szPort = TEXT(port);
//wsprintf( szPort, "COM%d", nPort );
hCom = CreateFile(szPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
DWORD err=GetLastError();
std::cout << "Failed\n";
}
else
{
std::cout << i << " Didn't Fail o.o";
CloseHandle(hCom);
}
}
return 0;
}