Capture received bytes and create unit tests

When you implement a new command for a camera and you need to process incoming data, it’s sometimes easier to capture the received data and analyze it while not connected to the camera.

Capture received bytes

To capture received bytes add listener to PTP.Core.DebugUtil.WriteMessage event. Send command to camera and print messages to debug output when message category is DebugUtil.ReadBytesCategory. The received bytes are shown in debug output like this:
63 01 00 00 02 00 01 10 00 00 00 00 64 00 06 00 00 00 C8 00 00 00 00 41 00 00 00 14 10 15 10 16 10 01 10 02 10 03 10 06 10 04 10 01 91 05 10 02 91 07 10 08 10 03 91 09 10 04 91 0A 10 …

You can copy-paste the received bytes to text editor and remove line breaks. After that you can copy paste it to hex editor called XVI32 (http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm).
  1. Open XVI32
  2. Create new file
  3. Select output from text editor and copy it to clipboard
  4. In XVI32 select Edit > Clipboard > Paste from hex string
XVI32_paste.png

Save captured bytes to *.bin file.

Create unit test

Save *.bin to a place where your unit test can find it. For example in PTP.Tests project GetDeviceInfo.bin file is saved to \Data\CanonEOS\GetDeviceInfo.bin and it is built as embedded resource.

The MbUnit unit test framework can load bytes from embedded resource like this:
    [Test]
    public void GetDeviceInfo([BinaryData(ResourcePath = "GetDeviceInfo.bin", ResourceScope = typeof(PTP.Data.CanonEOS.Data_CanonEOS))] byte[] data)
    {
      ...
    }
Then you can put the data to stream and create new PTP device that will use it:

      using (ContainerStream cs = new ContainerStream(new MockStream(data)))
      {
        GenericPTPDevice dev = new GenericPTPDevice(cs);
        DeviceInfo info = dev.GetDeviceInfo(); //Send GetDeviceInfo -command
        ...
When the device is created, you have to call the command that you sent when the bytes were captured. The program reads the stream and thinks that bytes came from real camera.

Last edited Feb 7, 2011 at 8:06 PM by UbiElectronic, version 3

Comments

No comments yet.