I work on a lot of different Windows CE projects.  Some of them have a display, while others do not.  Sometimes I just have a board laying on my desk with power and a debug serial connection.  So I thought it would be handy to have a simple little app that I could use for testing my drivers. 

Given my constraints, the debug port would be useful for presenting the user interface.  Sure I could use a serial driver for this, but would mean using two serial ports, which I don't always have available.  Of course I needed to get input from the debug serial port.

I cannot tell a lie here, my first pass was simple, not to mention bad style.  I simply set a pointer to the input FIFO and read bytes if they were available.  Sure it worked, but it wasn't very portable was it?  This solution would fail if I moved to a different processor (the CPUs that I use have UARTS built in).  It also would fail with Windows CE 6.0.

Well I did some digging and found that Microsoft does support reading from the serial port.  Makes sense, the OEMs have to support it by implementing OEMReadDebugByte().  So I did some searching and found a couple of functions using OEMReadDebugByte() in the private code. 

The function that I could use in my app is InputDebugCharW().  It isn't documented, but it is just a wrapper for OEMReadDebugByte() which is documented.  The documentation is simple, InputDebugCharW() returns an int which is the byte read, OEM_DEBUG_READ_NODATA if no bytes available, or OEM_DEBUG_COM_ERROR if an error is returned.  Looking at code I noticed that OEM_DEBUG_COM_ERROR could indicate that the function isn't implemented but I am going to ignore that case becuase I make sure that it is implemented.

InputDebugCharW() would be documented like this, if it was documented:

This function retrieves a byte from the debug monitor port.

int OEMReadDebugByte (void);
Return Values
Returns the data byte if available, OEM_DEBUG_COM_ERROR if an error is
detected, or OEM_DEBUG_READ_NODATA if no data is available at the port.
OS Versions: Windows CE 2.12 and later.
Header: pkfuncs.h.
Link Library: Coredll.lib.
So I am off and running.  I need a function to wrap InputDebugCharW() and handle the possible return values.  In my case, I just want to wait until a character shows up.  Errors will be ignored. 

Here is what I came up with:

 int GetInput(void)
  int c;
   Sleep( 100 );  // Don't hammer the CPU
   c = InputDebugCharW();
  }while( c == OEM_DEBUG_READ_NODATA );
  return c;

By trial and error, I discovered that I also needed a way to clear out the input FIFO in the UART.  To do that I have a function that is the opposite of GetInput():

 int ClearInput()
  int c;
   Sleep( 10 );
   c = InputDebugCharW();
  }while( c != OEM_DEBUG_READ_NODATA );
  return c;

Now I can read from the debug serial port as well as write to it.  I didn't mention it, but I use RETAILMSG to write to the port.

In the next few days I will put these functions together to create a simple little menu system to create a user interface.

Copyright © 2008 – Bruce Eitman
All Rights Reserved