Reputation: 611
I'm trying to open and write to a COM serial port on Windows 7, using below code in Visual Studio 2012 (C++)
printf("argv[2]= '%s'\n", argv[2]);
m_hCommPortSend= ::CreateFile(argv[2] ,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
if(m_hCommPortSend == INVALID_HANDLE_VALUE)
{
printf("%s error: %d\n", argv[2], GetLastError());
return -1;
}
This Code works well with argv[2] as COM1 or COM7
I have another com port COM39, When I pass COM39 as argv[2] it is not openeded GetLastError() returning 2.
Not sure why ?
Upvotes: 1
Views: 3714
Reputation: 10415
The MSDN page for CreateFile says the following under Communications Resources:
To specify a COM port number greater than 9, use the following syntax: \\.\COM10
. This syntax works for all port numbers and hardware that allows COM port numbers to be specified.
Upvotes: 6