C++使用 CreateFile 打开共享串行端口
我目前正在尝试使用 Windows 实现一个串行端口读取器类(已经使用 Boost 实现了一个,希望考虑到该平台可能没有 Boost 的可能性)。
一切似乎都运行良好,除了当我使用 Boost 串行端口编写器(单独的可执行文件)测试 Windows 串行端口读取器(可执行文件)时,编写器抛出访问被拒绝异常。否则,读取器会工作,因为它似乎正在读取串行端口上的任何内容,直到写入器无法再写入为止。
无论是先启动写入器还是先启动读取器,一旦读取器打开端口进行读取,写入器就无法再向该端口写入数据。
我尝试在 CreateFile() 方法中设置不同的参数,但到目前为止没有效果,我希望以下代码片段能够工作,但事实并非如此。
m_serial = CreateFile(
m_port,
GENERIC_READ,
FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
作者代码可能无关紧要,但无论如何:
void writeLine(std::string line)
{
boost::asio::io_service io;
boost::asio::serial_port serial(io, m_port);
serial.set_option(boost::asio::serial_port_base::baud_rate(m_rate));
boost::asio::write(serial, boost::asio::buffer(line.c_str(), line.size()));
}
boost::system::system_error 的 .what() 是:
open: Access is denied
有什么想法吗?
我看过这里: http://www.robbayer.com/files/serial-win .pdf (很棒的文章,但没有走那么远)和 http://msdn.microsoft.com/ en-us/library/windows/desktop/aa363858(v=vs.85).aspx - 似乎应该是 dwShareMode 参数,但我无法让它工作。
编辑:我应该提到我正在使用这个串行端口模拟器 http://www.eterlogic.com/Products .VSPE.html 这很可能是我的困境的原因,如果是这样那就没关系。不过,当将 Boost 阅读器与 Boost 编写器一起使用时(来自两个单独的可执行文件),模拟器会按预期工作。
I am currently trying to implement a serial port reader class using Windows (already implemented one using Boost, want to cater for the possibility that the platform may not have Boost).
All seems to be working well, except when I test my Windows serial port reader (executable) using my Boost serial port writer (a separate executable), the writer throws an access denied exception. Otherwise the reader works, as it seems to be reading whatever is on the serial port up until the point that the writer can no longer write.
It doesn't matter whether the writer is started first or the reader is started first, once the reader opens the port for reading, the writer can no longer write to that port.
I have tried setting different arguments in the CreateFile() method but to no avail so far, I would expect the following code snippet to work but it doesn't.
m_serial = CreateFile(
m_port,
GENERIC_READ,
FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
The writer code may be irrelevant but here it is anyway:
void writeLine(std::string line)
{
boost::asio::io_service io;
boost::asio::serial_port serial(io, m_port);
serial.set_option(boost::asio::serial_port_base::baud_rate(m_rate));
boost::asio::write(serial, boost::asio::buffer(line.c_str(), line.size()));
}
And the .what() for the boost::system::system_error is:
open: Access is denied
Any ideas?
I've looked here: http://www.robbayer.com/files/serial-win.pdf (great article, but doesn't go that far) and http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx - seems like it should be the dwShareMode argument but I can't get it working.
Edit: I should mention I am using this serial port emulator http://www.eterlogic.com/Products.VSPE.html which may well be the cause of my woes, if so then it doesn't matter. The emulator works as expected when using my Boost reader with my Boost writer though (from two separate executables).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以理解为什么现在还没有回复。根据记录,打开串行端口的参数很好。问题是我试图为每次读取打开相同的端口而不关闭它,所以我只是将其保持打开状态,直到完成为止。
I can understand why there have been no replies now. For the record, the arguments to open the serial port were fine. The problem was that I was attempting to open the same port for each read without closing it, so I've just left it open until I've completed finished with it.