P/Invoke 调用就停止了
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢 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 :)
从 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!