WDK Virtual Serial Example Not Working

Hello.

I’m working on a VirtualSerial driver.

I start from “Virtual serial driver sample” of “windows-driver-kit-81-cpp.zip” and run without modification.

I’m able to install and debug example on VMware Windows 7 virtual machine.

After running, driver example create a new COM port (COM12 on my PC).

If I open virtual COM port from some serial port terminal (Hercules for example) every thing it’s ok.

I got problems if I open COM port from my own Java program or my owm VB.Net program.

VB.Net cannot open COM port (I got problem with SerialPort.Open).

I think Microsoft VirtualSerial example is bugged. I wrote to Microsoft support but I got no usefull reply.

This is source code of my Console application example:

Imports System.IO.Ports
Imports System.Threading
Imports sql.lhr.net

Module Module1

Dim uartReceived As Boolean
Dim _serialPort As SerialPort

Sub Main()

’ Create a new SerialPort object with default settings.
_serialPort = New SerialPort()

’ Allow the user to set the appropriate properties.
_serialPort.PortName = “COM12”
_serialPort.BaudRate = 115200
_serialPort.Parity = IO.Ports.Parity.None
_serialPort.DataBits = 8
_serialPort.StopBits = 1
_serialPort.Handshake = Handshake.None

’ Set the read/write timeouts
_serialPort.ReadTimeout = 500
_serialPort.WriteTimeout = 500

Dim ret() As Byte = UserDefinedFunctions.CreateTransport(1, 0, UserDefinedFunctions.CreateSimplyMxpPacket21(1234567890123456, UserDefinedFunctions.CreateMxpCommand2(2, 0, hexStringToByteArray(“0x”), 1), 1, 0), 2)

Console.WriteLine(“Open COM12”)

_serialPort.Open()

uartReceived = False

Console.WriteLine("Write: " + ret.ToString)

_serialPort.Write(ret.ToString)

Console.WriteLine(“Wait for receive”)

While (Not uartReceived)

End While

Console.WriteLine(“Received”)
Console.WriteLine(“Close COM12”)

_serialPort.Close()

End Sub

End Module

>If I open virtual COM port from some serial port terminal (Hercules for example)
every thing it’s ok.

So the driver is working.

I got problems if I open COM port from my own Java program or my owm VB.Net
program.

VB.Net cannot open COM port (I got problem with SerialPort.Open).

The very first thing to do in that case is to get the error code. How could you investigate without the error code ?

Use createfile to get a handle with proper permissions , if it give proper
success handle were opened or else it’s not opened , call getlasterrorcode
to check the ntstatus value to debug it mode

On 31 Mar 2017 4:14 a.m., wrote:

> >If I open virtual COM port from some serial port terminal (Hercules for
> example)
> every thing it’s ok.
>
> So the driver is working.
>
> >I got problems if I open COM port from my own Java program or my owm
> VB.Net
> program.
>
> >VB.Net cannot open COM port (I got problem with SerialPort.Open).
>
> The very first thing to do in that case is to get the error code. How
> could you investigate without the error code ?
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer&gt;
></http:></http:>

I made these three test situation:

  1. Windows 7 VMWare Virtual Machine with Virtual Serial driver example by Microchip. VirtualSerial got COM name “COM13”.
    From a serial terminal program (Hercules) I connect to COM13, send some data and receive data.
    Everything is working perfectly.

  2. Windows 7 VMWare Virtual Machine with Virtual Serial driver example by Microchip. VirtualSerial got COM name “COM13”.
    From a Java program I try to open COM13, send some data and receive some data.
    Opening COM13 works; send data works (I put breakpoint in driver code and check it).
    When driver try to send data to Java, in “queue.cpp” file, “CMyQueue::OnWrite” funct, the call
    “hr = m_FxReadQueue->RetrieveNextRequest(&pSavedRequest)” fail.
    I got result error code “hr = 0x80070103”.

  3. Windows 7 VMWare Virtual Machine with Virtual Serial driver example by Microchip. VirtualSerial got COM name “COM13”.
    From VB.Net code I try to open COM13. I got exception in “_serialPort.Open()”.

