Click here to Skip to main content
15,860,972 members
Articles / Mobile Apps / Windows Mobile

Serial driver for Pocket PC

Rate me:
Please Sign up or sign in to vote.
4.69/5 (22 votes)
26 Mar 2006CPOL2 min read 174.3K   2.5K   64   62
How to add a new serial port to a Pocket PC device and how to drive this port.

Sample Image - DemoDriver.jpg

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Patrol-IT
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generaldifferent baud rate Pin
pguido17-Jan-11 5:02
pguido17-Jan-11 5:02 
GeneralCan't load the driver in WM5 and WM6 Pin
mgr7-Aug-10 0:13
mgr7-Aug-10 0:13 
GeneralFailed to load the driver. Pin
voquochung27-Oct-08 22:09
voquochung27-Oct-08 22:09 
GeneralRegarding Copy to SIM Pin
Sreekanth Muralidharan13-Jul-08 14:57
Sreekanth Muralidharan13-Jul-08 14:57 
GeneralVirtual COM Pin
poornimaniranjan12-May-08 23:03
poornimaniranjan12-May-08 23:03 
Questiondetecting the process connecting a virtual port Pin
mk1757-Mar-08 8:34
mk1757-Mar-08 8:34 
QuestionSerial Driver Pin
kathalora5-Nov-07 19:11
kathalora5-Nov-07 19:11 
AnswerRe: Serial Driver Pin
Eugene Ochakovsky5-Nov-07 21:47
Eugene Ochakovsky5-Nov-07 21:47 
QuestionRe: Serial Driver Pin
kathalora6-Nov-07 15:28
kathalora6-Nov-07 15:28 
QuestionWindows CE or Microsoft mobile 5.0? Pin
konbanke14-Oct-07 19:43
konbanke14-Oct-07 19:43 
QuestionVirtual COM Port Pair? Pin
EricGHLU30-Sep-07 17:35
EricGHLU30-Sep-07 17:35 
AnswerRe: Virtual COM Port Pair? Pin
Eugene Ochakovsky30-Sep-07 21:40
Eugene Ochakovsky30-Sep-07 21:40 
QuestionFailed to open Virtual COM Port in Windows Mobile 5? Pin
EricGHLU30-Sep-07 17:29
EricGHLU30-Sep-07 17:29 
AnswerRe: Failed to open Virtual COM Port in Windows Mobile 5? Pin
Eugene Ochakovsky30-Sep-07 21:36
Eugene Ochakovsky30-Sep-07 21:36 
GeneralThanks for your help Pin
Xuewu Liu8-Sep-07 20:50
Xuewu Liu8-Sep-07 20:50 
GeneralFirst Steps / Buffer Overflow > 2000 Byte [modified] Pin
Atlan198026-Jul-07 20:39
Atlan198026-Jul-07 20:39 
GeneralNice one Pin
Marcel Schlebusch24-Jul-07 6:14
Marcel Schlebusch24-Jul-07 6:14 
QuestionHow can i use WM5 ? Pin
hyohaeng20-Jul-07 7:11
hyohaeng20-Jul-07 7:11 
AnswerRe: How can i use WM5 ? Pin
Eugene Ochakovsky21-Jul-07 21:42
Eugene Ochakovsky21-Jul-07 21:42 
QuestionVirtual serial port Pair Pin
kum8417-Jul-07 16:34
kum8417-Jul-07 16:34 
AnswerRe: Virtual serial port Pair Pin
Eugene Ochakovsky17-Jul-07 21:18
Eugene Ochakovsky17-Jul-07 21:18 
Question[Q] Mapping COM-port PDA to Terminal Host Pin
longvalery11-Jul-07 20:00
longvalery11-Jul-07 20:00 
QuestionAny example create serial port in Windows XP? Pin
pctimhk3-May-07 20:20
pctimhk3-May-07 20:20 
QuestionMultiple port registration Pin
kolda17-Feb-07 23:31
kolda17-Feb-07 23:31 
AnswerRe: Multiple port registration Pin
Eugene Ochakovsky18-Feb-07 1:13
Eugene Ochakovsky18-Feb-07 1:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.