Hi,
Im quite new to SerilaPort handling.Im trying to open a serial communication on a particular COM port.Im able to Open a new one by using

CreateFile(L\\\\.\\Com5,

                          GENERIC_READ | GENERIC_WRITE,
                          0, // exclusive access
                          NULL, // no security
                          OPEN_EXISTING,
                          0, // no overlapped I/O
                          NULL);

But I want to do this by getting the user input on the console ,say 'x' COM and then initialize it.Kindly help me with the way i should concatenate the com number'x' with L\\.\Com .

Just get the port number from the user then format a string, something like this. The code may not be exactly correct, I didn't test it.

int port = 0;
cout << "Enter port number\n";
cin >> port;
stringstream str;
str << "\\\\.\\Com";
str << port;

CreateFile(str.str(), ... );

Edited 2 Years Ago by Ancient Dragon

Hi,

The first argument should be something like L"\\\\.\\Com5" .Please Note L should be outside ""

Something like I posted WILL make the first argument like that. You don't have to use string literals as the first argument -- it can be a formatted char array.

The L prefix denotes a wide character/string literal. That is, it is of type wchar_t instead of char. Also, to reiterate the L prefix is only used with literals.

#include <iostream>
#include <string>

int main() {
  std::wstring comport;
  std::wcout << L"Enter your comport: "; 
  std::wcin >> comport;
  std::wcout << L"Com Port is: " << comport << std::endl;
}
This article has been dead for over six months. Start a new discussion instead.