Open COM13: exception System.ArgumentException: The given port name does not start with COM/com or does not resolve to a valid serial port.
Parameter name: portName
at System.IO.Ports.SerialStream…ctor(String portName, Int32 baudRate, Parity
parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeou
t, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
at ConsoleApplication1.Module1.Main()

Update:

In my VB.Net console application I added this code:

Dim hFile As SafeFileHandle = CreateFile(“\.\COM13”, dwAccess, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero)

“CreateFile” return a valid handle (“hFile.IsInvalid” is false).

Then I call:

Dim fileType As Integer = GetFileType(hFile)

If (fileType <> 2) AndAlso (fileType <> 0) Then
Console.WriteLine(“Invalid Serial Port”)
End
End If

I got result “fileType = 1”, while I expect “fileType = 2” OR “fileType = 0”.

In driver source code, where I have to set this behavior?

xxxxx@metasystem.it wrote:

In my VB.Net console application I added this code:

Dim hFile As SafeFileHandle = CreateFile(“\.\COM13”, dwAccess, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero)

“CreateFile” return a valid handle (“hFile.IsInvalid” is false).

Then I call:

Dim fileType As Integer = GetFileType(hFile)

If (fileType <> 2) AndAlso (fileType <> 0) Then
Console.WriteLine(“Invalid Serial Port”)
End
End If

I got result “fileType = 1”, while I expect “fileType = 2” OR “fileType = 0”.

In driver source code, where I have to set this behavior?

GetFileType is not a system API. Is that part of your code? What does
it do? Wouldn’t it be easier just to eliminate the check?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

GetFileType is a win32 API. For instance, cmd.exe uses it to validate it is opening a COM port. DeviceType of the device object being opened informs the return value. To override the DeviceType, UMDF requires that you set it in the INF. From the virtual serial port example on github, virtualserial2.inx

[SetDeviceType_AddReg]
HKR,DeviceType,0x10001,0x0000001b ; 0x1b = FILE_DEVICE_SERIAL_PORT

https://github.com/Microsoft/Windows-driver-samples/blob/master/serial/VirtualSerial2/ComPort/virtualserial2um.inx

I don’t know if the win8.1 version of the sample does this or not.

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Tim Roberts
Sent: Friday, March 31, 2017 10:35 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] WDK Virtual Serial Example Not Working

xxxxx@metasystem.it wrote:
> In my VB.Net console application I added this code:
>
> Dim hFile As SafeFileHandle = CreateFile(“\.\COM13”, dwAccess, 0,
> IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero)
>
> “CreateFile” return a valid handle (“hFile.IsInvalid” is false).
>
> Then I call:
>
> Dim fileType As Integer = GetFileType(hFile)
>
> If (fileType <> 2) AndAlso (fileType <> 0) Then
> Console.WriteLine(“Invalid Serial Port”)
> End
> End If
>
> I got result “fileType = 1”, while I expect “fileType = 2” OR “fileType = 0”.
>
> In driver source code, where I have to set this behavior?

GetFileType is not a system API. Is that part of your code? What does it do? Wouldn’t it be easier just to eliminate the check?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:</http:></http:></http:>

Yes, if the device type is set to FILE_DEVICE_SERIAL_PORT =0x0000001b with the INF file then GetFileType returns 2.

Here is what how the INF file should be modified (VirtualSerial_Install.NT.hw is empty in the original INF file):

[VirtualSerial_Install.NT.hw]
AddReg=VirtualSerial_Install_AddReg

[VirtualSerial_Install_AddReg]
HKR,DeviceType,0x10001,0x0000001b

CreateFile onlly recognizes COMn as device name for COM0 to COM9. To opem COM13, you need to specify its name as \.\COM13 (“\\.\COM13”)

GetFileType is a Win32 API
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364960(v=vs.85).aspx

But I would not expect you to need to call it when you pass in a literal string like this to CreateFile. This API is designed to be used by applications that get paths / handles from external sources and need to alter their behaviour depending on where there output is being piped to POSIX style. It has been a long time since I last looked at this API, but IIRC on modern Windows (XP+ maybe) it always returns FILE_TYPE_DISK for any valid handle

Also, I assume you know that you should be using the constants FILE_TYPE_CHAR & FILE_TYPE_UNKNOWN and not the numeric literals 2 & 0 and that in the case where it returns FILE_TYPE_UNKNOWN you need to differentiate between truly unknown and an API failure by calling GetLastError

Sent from Mailhttps: for Windows 10

From: Tim Robertsmailto:xxxxx
Sent: March 31, 2017 1:36 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: Re: [ntdev] WDK Virtual Serial Example Not Working

xxxxx@metasystem.it wrote:
> In my VB.Net console application I added this code:
>
> Dim hFile As SafeFileHandle = CreateFile(“\.\COM13”, dwAccess, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero)
>
> “CreateFile” return a valid handle (“hFile.IsInvalid” is false).
>
> Then I call:
>
> Dim fileType As Integer = GetFileType(hFile)
>
> If (fileType <> 2) AndAlso (fileType <> 0) Then
> Console.WriteLine(“Invalid Serial Port”)
> End
> End If
>
> I got result “fileType = 1”, while I expect “fileType = 2” OR “fileType = 0”.
>
> In driver source code, where I have to set this behavior?

GetFileType is not a system API. Is that part of your code? What does
it do? Wouldn’t it be easier just to eliminate the check?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:</http:></http:></http:></mailto:xxxxx></mailto:xxxxx></https:>

As suggest by D. T. (thank you) I added these two lines to “virtualserial.inx” file:

[VirtualSerial_Install.NT.hw] AddReg=VirtualSerial_Install_AddReg
[VirtualSerial_Install_AddReg] HKR,DeviceType,0x10001,0x0000001b

Now driver work perfectly with VB.Net application (“_serialPort.Open()” is used).

Still I have problems with Java and some others terminal software (I test with “Tera Term” - https://ttssh2.osdn.jp/index.html.en).
Java program is able to open virtual COM port and send data to driver (in driver I receive correctly all data bytes) but I got problem when driver try to send data to Java application.

I debug problem and this is in “CMyQueue::OnWrite” routine.

“hr = m_FxReadQueue->RetrieveNextRequest(&pSavedRequest)” fail with result “hr = 0x80070103”.

Any suggestion?

Thank you