If you do much work with Windows Mobile applications, there's a good chance that you frequently use the Cellular Emulator to test out things like call handling, SMS message handling, as well as cellular network behavior. I'm a pretty big fan of the Cellular Emulator … I remember what it was not that long ago when we didn't have it … that said …
There is one thing that I do sometimes find bothersome (yes I know this is a very small thing) – it's the process of associating the Cellular Emulator with a Device Emulator. You have to:
- Open the Device Emulator Configuration dialog
- Select the Peripherals tab
- Enter the Cellular Emulator COMx port number into the Serial Port 0 field
- Dismiss the Configuration Dialog
- Reset the Device Emulator by selecting File | Reset | Soft
- Click OK to confirm the soft reset
I realize that there are bigger problems in the world but when you test your apps across several different Device Emulators multiple times each day, that little 6-step process gets old. Fortunately, thanks to the Device Emulator Manager API one can create a little program to automate the whole process.
In an attempt to be a better person and to help my fellow man J … I thought I should share this little, but very helpful block of code.
public static void AssociateDeviceAndCellularEmulator(string emulatorName, string commPort)
{
IDeviceEmulatorManager emulatorManager = new DeviceEmulatorManagerClass();
IDeviceEmulatorManagerVMID theEmulator = FindEmulator(emulatorManager, emulatorName);
// Start the device emulator if not already running
if (theEmulator.get_State() == EMULATOR_STATE.EMU_NOT_RUNNING)
theEmulator.Connect();
SetEmulatorCommPort(theEmulator, commPort);
// Soft-Reset the Device Emulator
theEmulator.Reset(1);
}
private static void SetEmulatorCommPort(IDeviceEmulatorManagerVMID theEmulator, string commPort)
{
string originalConfigString = theEmulator.GetConfiguration();
XmlDocument configXml = new XmlDocument();
configXml.LoadXml(originalConfigString);
XmlNamespaceManager nsManager = new XmlNamespaceManager(configXml.NameTable);
nsManager.AddNamespace("decfg", "http://schemas.microsoft.com/DeviceEmulator/2006/01/DeCfg");
XmlElement serialPort0 = (XmlElement) configXml.SelectSingleNode(
"/decfg:DeviceEmulator/decfg:Peripherals/decfg:SerialPort[@UARTNumber='0']",
nsManager);
serialPort0.InnerText = commPort;
string updatedConfigString = configXml.OuterXml;
theEmulator.SetConfiguration(updatedConfigString);
}
The first function takes care of starting the Device Emulator if it isn't running and doing the soft-reset of the Device Emulator. Before doing the reset, it first calls the 2nd function, SetEmulatorCommPort, which retrieves the specified Device Emulator's current configuration XML, locates the serial port 0 element, assigns the correct COMx port number, then passes the updated configuration XML back to the Device Emulator. After which, the Device Emulator is correctly associated with the Cellular Emulator and is ready to use.
With these functions in place, to associate the Windows Mobile 6 Professional Emulator with the Cellular Emulator (we'll assume it requires COM4) – we just make the following function call.
AssociateDeviceAndCellularEmulator("Windows Mobile 6 Professional Emulator", "COM4");
Just a little bit of code but I find it makes my day-to-day application testing a little easier.
Posted
Mar 18 2008, 01:19 PM
by
jim-wilson