简体   繁体   English

CreateFile无法共享串行(COM)端口

[英]CreateFile not able to share serial (COM) port

The CreateFile function is useful for opening files or devices for read/write access, providing a handle. CreateFile函数对于打开文件或设备以进行读/写访问(提供句柄)很有用。

The third parameter, dwShareMode , specifies if the file/device can later be accessed by others. 第三个参数dwShareMode ,指定文件/设备以后是否可以被其他人访问。 An example, with files: 带有文件的示例:

void* pFileHandle1 = ::CreateFileA("C:\\test.txt", GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
DWORD lastError = GetLastError(); // 0, ERROR_SUCCESS
void* pFileHandle2 = ::CreateFileA("C:\\test.txt", GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
lastError = GetLastError(); // 0, ERROR_SUCCESS

All good here: we have 2 different handles that can read/write a single file. 一切都很好:我们有2个可以读取/写入单个文件的不同句柄。

But in my case, I want to use a COM port: 但就我而言,我想使用一个COM端口:

void* pComHandle1 = ::CreateFileA("\\\\.\\COM3", GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
lastError = GetLastError(); // 0, ERROR_SUCCESS
void* pComHandle2 = ::CreateFileA("\\\\.\\COM3", GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
lastError = GetLastError(); // 5, ERROR_ACCESS_DENIED Oops!

The first handle is valid and can be used, but the second one is INVALID_HANDLE_VALUE. 第一个句柄有效并且可以使用,但是第二个句柄是INVALID_HANDLE_VALUE。

What's up with that? 那是怎么回事? Can't you share COM ports that way? 您不能以这种方式共享COM端口吗?

Quoting the documentation for CreateFile : 引用CreateFile的文档:

The CreateFile function can create a handle to a communications resource, such as the serial port COM1 . CreateFile函数可以创建通信资源的句柄,例如串行端口COM1 For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING , the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL . 对于通信资源, dwCreationDisposition参数必须为OPEN_EXISTINGdwShareMode参数必须为零(独占访问),并且hTemplateFile参数必须为NULL Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O. 可以指定读,写或读/写访问权限,并且可以为重叠的I / O打开句柄。

The implication from the documentation here is that communication objects cannot be shared like ordinary files. 这里文档的含义是,不能像普通文件一样共享通信对象。 The Windows API leaves it to whoever opened the port to decide how/if they want to share access to that resource, and leaves them to manage the consequences of that decision. Windows API将其留给开放端口的人来决定如何/是否希望共享对该资源的访问,并留给他们来管理该决定的后果。

To share the port, you can use DuplicateHandle and pass that to whoever you want to grant access to the port after you've opened it. 要共享端口,可以使用DuplicateHandle并将其传递给您要在打开端口后授予访问权限的任何人。 For further reading, check out this ancient article from MSDN 要进一步阅读,请查阅MSDN上的这篇古老文章

That said, if you want to share a COM port across multiple processes, you're better off opening it in only one of them, and using some form of IPC to transfer data. 就是说,如果要跨多个进程共享一个COM端口,最好只在其中一个进程中打开它,并使用某种形式的IPC来传输数据。 Let one process handle servicing the port. 让一个进程处理端口的维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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