Thursday, May 03, 2007

Problem
I've got a text file on the device that's stored in [ASCII/Unicode]. Sure, I can create a System.IO.Stream derivative, read the data and close it, but I'd like something simpler. What can the SDF do for me?

Solution
The OpenNETCF.IO.FileHelper class provides some useful methods to explore.  Take a look at these snippets:

string fileContents;
fileContents = OpenNETCF.IO.FileHelper.ReadAllText("MyASCIIFile.txt", Encoding.ASCII);
fileContents = OpenNETCF.IO.FileHelper.ReadAllText("MyUnicodeFile.txt", Encoding.Unicode);

string[] linesOfTextFile;
linesOfTextFile = OpenNETCF.IO.FileHelper.ReadAllLines("MyASCIIFile.txt", Encoding.ASCII);
linesOfTextFile = OpenNETCF.IO.FileHelper.ReadAllLines("MyUnicodeFile.txt", Encoding.Unicode);

5/3/2007 1:45:49 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]  | 

Problem
I have an assembly that is in the GAC on my device.  The assembly is used by a couple different applications, but often through other intermediate assemblies.  I'd like to do some logging of the actual application using my assembly. What can the SDF do for me?

Solution
The OpenNETCF.Reflection.Assembly2 class provides the key:

OpenNETCF.Reflection.Assembly2.GetEntryAssembly();


 

5/3/2007 1:40:54 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]  | 
 Wednesday, April 25, 2007

Problem A
I have an application that uses an enum whos values aren't nicely readable.  Given a value of the enum, I'd like to get a nice, readble text name for it.  I'd like the solution to be easily extensible and maintanable.  What can the SDF do for me?

Problem B
I have an application with an enum and I need to populate a combobox with its value names. What can the SDF do for me?

Problem C
I have an application with an enum and I need to get a list of all of the values in the enum. What can the SDF do for me?

Solution
The solution to all of these is found in the OpenNETCF.Enum2 class.  Here's a snippet from a project I'm working on:
[Tested on the PPC 2003 Emulator with SDF 2.1]

public class DescriptionAttribute : Attribute
{
  public string Description;

  public DescriptionAttribute(string description)
  {
    Description = description;
  }
}

public enum Places
{
  [Description("Missoula, Montana")]
  MissoulaMT,
  [Description("Denton, Texas")]
  DentonTX,
  [Description("Berwyn, Illinois")]
  BerwynIL,
  [Description("Frederick, Maryland")]
  FrederickMD
}

...

using System.Reflection;
using System.ComponentModel;
using OpenNETCF;
...
private void button1_Click(object sender, EventArgs e)
{
  int maxPlaces = Enum2.GetValues(typeof(Places)).Length;
  Places p = (Places)(new System.Random().Next(maxPlaces));
  FieldInfo fi = p.GetType().GetField(Enum2.GetName(typeof(Places), p));
  DescriptionAttribute desc = (DescriptionAttribute)fi.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
  textBox1.Text = desc.Description;
}

4/25/2007 11:48:28 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]  | 
 Sunday, April 22, 2007

Problem A
I have an application that starts up a worker thread.  I have code that properly kills that thread during normal shutdown, but if I'm debugging and stop the app, the thread keeps the app alive and I can't debug the app again without resetting teh device.  What can the SDF do for me?

Problem B
I have a CE device that is running a process without a window (console or otherwise) and I'd like to kill it.  What can the SDF do for me?

Solution
The solution to both is found in the OpenNETCF.ToolHelp.ProcessEntry class.  Add this to an app and run it.

ProcessEntry[] currentProcesses = ProcessEntry.GetProcesses();

foreach (ProcessEntry p in currentProcesses)
{
  if (p.ExeFile.ToLower() == "MyAppName.exe".ToLower())
  {
    p.Kill();
    return;
  }
}

4/22/2007 11:23:10 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]  | 
 Friday, April 20, 2007

Problem A
I have an application that transfers data to another app via (serial/CAN/Ethernet/can-and-string/cheese stream ion a macaroni pipe/whatever).  I have set up a send and acknowlege paradigm so I know when the receiver has all of the data, but I need to know that what the other end received is correct.  What does the SDF do for me?

Problem B
I have an application that receives a file from a native application.  The application send me a 'checksum' along with the data and the native guys tell me that I must use that checksum value to make sure that the data I received has not been corrupted.  I vaguely understand what a checksum is, but I have no clue how to calculate one from the file.  What does the SDF do for me?

Solution
[Tested on a custom PXA270-based Windows CE 5.0 device *and* the desktop (full framework 2.0)]

Nicely enough, the SDF solves both of these with the use of the OpenNETCF.CRC class. The CRC (short for Cyclic Redundancy Check) class supports generating a CRC for a byte array or a FileStream.

Here's what usage for a file looks like.  This creates a 32-bit checksum (we support 8-64 bit) using a standard ploynomial (we support any custom polynomial too):

FileStream fs = File.OpenRead(sourceFileName);
uint checksum = (uint)OpenNETCF.CRC.GenerateChecksum(fs, 32, (ulong)OpenNETCF.CRCPolynomial.CRC_CCITT32);
fs.Close();

 

4/20/2007 2:40:28 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]  | 
 Wednesday, April 18, 2007

Sometimes we tend to forget just how useful the Smart Device Framework really is.  There are tons of little gems in it that we put in there, some times years ago, and then we forget about it.  I have a general idea of what all is there, but by no means can I tell you everything, so when I need a feature in an app, I always go look to see if we've already done it.

Unfortunately we tend to lack concrete examples of how a lot of it can help you in your everyday work.  In an effort to rectify that, over the coming months all of us here at OpenNETCF will be blogging short snippets of cool, useful stuff you can do.  We also are taking requests.

So here's one.  

Problem
I have a device and we want to know when a storage device (USB, CF, PCMCIA, SD, etc.) is either inserted or removed.  What does the SDF do for me?

Solution

[Developed and tested on an OLDI 56SAM-400 800MHz x86 device running CE 5.0]

private DeviceStatusMonitor m_diskMonitor;

public MyClass()
{
    ...

    m_diskMonitor = new DeviceStatusMonitor(DeviceStatusMonitor.FATFS_MOUNT_GUID, false);
    m_diskMonitor.DeviceNotification += FATMounted;
    m_diskMonitor.StartStatusMonitoring();
    ...
}

~MyClass()
{
    m_diskMonitor.StopStatusMonitoring();
}

void FATMounted(object sender, DeviceNotificationArgs e)
{
    Trace2.WriteLine(string.Format("FAT device '{0}' {1}mounted", e.DeviceName, e.DeviceAttached ? "" : "un"));
}

The DeviceStatusMonitor ctor's first parameter can be a few things - I've chosen FAT file system (we can detect a CD too!).

The thing I don't understand is how the hell this class ended up in the OpenNETCF.Net namespace.  Expect that to change in 2.2.  I'll probably deprecate the existing classes with a warning and move them somewhere that it makes sense.  I may also streamline the usage as well.

4/18/2007 10:37:04 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0]  |