P/Invoke 调用就停止了

发布于 2024-10-06 00:00:39 字数 1493 浏览 3 评论 0原文

我正在尝试通过 P/Invoke 将数据写入串行端口(明确不使用 SerialPort 类)。这是我到目前为止所得到的:

[DllImport("kernel32", SetLastError = true)]
static extern IntPtr CreateFile(string filename,       // file name
                                uint desiredAccess,    // read? write?
                                uint shareMode,        // sharing
                                uint attributes,       // SecurityAttributes pointer
                                uint creationDisposition,
                                uint flagsAndAttributes,
                                uint templateFile);

[DllImport("kernel32", SetLastError = true)]
static extern bool WriteFile(IntPtr hFile,               // handle to file
                             byte[] lpBuffer,            // data buffer
                             uint nBytesToWrite,         // number of bytes to write
                             out uint nBytesWritten,     // number of bytes written
                             [In] ref NativeOverlapped overlapped); // overlapped buffer

const uint OPEN_EXISTING = 3;

_portHandle = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
uint bytesWritten;
var buffer = Encoding.ASCII.GetBytes("foo\r");
var overlapped = new NativeOverlapped();
WriteFile(_portHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);

我得到一个 _portHandle != -1 并且可以调用 WriteFile。但调用后,什么也没做。我可以在 VS 调试模式下永远等待并等待调用返回。此外,也不会引发异常。

有 kernel32.dll 大师可以帮助我吗?

I'm trying to write data to a serial port via P/Invoke (explicitly not with the SerialPort-class). Here's what I have so far:

[DllImport("kernel32", SetLastError = true)]
static extern IntPtr CreateFile(string filename,       // file name
                                uint desiredAccess,    // read? write?
                                uint shareMode,        // sharing
                                uint attributes,       // SecurityAttributes pointer
                                uint creationDisposition,
                                uint flagsAndAttributes,
                                uint templateFile);

[DllImport("kernel32", SetLastError = true)]
static extern bool WriteFile(IntPtr hFile,               // handle to file
                             byte[] lpBuffer,            // data buffer
                             uint nBytesToWrite,         // number of bytes to write
                             out uint nBytesWritten,     // number of bytes written
                             [In] ref NativeOverlapped overlapped); // overlapped buffer

const uint OPEN_EXISTING = 3;

_portHandle = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
uint bytesWritten;
var buffer = Encoding.ASCII.GetBytes("foo\r");
var overlapped = new NativeOverlapped();
WriteFile(_portHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);

I get a _portHandle != -1 and can call WriteFile. But after the call, it does nothing. I can wait forever in VS-debug mode and wait for the call to return. Also, no exception is thrown.

Any kernel32.dll-masters out there who can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-10-13 00:00:39

感谢 Papa Mufflon 介绍了这个可用的项目,我自己衍生了这个项目,并从中制作了非常简单的开源项目 https:// github.com/ebraminio/PInvokeSerialPort,您也可以从 https://nuget.org/packages 下载它/PInvokeSerialPort

当然,这个项目尚未完成,因此任何建议或要求的功能对我来说都是有价值的:)

Thanks from Papa Mufflon for introducing that usable project, I derived that for myself and made very simple opensource project from it https://github.com/ebraminio/PInvokeSerialPort that you can also download it from https://nuget.org/packages/PInvokeSerialPort.

Of-course this project is not complete so any suggestion any feature requesting on it is valuable for me :)

魂ガ小子 2024-10-13 00:00:39

从 John Hind (http://msdn.microsoft.com/en-us/magazine/cc301786.aspx) 获取串行通信,而不是我自己开发:)

效果很好!

Taken Serial Comm from John Hind (http://msdn.microsoft.com/en-us/magazine/cc301786.aspx) instead of developing my own :)

Works great!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文