Advertisement
Guest User

Untitled

a guest
Jun 10th, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.32 KB | None | 0 0
  1. using System;
  2. using System.IO.Ports;
  3. using System.Threading;
  4. using System.Collections;
  5. using Microsoft.SPOT;
  6.  
  7. namespace CameraTest
  8. {
  9.  
  10.     public class LinkSprite
  11.     {
  12.         private SerialPort serialPort;
  13.  
  14.         public enum Baud { B57600 = 57600, B115200 = 115200 };
  15.         public enum ImageSize { VGA, QVGA }
  16.  
  17.         public  LinkSprite(string name)
  18.         {
  19.             serialPort = new SerialPort(name, 38400);
  20.             serialPort.Open();
  21.  
  22.             //byte[] buff = new byte[1000];
  23.  
  24.             //int i = 0;
  25.             //while (serialPort.BytesToRead != 0)
  26.             //{
  27.             //    buff[i++] = (byte)serialPort.ReadByte();
  28.             //}
  29.             //string output = new string(Bytes2Chars(buff));
  30.             //Thread.Sleep(2000);
  31.             Reset();
  32.  
  33.         }
  34.  
  35.         public bool ChangeBaudRate(Baud baudRate)
  36.         {
  37.             byte b1= 0x2A,b2= 0xF2;
  38.             switch (baudRate)
  39.             {
  40.                 case Baud.B57600:
  41.                     b1 = 0x1C;
  42.                     b2 = 0x4C;
  43.                     break;
  44.                 case Baud.B115200:
  45.                     b1 = 0x0D;
  46.                     b2 = 0xA6;
  47.                     break;
  48.             }
  49.  
  50.             byte[] outbuff = new byte[] { 0x56, 0x00, 0x24, 0x03, 0x01, b1, b2 };
  51.             serialPort.Write(outbuff, 0, 7);
  52.  
  53.             byte[] inbuff = WaitForResponse(5, 50);
  54.  
  55.             if ((inbuff[0] == 0x76) && (inbuff[2] == 0x24))
  56.             {
  57.                 serialPort.Close();
  58.                 serialPort.BaudRate = 115200;
  59.                 serialPort.Open();
  60.                 return true;
  61.             }
  62.  
  63.             return false;
  64.         }
  65.  
  66.         public bool TakePic()
  67.         {
  68.             serialPort.DiscardInBuffer();
  69.  
  70.             byte[] outbuff = new byte[] { 0x56, 0x00, 0x36, 0x01, 0x00 };
  71.             serialPort.Write(outbuff, 0, 5);
  72.  
  73.             byte[] inbuff = WaitForResponse(5, 50);
  74.  
  75.             return true;
  76.         }
  77.  
  78.  
  79.         public bool StopPic()
  80.         {
  81.             Thread.Sleep(50);
  82.             serialPort.DiscardInBuffer();
  83.  
  84.             byte[] outbuff = new byte[] { 0x56, 0x00, 0x36, 0x01, 0x03 };
  85.             serialPort.Write(outbuff, 0, 5);
  86.  
  87.             byte[] inbuff = WaitForResponse(5, 50);
  88.  
  89.             return true;
  90.         }
  91.  
  92.         public bool SetCompression(byte compression)
  93.         {
  94.             byte[] outbuff = new byte[] { 0x56, 0x00, 0x31, 0x05, 0x01, 0x01, 0x12, 0x04, compression };
  95.             serialPort.Write(outbuff, 0, 9);
  96.  
  97.             byte[] inbuff = WaitForResponse(5, 50);
  98.  
  99.             // We need a delay to allow the camera to reinit after compression change
  100.             Thread.Sleep(200);
  101.  
  102.             return true;
  103.         }
  104.  
  105.         public bool Reset()
  106.         {
  107.             byte[] outbuff = new byte[] { 0x56, 0x00, 0x26, 0x00 };
  108.  
  109.             serialPort.Write(outbuff, 0, 4);
  110.  
  111.             byte[] inbuff = WaitForResponse(4, 50);
  112.  
  113.             Thread.Sleep(2000);
  114.             // Wait for initalisation preamble
  115.             inbuff = WaitForResponse(67, 50);
  116.  
  117.  
  118.             return true;
  119.         }
  120.  
  121.         public bool SetImageSize(ImageSize size)
  122.         {
  123.             byte b = 0x11;
  124.             switch (size)
  125.             {
  126.                 case ImageSize.QVGA:
  127.                     b = 0x11;
  128.                     break;
  129.                 case ImageSize.VGA:
  130.                     b = 0x00;
  131.                     break;
  132.             }
  133.             byte[] outbuff = new byte[] { 0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, b };
  134.             serialPort.Write(outbuff, 0, 9);
  135.  
  136.             byte[] inbuff = WaitForResponse(5, 50);
  137.  
  138.             if (inbuff != null && inbuff[0] == 0x76 && inbuff[2] == 0x31)
  139.             {
  140.  
  141.                 outbuff = new byte[] { 0x56, 0x00, 0x54, 0x01, b };
  142.                 serialPort.Write(outbuff, 0, 5);
  143.  
  144.                 inbuff = WaitForResponse(5, 50);
  145.  
  146.                 if (inbuff != null && inbuff[0] == 0x76 && inbuff[2] == 0x54)
  147.                 {
  148.                    // Reset();
  149.  
  150.                     return true;
  151.                 }
  152.             }
  153.  
  154.             return false;
  155.         }
  156.  
  157.         public int CapturedImageDataLength()
  158.         {
  159.             int size = -1;
  160.             byte[] outbuff = new byte[] { 0x56, 0x00, 0x34, 0x01, 0x00 };
  161.             serialPort.Write(outbuff, 0, 5);
  162.  
  163.             byte[] inbuff = WaitForResponse(9, 50);
  164.  
  165.             if (inbuff[0] == 0x76 && inbuff[2] ==0x34 && inbuff[4] == 0x04)
  166.             {
  167.                 size = (inbuff[7] << 8) | inbuff[8];
  168.             }
  169.  
  170.             return size;
  171.         }
  172.  
  173.         public byte[] GetImageData(int imagelength)
  174.         {
  175.             byte bdelay1 = 0x00;
  176.             byte bdelay2 = 0x0A;
  177.             byte bPos1 = 0x00;
  178.             byte bPos2 = 0x00;
  179.  
  180.             // datalength of 496. Camera transfer maximum 512 bytes at a time. Each packet has 5 bytes at the start and 5 at the end
  181.             // and the datalength must be a multiple of 8. So 496 is the biggest we can have
  182.             byte bLen1 = 0x01;
  183.             byte bLen2 = 0xF0;
  184.  
  185.             byte[] outbuff = new byte[] { 0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00, bPos1, bPos2, 0x00, 0x00, bLen1, bLen2, bdelay1, bdelay2 };
  186.  
  187.             int numPackets = imagelength / 496;
  188.             if (imagelength > (numPackets * 496)) numPackets += 1;
  189.  
  190.             byte[] imageData = new byte[imagelength];
  191.  
  192.             for (int i = 0; i < numPackets; i++)
  193.             {
  194.                 int pos = i*496;
  195.  
  196.                 // Ask for the next packet
  197.                 outbuff[8] = (byte)(pos >> 8);
  198.                 outbuff[9] = (byte)(pos & 0xFF);
  199.                 serialPort.Write(outbuff, 0, outbuff.Length);
  200.  
  201.                 int bytesToRead = 506;
  202.                 if ((imagelength - pos) < 496)
  203.                 {
  204.                     bytesToRead = (imagelength - pos) + 10;
  205.                 }
  206.                 WaitForData(bytesToRead, 100, imageData, pos);
  207.  
  208.             }
  209.  
  210.             StopPic();
  211.  
  212.             return imageData;
  213.  
  214.  
  215.         }
  216.  
  217.         private byte[] WaitForResponse(int expectedNumBytes, int timeout)
  218.         {
  219.             int count = 0;
  220.  
  221.             while ((serialPort.BytesToRead < expectedNumBytes) && (count++ < timeout))
  222.             {
  223.                 Thread.Sleep(1);
  224.             }
  225.  
  226.             if (serialPort.BytesToRead >= expectedNumBytes)
  227.             {
  228.                 byte[] inbuff = new byte[expectedNumBytes];
  229.                 serialPort.Read(inbuff, 0, inbuff.Length);
  230.  
  231.                 return inbuff;
  232.             }
  233.             else
  234.             {
  235.                 // We timed out, clear the buffer to get rid of any crap
  236.                 serialPort.DiscardInBuffer();
  237.             }
  238.             return null;
  239.         }
  240.  
  241.         private bool WaitForData(int expectedNumBytes, int timeout, byte[] buffer, int offset)
  242.         {
  243.             int count = 0;
  244.  
  245.             while ((serialPort.BytesToRead < expectedNumBytes) && (count++ < (timeout / 2)))
  246.             {
  247.                 Thread.Sleep(2);
  248.             }
  249.             //Microsoft.SPOT.Debug.Print("Waited " + (count * 2) + "ms For Data");
  250.  
  251.  
  252.             if (serialPort.BytesToRead >= expectedNumBytes)
  253.             {
  254.                 //Microsoft.SPOT.Debug.Print("Offset = " + offset + ", Bytes in buffer:" + serialPort.BytesToRead);
  255.  
  256.                 // First read the header
  257.                 byte[] header = new byte[5];
  258.                 serialPort.Read(header, 0, 5);
  259.  
  260.                 serialPort.Read(buffer, offset, expectedNumBytes - 10);
  261.                 byte[] footer = new byte[5];
  262.                 serialPort.Read(footer, 0, 5);
  263.  
  264.                 //Microsoft.SPOT.Debug.Print("Bytes left in buffer: " + serialPort.BytesToRead );
  265.  
  266.             }
  267.             else
  268.             {
  269.                 // We timed out, clear the buffer to get rid of any crap
  270.                 serialPort.DiscardInBuffer();
  271.                 return false;
  272.             }
  273.             return true;
  274.         }
  275.  
  276.  
  277.         public static char[] Bytes2Chars(byte[] Input)
  278.         {
  279.             char[] Output = new char[Input.Length];
  280.             for (int Counter = 0; Counter < Input.Length; ++Counter)
  281.                 Output[Counter] = (char)Input[Counter];
  282.             return Output;
  283.         }
  284.     }
  285. }
  286.  
  287.  
  288.  
  289. using System;
  290. using System.Net;
  291. using System.Net.Sockets;
  292. using System.Threading;
  293. using Microsoft.SPOT;
  294. using Microsoft.SPOT.Hardware;
  295. using SecretLabs.NETMF.Hardware;
  296. using SecretLabs.NETMF.Hardware.NetduinoPlus;
  297. using System.IO.Ports;
  298. using System.Text;
  299.  
  300. namespace CameraTest
  301. {
  302.     public class Program
  303.     {
  304.         private static LinkSprite ls = null;
  305.         public static void Main()
  306.         {
  307.             if (ls == null)
  308.             {
  309.                 ls = new LinkSprite("COM1");
  310.                 bool cam = ls.SetImageSize(LinkSprite.ImageSize.QVGA);
  311.                 Debug.Print("set image size " + cam.ToString());
  312.                 // cam = ls.ChangeBaudRate(LinkSprite.Baud.B115200);
  313.                 // Debug.Print("baud rate " + cam.ToString());
  314.                 cam = ls.SetCompression(0xff);
  315.                 Debug.Print("compression " + cam.ToString());
  316.             }
  317.  
  318.             ls.TakePic();
  319.             int imageSize = ls.CapturedImageDataLength();
  320.             byte[] image = ls.GetImageData(imageSize);
  321.             ByteArrayToString(image);
  322.          }
  323.     }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement