本文介绍了CreateFile()串行通信问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过USB端口(名为COM15)进行一些串行通信,但出现错误.这是发生错误的代码:

I am trying to do some serial communication through my usb port (named COM15), and I am getting an error. This is the code where the error is occurring:

HANDLE myPortHandle = CreateFile("COM15",
                                  GENERIC_READ | GENERIC_WRITE,
                                  0,
                                  NULL,
                                  OPEN_EXISTING,
                                  0,
                                  NULL);

if (myPortHandle == INVALID_HANDLE_VALUE)
{
    DWORD lastError = GetLastError();
    cout<<"ERROR HERE! = "<<lastError<<endl;
}

每次编译程序时,打开的串口的句柄== INVALID_HANDLE_VALUE.我阅读了CreateFile()msdn文档,以使用GetLastError()来获取扩展错误信息".现在...当我运行代码时,GetLastError()返回值:2

Every time I compile the program, the handle to the opened serial port == INVALID_HANDLE_VALUE. I read in the CreateFile() msdn documentation to use GetLastError() in order to get "extended error information". Now... when I run the code, GetLastError() returns a value of: 2

在GetLastError()的msdn文档中,它说:

In the msdn documentation of GetLastError(), it says:

文档中用于设置最后一个错误代码的每个函数的返回值"部分都记录了该函数设置最后一个错误代码的条件."

"The Return Value section of the documentation for each function that sets the last-error code notes the conditions under which the function sets the last-error code."

我试图在CreateFile()msdn文档的返回值"部分中查找"2"的含义,但找不到任何地方的含义.

I tried looking for the meaning of the '2' in the Return Value section of the CreateFile() msdn documentation and couldn't find the meaning anywhere.

问题:

1)为什么会发生:myPortHandle == INVALID_HANDLE_VALUE?

1) Why is this happening: myPortHandle == INVALID_HANDLE_VALUE?

2)另外,如果有人可以引导我到需要查找msdn文档的地方,以找到GetLastError()返回的"2"的含义,那真是太棒了!

2) Also, if anyone could direct me to where I need to look in the msdn documentation to find the meaning of the '2' returned by GetLastError(), that would be awesome!

推荐答案

此处记录了Windows错误代码: http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms681382.aspx

Windows error codes are documented here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx

您的错误代码是ERROR_FILE_NOT_FOUND.这意味着找不到COM15.根据本文,您需要使用名称: "\\\\.\\COM15".

Your error code is ERROR_FILE_NOT_FOUND. Which means that COM15 cannot be found. According to this article you need to use the name: "\\\\.\\COM15".

当您将"COM1"至"COM9"使用为 文件名;但是,消息INVALID_HANDLE_VALUE是 如果使用"COM10"或更高版本,则返回.

CreateFile() is successful when you use "COM1" through "COM9" for the name of the file; however, the message INVALID_HANDLE_VALUE is returned if you use "COM10" or greater.

如果端口名称为\\.\COM10,则指定端口号的正确方法 调用CreateFile()的串行端口如下:

If the name of the port is \\.\COM10, the correct way to specify the serial port in a call to CreateFile() is as follows:

CreateFile(
  "\\\\.\\COM10",     // address of name of the communications device
  fdwAccess,          // access (read-write) mode
  0,                  // share mode
  NULL,               // address of security descriptor
  OPEN_EXISTING,      // how to create
  0,                  // file attributes
  NULL                // handle of file with attributes to copy
);

注释:此语法也适用于端口COM1至COM9.某些主板可以让您自己选择端口名称.这 语法也适用于这些名称.

NOTES: This syntax also works for ports COM1 through COM9. Certain boards will let you choose the port names yourself. This syntax works for those names as well.

或者从文档中选择 CreateFile 本身:

Or alternatively from the documentation to CreateFile itself:

要指定大于9的COM端口号,请使用以下语法: \\.\COM10.此语法适用于所有端口号和 允许指定COM端口号.

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.

这篇关于CreateFile()串行通信问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:18