Timeout exception when writing to a virtual COM

巧了我就是萌 提交于 2019-12-14 03:12:34

问题


I am trying to write to COM port using .net sample

I have no problem to write to a real COM, but when I try to write to a virtual COM I get a timeout exception

Unhandled Exception: System.TimeoutException: The write timed out. at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count , Int32 timeout) at System.IO.Ports.SerialPort.Write(String text) at System.IO.Ports.SerialPort.WriteLine(String text)

I googled the issue, and found this. according to what is said there this is the issue:

.net's write() function is using CreateFile and WriteFile Async like this :

CreateFile("\\\\.\\" + portName,
            NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE,
            0,    // comm devices must be opened w/exclusive-access
            IntPtr.Zero, // no security attributes
            UnsafeNativeMethods.OPEN_EXISTING, // comm devices must use OPEN_EXISTING
            FILE_FLAG_OVERLAPPED,
            IntPtr.Zero  // hTemplate must be NULL for comm devices
            );

WriteFile(_handle, p + offset, count, IntPtr.Zero, overlapped);

when I used those I had NULL instead of the FILE_FLAG_OVERLAPPE

My question is, how can I overcome this issue? do I have to write my own code?

来源:https://stackoverflow.com/questions/18746980/timeout-exception-when-writing-to-a-virtual-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!