简体   繁体   中英

CreateFile COM port error 2

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM