public SendTextToUAV ( string textToUAV ) : void | ||
textToUAV | string | |
return | void |
private void MyInitialize() { uavConn.Connect(0); progressBar.Minimum = 1; progressBar.Step = 1; progressBar.Value = 1; fileDirectory = System.IO.Directory.GetCurrentDirectory(); filePathTextBox.Text = System.IO.Directory.GetCurrentDirectory(); jpegList = Directory.GetFiles(fileDirectory, "*.jpg"); uavConn.SendTextToUAV("payload.broadcast_bytes 255 0"); ResolutionComboBox.SelectedIndex = 3; }
private void doCommand() { UAVConnector uavConn = new UAVConnector(); uavConn.Connect(0); FileStream fileStream = new FileStream("output.jpg", FileMode.Create); BinaryWriter opFile = new BinaryWriter(fileStream); uavConn.SendTextToUAV("da 20 payload[0].mem_bytes[0]"); byte[] zeroToken = { 0 }; // send 0 to receive data uavConn.SendCommand(zeroToken); uint imageID = 0; while (true) { Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packet[0] == 1) { // got PICTURE_TAKEN Console.WriteLine("Found PICTURE_TAKEN"); imageID = (uint)packet[1] + (uint)(packet[2] << 8); break; } } byte[] imageDownloadRequest = { 2, (byte)imageID, (byte)(imageID >> 8) }; uavConn.SendCommand(imageDownloadRequest); uint lastPacketNum = 500; uint totalPackets = 500; bool startImage = false; long numBytes = 0; while (true) { Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packet[0] == 3) // is a IMAGE_DOWNLOAD_INFO packet { Console.WriteLine("Found IMAGE_DOWNLOAD_INFO"); totalPackets = (uint)packet[1] + (uint)(packet[2] << 8); } else if (packet[0] == 4) // is a IMAGE_DATA packet { Console.WriteLine("Found IMAGE_DATA"); uint packetNum = (uint)packet[1] + (uint)(packet[2] << 8); Console.Write("#" + packetNum + " "); for (int i = 3; i < packetSize; i++) { Console.Write(packet[i] + " "); } Console.WriteLine(); if (packetNum == 0) startImage = true; if (startImage == false) continue; if (packetNum == lastPacketNum) { //Console.WriteLine("End of image."); continue; } Console.WriteLine("Writing."); for (int i = 3; i < packetSize; i++) { opFile.Write(packet[i]); numBytes++; } if (packetNum == totalPackets - 1) break; lastPacketNum = packetNum; } } opFile.Close(); Console.WriteLine("Done!"); uavConn.Close(); Image img = Image.FromFile("output.jpg"); pictureBox.Image = img; // Console.ReadLine(); /* // CameraCommand newCommand = new CameraCommand(); byte[] command = new byte[3]; byte[] uavDataCurrent = new byte[uavDataLength]; byte[] imageData = new byte[250]; byte[] allImageData = new byte[30000]; byte[] zeroToken = { 0 }; // send 0 to receive data string fileName=string.Format("uavPictureAt{0:yyyy-MM-dd_hh-mm-ss-tt}.jpg", DateTime.Now); // uavConnector.SendTextToUAV("da 20 payload[0].mem_bytes[0]"); uavConnector.StartStream(0); byte[] currentCommand = { 0 }; uavConnector.SendCommand(zeroToken); // uavConnector.SendTextToUAV("payload[0].mem_bytes[0] 0"); //while (currentCommand.Length > 0 && currentCommand[0] != PICTURE_TAKEN) while (true) { // currentCommand = uavConnector.ReceiveFromStream(); currentCommand = uavConnector.ReceiveFromStream(); } //uavConnector.SendTextToUAV("payload[0].mem_byte[0] 0"); uint imageID = (uint)currentCommand[1] + (uint)(currentCommand[2] << 8); command[0] = SEND_DOWNLOAD_REQUEST; command[1] = currentCommand[1]; command[2] = currentCommand[2]; uavConnector.SendCommand(command); while (currentCommand[0] != DOWNLOAD_INFO_COMMAND) { currentCommand = uavConnector.ReceiveFromStream(); } uint numberOfPacket = (uint)command[1] + (uint)(command[2] << 8); int allImageDataCount=0; while (currentCommand[0] != TAKEN_IMAGE_DATA) { currentCommand = uavConnector.ReceiveFromStream(); } uint prevPacketNumber = 500; while(currentCommand[0] == TAKEN_IMAGE_DATA) { uint currentPacketNum = (uint)command[1] + (uint)(command[2] << 8); if (currentPacketNum == prevPacketNumber + 1) { for (int countData = 3; countData < currentCommand.Length; countData++, allImageDataCount++) { allImageData[allImageDataCount] = imageData[countData]; } if (currentPacketNum >= numberOfPacket - 1) { break; } } else if(currentPacketNum != prevPacketNumber) { throw new Exception("ERROR: Skipped a packet."); } prevPacketNumber = currentPacketNum; currentCommand = uavConnector.ReceiveFromStream(); } Image imageFromUAV = uavConnector.GetImage(allImageData); pictureBox.Image = imageFromUAV; uavConnector.SaveImage(allImageData, fileName, fileDirectory); //uavDataPrevious = uavDataCurrent; * * */ /* while (!endOfCommand) { uavDataCurrent= streamPort.StartReceiving(); switch (uavDataCurrent[COMMAND_BYTE_LOCATION]) { case PICTURE_TAKEN: pictureTaken = true; numberOfPacket = (int)uavDataCurrent[1]*0xFF+(int)uavDataCurrent[2]; imageID = (int)uavDataCurrent[3]; break; case IMAGE_DATA: if (pictureTaken == true) { if (uavDataCurrent != uavDataPrevious && pictureTaken == true) { packetID = (int)uavDataCurrent[PACKET_ID_LOCATION]; dataLength = (int)uavDataCurrent[LENGTH_BYTE_LOCATION_1] * 0xFF + uavDataCurrent[LENGTH_BYTE_LOCATION_2]; // IMPORTANT: Make the second byte(first byte is command byte) of the data a data length byte in the PAYLOAD MODULE //This for loop check whether the data is at the end of file or not //I set 0xD9 to be the end of file code //also it will check for data length cycle. //IMPORTANT: the eachByteCheck start at 1 so make sure that the data start at second byte !! (or modify the code) for (int eachByteCheck = START_OF_IMAGE_DATA; eachByteCheck < dataLength&&!endOfCommand; eachByteCheck++, count++) { imageData[count] = uavDataCurrent[eachByteCheck]; //Check for end of file if (imageData[count] == COMMAND_HEADER || nextIsCommandByte) { if (((eachByteCheck != dataLength - 1) && imageData[count + 1] == END_OF_FILE) || nextIsCommandByte && imageData[count] == END_OF_FILE) { endOfCommand = true; } else if (eachByteCheck == dataLength - 1) { nextIsCommandByte = true; } } } } } break; default: break; } } * */ //consolePort.SendCommand(stopSendingDataCommand, STOP_SENDING_COMMAND_LENGTH); }
static void Main(string[] args) { mainForm.Show(); while (true) { Application.DoEvents(); } #if false UAVConnector uavConn = new UAVConnector(); uavConn.Connect(0); FileStream fileStream = new FileStream("output.jpg", FileMode.Create); BinaryWriter opFile = new BinaryWriter(fileStream); uavConn.SendTextToUAV("da 20 payload[0].mem_bytes[0]"); byte[] zeroToken = { 0 }; // send 0 to receive data uavConn.SendCommand(zeroToken); uint imageID = 0; while (true) { Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packet[0] == 1) { // got PICTURE_TAKEN Console.WriteLine("Found PICTURE_TAKEN"); imageID = (uint)packet[1] + (uint)(packet[2] << 8); break; } } byte[] imageDownloadRequest = { 2, (byte)imageID, (byte)(imageID >> 8) }; uavConn.SendCommand(imageDownloadRequest); uint lastPacketNum = 500; uint totalPackets = 500; bool startImage = false; long numBytes = 0; while (true) { Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packet[0] == 3) // is a IMAGE_DOWNLOAD_INFO packet { Console.WriteLine("Found IMAGE_DOWNLOAD_INFO"); totalPackets = (uint)packet[1] + (uint)(packet[2] << 8); } else if(packet[0] == 4) // is a IMAGE_DATA packet { Console.WriteLine("Found IMAGE_DATA"); uint packetNum = (uint)packet[1] + (uint)(packet[2] << 8); Console.Write("#" + packetNum + " "); for (int i = 3; i < packetSize; i++) { Console.Write(packet[i] + " "); } Console.WriteLine(); if (packetNum == 0) startImage = true; if (startImage == false) continue; if (packetNum == lastPacketNum) { //Console.WriteLine("End of image."); continue; } Console.WriteLine("Writing."); for (int i = 3; i < packetSize; i++) { opFile.Write(packet[i]); numBytes++; } if (packetNum == totalPackets - 1) break; lastPacketNum = packetNum; } } Console.WriteLine("Done!"); opFile.Close(); uavConn.Close(); Console.ReadLine(); #endif }
static void Main(string[] args) { mainForm.Show(); while (true) { Application.DoEvents(); } #if false UAVConnector uavConn = new UAVConnector(); uavConn.Connect(0); FileStream fileStream = new FileStream("output.jpg", FileMode.Create); BinaryWriter opFile = new BinaryWriter(fileStream); uavConn.SendTextToUAV("da 20 payload[0].mem_bytes[0]"); byte[] zeroToken = { 0 }; // send 0 to receive data uavConn.SendCommand(zeroToken); uint imageID = 0; while (true) { Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packet[0] == 1) { // got PICTURE_TAKEN Console.WriteLine("Found PICTURE_TAKEN"); imageID = (uint)packet[1] + (uint)(packet[2] << 8); break; } } byte[] imageDownloadRequest = { 2, (byte)imageID, (byte)(imageID >> 8) }; uavConn.SendCommand(imageDownloadRequest); uint lastPacketNum = 500; uint totalPackets = 500; bool startImage = false; long numBytes = 0; while (true) { Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packet[0] == 3) // is a IMAGE_DOWNLOAD_INFO packet { Console.WriteLine("Found IMAGE_DOWNLOAD_INFO"); totalPackets = (uint)packet[1] + (uint)(packet[2] << 8); } else if (packet[0] == 4) // is a IMAGE_DATA packet { Console.WriteLine("Found IMAGE_DATA"); uint packetNum = (uint)packet[1] + (uint)(packet[2] << 8); Console.Write("#" + packetNum + " "); for (int i = 3; i < packetSize; i++) { Console.Write(packet[i] + " "); } Console.WriteLine(); if (packetNum == 0) { startImage = true; } if (startImage == false) { continue; } if (packetNum == lastPacketNum) { //Console.WriteLine("End of image."); continue; } Console.WriteLine("Writing."); for (int i = 3; i < packetSize; i++) { opFile.Write(packet[i]); numBytes++; } if (packetNum == totalPackets - 1) { break; } lastPacketNum = packetNum; } } Console.WriteLine("Done!"); opFile.Close(); uavConn.Close(); Console.ReadLine(); #endif }