Introduction
This article shows how to create Windows CE serial driver. This will add a new serial (COM) port to the device, allowing applications to use this port the same way standard hardware ports are used. The new virtual port provides the same interface as hardware ports do.
Background
The purpose of creating a virtual port is to allow applications (like Tomtom and Destinator) to receive data from a GPS antenna connected to the hardware serial port (COM1) and also to save the received GPS signals to play back when needed and simulate the actual driving.
Using the code
The driver is a DLL that exports the following functions:
COM_Init
COM_Deinit
COM_Open
COM_Close
COM_Read
COM_Write
COM_Seek
COM_IOControl
COM_PowerDown
COM_PowerUp
DEMODLL_API DWORD COM_Init(
LPCTSTR pContext, LPCVOID lpvBusContext );
DEMODLL_API BOOL COM_Deinit(
DWORD hDeviceContext );
DEMODLL_API DWORD COM_Open(
DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode );
DEMODLL_API BOOL COM_Close(
DWORD hOpenContext );
DEMODLL_API BOOL COM_IOControl(
DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn,
PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut );
DEMODLL_API void COM_PowerUp(
DWORD hDeviceContext );
DEMODLL_API void COM_PowerDown(
DWORD hDeviceContext );
DEMODLL_API DWORD COM_Read(
DWORD hOpenContext, LPVOID pBuffer, DWORD Count );
DEMODLL_API DWORD COM_Write(
DWORD hOpenContext, LPCVOID pBuffer, DWORD Count );
DEMODLL_API DWORD COM_Seek(
DWORD hOpenContext, long Amount, WORD Type );
In the current sample driver, we map the new serial port to a hardware port (managed port), so all operations on the new port will perform the same operations on the managed port. We also open a log file, and we write all performed operations to this log.
Another important part is how to install the driver on a Pocket PC device. The DLL file must be placed in the \Windows directory, and registry must be updated with the following information:
HKEY_LOCAL_MACHINE\Drivers\BuiltIn\Serial6
Dll = DemoDriver.dll
FriendlyName = Demo Driver
Index = 6
Order = 2
Prefix = COM
ManagePort = 1
In this example, we create a virtual port COM6, but this can be any number from 1 to 9. The ManagePort
entry is not required by the OS to create a serial port. This entry is used by our demo driver to define the hardware port number to map to.
The easiest way to install the driver is to create a CAB file that will copy the DemoDriver.dll file to the \Windows directory and update all the registry settings. We provide a sample .inf file to be used with CabWiz to create a CAB file.
[Version]
Signature = "$Windows NT$"
Provider = "Code Project"
CESignature = "$Windows CE$"
[CEStrings]
AppName = "DemoDriver"
InstallDir = %CE2%
[Strings]
reg_path = Drivers\Builtin\Serial6
[DefaultInstall]
CopyFiles = Dllfiles
Addreg = Regkeys
[SourceDisksNames]
1 =, "Common Files",, .
[SourceDisksFiles]
DemoDriver.dll = 1
[DestinationDirs]
Dllfiles = 0, %CE2%
[Dllfiles]
"DemoDriver.dll"
[Regkeys]
HKLM,%reg_path%,Dll,0x00000000,DemoDriver.dll
HKLM,%reg_path%,Prefix,0x00000000,COM
HKLM,%reg_path%,FriendlyName,0x00000000,Demo Driver
HKLM,%reg_path%,Index,0x00010001,6
HKLM,%reg_path%,Order,0x00010001,2
HKLM,%reg_path%,ManagePort,0x00010001,1
Having the CAB file, we copy it to the device and click to install. Soft reset is needed for the OS to start operating the driver.
To make the driver work on WM5 devices, we have to sign the DLL. In this example, we used a method provided in this article: Deploy own certificate built-in our application's CAB (thanks to the author). Our example includes a self signed certificate called MyCert.pfx, and the Cab\PreXML_MyCert.xml file that we provide to CabWiz while creating the cab file.
To sign the driver, we call:
signtool.exe sign /f MyCert.pfx DemoDriver.dll
To create a CAB file, we run:
CabWiz.exe driver.inf /prexml PreXML_MyCert.xml
History
- March 7, 2006 - The first release.
- March 27, 2006 - Added code signing for WM5 support.