Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO.Ports;
- using System.Threading;
- using System.Collections;
- using Microsoft.SPOT;
- namespace CameraTest
- {
- public class LinkSprite
- {
- private SerialPort serialPort;
- public enum Baud { B57600 = 57600, B115200 = 115200 };
- public enum ImageSize { VGA, QVGA }
- public LinkSprite(string name)
- {
- serialPort = new SerialPort(name, 38400);
- serialPort.Open();
- //byte[] buff = new byte[1000];
- //int i = 0;
- //while (serialPort.BytesToRead != 0)
- //{
- // buff[i++] = (byte)serialPort.ReadByte();
- //}
- //string output = new string(Bytes2Chars(buff));
- //Thread.Sleep(2000);
- Reset();
- }
- public bool ChangeBaudRate(Baud baudRate)
- {
- byte b1= 0x2A,b2= 0xF2;
- switch (baudRate)
- {
- case Baud.B57600:
- b1 = 0x1C;
- b2 = 0x4C;
- break;
- case Baud.B115200:
- b1 = 0x0D;
- b2 = 0xA6;
- break;
- }
- byte[] outbuff = new byte[] { 0x56, 0x00, 0x24, 0x03, 0x01, b1, b2 };
- serialPort.Write(outbuff, 0, 7);
- byte[] inbuff = WaitForResponse(5, 50);
- if ((inbuff[0] == 0x76) && (inbuff[2] == 0x24))
- {
- serialPort.Close();
- serialPort.BaudRate = 115200;
- serialPort.Open();
- return true;
- }
- return false;
- }
- public bool TakePic()
- {
- serialPort.DiscardInBuffer();
- byte[] outbuff = new byte[] { 0x56, 0x00, 0x36, 0x01, 0x00 };
- serialPort.Write(outbuff, 0, 5);
- byte[] inbuff = WaitForResponse(5, 50);
- return true;
- }
- public bool StopPic()
- {
- Thread.Sleep(50);
- serialPort.DiscardInBuffer();
- byte[] outbuff = new byte[] { 0x56, 0x00, 0x36, 0x01, 0x03 };
- serialPort.Write(outbuff, 0, 5);
- byte[] inbuff = WaitForResponse(5, 50);
- return true;
- }
- public bool SetCompression(byte compression)
- {
- byte[] outbuff = new byte[] { 0x56, 0x00, 0x31, 0x05, 0x01, 0x01, 0x12, 0x04, compression };
- serialPort.Write(outbuff, 0, 9);
- byte[] inbuff = WaitForResponse(5, 50);
- // We need a delay to allow the camera to reinit after compression change
- Thread.Sleep(200);
- return true;
- }
- public bool Reset()
- {
- byte[] outbuff = new byte[] { 0x56, 0x00, 0x26, 0x00 };
- serialPort.Write(outbuff, 0, 4);
- byte[] inbuff = WaitForResponse(4, 50);
- Thread.Sleep(2000);
- // Wait for initalisation preamble
- inbuff = WaitForResponse(67, 50);
- return true;
- }
- public bool SetImageSize(ImageSize size)
- {
- byte b = 0x11;
- switch (size)
- {
- case ImageSize.QVGA:
- b = 0x11;
- break;
- case ImageSize.VGA:
- b = 0x00;
- break;
- }
- byte[] outbuff = new byte[] { 0x56, 0x00, 0x31, 0x05, 0x04, 0x01, 0x00, 0x19, b };
- serialPort.Write(outbuff, 0, 9);
- byte[] inbuff = WaitForResponse(5, 50);
- if (inbuff != null && inbuff[0] == 0x76 && inbuff[2] == 0x31)
- {
- outbuff = new byte[] { 0x56, 0x00, 0x54, 0x01, b };
- serialPort.Write(outbuff, 0, 5);
- inbuff = WaitForResponse(5, 50);
- if (inbuff != null && inbuff[0] == 0x76 && inbuff[2] == 0x54)
- {
- // Reset();
- return true;
- }
- }
- return false;
- }
- public int CapturedImageDataLength()
- {
- int size = -1;
- byte[] outbuff = new byte[] { 0x56, 0x00, 0x34, 0x01, 0x00 };
- serialPort.Write(outbuff, 0, 5);
- byte[] inbuff = WaitForResponse(9, 50);
- if (inbuff[0] == 0x76 && inbuff[2] ==0x34 && inbuff[4] == 0x04)
- {
- size = (inbuff[7] << 8) | inbuff[8];
- }
- return size;
- }
- public byte[] GetImageData(int imagelength)
- {
- byte bdelay1 = 0x00;
- byte bdelay2 = 0x0A;
- byte bPos1 = 0x00;
- byte bPos2 = 0x00;
- // datalength of 496. Camera transfer maximum 512 bytes at a time. Each packet has 5 bytes at the start and 5 at the end
- // and the datalength must be a multiple of 8. So 496 is the biggest we can have
- byte bLen1 = 0x01;
- byte bLen2 = 0xF0;
- byte[] outbuff = new byte[] { 0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00, bPos1, bPos2, 0x00, 0x00, bLen1, bLen2, bdelay1, bdelay2 };
- int numPackets = imagelength / 496;
- if (imagelength > (numPackets * 496)) numPackets += 1;
- byte[] imageData = new byte[imagelength];
- for (int i = 0; i < numPackets; i++)
- {
- int pos = i*496;
- // Ask for the next packet
- outbuff[8] = (byte)(pos >> 8);
- outbuff[9] = (byte)(pos & 0xFF);
- serialPort.Write(outbuff, 0, outbuff.Length);
- int bytesToRead = 506;
- if ((imagelength - pos) < 496)
- {
- bytesToRead = (imagelength - pos) + 10;
- }
- WaitForData(bytesToRead, 100, imageData, pos);
- }
- StopPic();
- return imageData;
- }
- private byte[] WaitForResponse(int expectedNumBytes, int timeout)
- {
- int count = 0;
- while ((serialPort.BytesToRead < expectedNumBytes) && (count++ < timeout))
- {
- Thread.Sleep(1);
- }
- if (serialPort.BytesToRead >= expectedNumBytes)
- {
- byte[] inbuff = new byte[expectedNumBytes];
- serialPort.Read(inbuff, 0, inbuff.Length);
- return inbuff;
- }
- else
- {
- // We timed out, clear the buffer to get rid of any crap
- serialPort.DiscardInBuffer();
- }
- return null;
- }
- private bool WaitForData(int expectedNumBytes, int timeout, byte[] buffer, int offset)
- {
- int count = 0;
- while ((serialPort.BytesToRead < expectedNumBytes) && (count++ < (timeout / 2)))
- {
- Thread.Sleep(2);
- }
- //Microsoft.SPOT.Debug.Print("Waited " + (count * 2) + "ms For Data");
- if (serialPort.BytesToRead >= expectedNumBytes)
- {
- //Microsoft.SPOT.Debug.Print("Offset = " + offset + ", Bytes in buffer:" + serialPort.BytesToRead);
- // First read the header
- byte[] header = new byte[5];
- serialPort.Read(header, 0, 5);
- serialPort.Read(buffer, offset, expectedNumBytes - 10);
- byte[] footer = new byte[5];
- serialPort.Read(footer, 0, 5);
- //Microsoft.SPOT.Debug.Print("Bytes left in buffer: " + serialPort.BytesToRead );
- }
- else
- {
- // We timed out, clear the buffer to get rid of any crap
- serialPort.DiscardInBuffer();
- return false;
- }
- return true;
- }
- public static char[] Bytes2Chars(byte[] Input)
- {
- char[] Output = new char[Input.Length];
- for (int Counter = 0; Counter < Input.Length; ++Counter)
- Output[Counter] = (char)Input[Counter];
- return Output;
- }
- }
- }
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Hardware;
- using SecretLabs.NETMF.Hardware;
- using SecretLabs.NETMF.Hardware.NetduinoPlus;
- using System.IO.Ports;
- using System.Text;
- namespace CameraTest
- {
- public class Program
- {
- private static LinkSprite ls = null;
- public static void Main()
- {
- if (ls == null)
- {
- ls = new LinkSprite("COM1");
- bool cam = ls.SetImageSize(LinkSprite.ImageSize.QVGA);
- Debug.Print("set image size " + cam.ToString());
- // cam = ls.ChangeBaudRate(LinkSprite.Baud.B115200);
- // Debug.Print("baud rate " + cam.ToString());
- cam = ls.SetCompression(0xff);
- Debug.Print("compression " + cam.ToString());
- }
- ls.TakePic();
- int imageSize = ls.CapturedImageDataLength();
- byte[] image = ls.GetImageData(imageSize);
- ByteArrayToString(image);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement