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 .

Recommended Answers

All 4 Replies

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(), ... );

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.