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


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

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 .

Re: Using CreateFile() from User Inputs 80 80

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(), ... );
Re: Using CreateFile() from User Inputs 80 80


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

Re: Using CreateFile() from User Inputs 80 80

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.

Re: Using CreateFile() from User Inputs 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.