问题
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