private void doCommand() { statusLabel.Text = "Starting"; progressBar.Value = 1; string fileName = string.Format("uavPictureAt{0:yyyy-MM-dd_hh-mm-ss-tt}.jpg", DateTime.Now); FileStream fileStream; fileStream = new FileStream(filePathTextBox.Text + "\\" + fileName, FileMode.Create); BinaryWriter opFile = new BinaryWriter(fileStream); uavConn.SendTextToUAV("da 20 payload[0].mem_bytes[0]"); stopCommand = false; byte[] zeroToken = { 0 }; // send 0 to receive data uavConn.SendCommand(zeroToken, false); uint imageID = 0; while (stopCommand == false) { Application.DoEvents(); Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packetSize > 0) { if (packet[0] == 1 && packetSize == 3) { // got PICTURE_TAKEN byte[] picture_taken_ack = { 8, 1 }; // ack the picture_taken command uavConn.SendCommand(picture_taken_ack, true); statusLabel.Text = "Found 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, false); imageListen(fileName, fileStream, opFile); }
private void doCommand(bool capture) { statusLabel.Text = "Starting"; progressBar.Value = 1; string fileName = string.Format("uavPictureAt{0:yyyy-MM-dd_hh-mm-ss-tt}.jpg", DateTime.Now); string filePath = filePathTextBox.Text + "\\" + fileName; stopCommand = false; if (capture) { byte[] zeroToken = { 0 }; // send 0 to take picture uavConn.SendCommand(zeroToken, false); } uint imageID = 0; while (stopCommand == false) { Application.DoEvents(); Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packetSize > 0) { if (packet[0] == 1 && packetSize == 3) { // got PICTURE_TAKEN //byte[] picture_taken_ack = { 8, 1 }; // ack the picture_taken command //uavConn.SendCommand(picture_taken_ack, true); statusLabel.Text = "Found 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, false); imageListen(fileName, filePath); }
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 }
private void doCommand() { statusLabel.Text = "Starting"; progressBar.Value = 1; string fileName = string.Format("uavPictureAt{0:yyyy-MM-dd_hh-mm-ss-tt}.jpg", DateTime.Now); FileStream fileStream; fileStream = new FileStream(filePathTextBox.Text + "\\" + fileName, FileMode.Create); BinaryWriter opFile = new BinaryWriter(fileStream); uavConn.SendTextToUAV("da 20 payload[0].mem_bytes[0]"); stopCommand = false; byte[] zeroToken = { 0 }; // send 0 to receive data uavConn.SendCommand(zeroToken); uint imageID = 0; while (stopCommand == false) { Application.DoEvents(); Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; if (packetSize > 0) { if (packet[0] == 1 && packetSize == 3) { // got PICTURE_TAKEN statusLabel.Text = "Found 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 (stopCommand == false) { Application.DoEvents(); // add cancel code check Console.Write("."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; //progressbar maximum if (packet[0] == 3) // is a IMAGE_DOWNLOAD_INFO packet { statusLabel.Text = "Found IMAGE_DOWNLOAD_INFO"; Console.WriteLine("Found IMAGE_DOWNLOAD_INFO"); totalPackets = (uint)packet[1] + (uint)(packet[2] << 8); progressBar.Maximum = (int)totalPackets; } else if (packet[0] == 4) // is a IMAGE_DATA packet { statusLabel.Text = "Found IMAGE_DATA"; 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; } statusLabel.Text = "Writing"; Console.WriteLine("Writing."); for (int i = 3; i < packetSize; i++) { opFile.Write(packet[i]); numBytes++; } progressBar.PerformStep(); if (packetNum == totalPackets - 1) { break; } lastPacketNum = packetNum; } } if (stopCommand == false) { fileStream.Close(); opFile.Close(); progressBar.Value = 1; statusLabel.Text = "Done!"; Console.WriteLine("Done!"); updateDirectory(fileName); Array.Resize(ref jpegList, jpegList.Length + 1); jpegList[jpegList.Length - 1] = fileDirectory + "\\" + fileName; jpegList = Directory.GetFiles(fileDirectory, "*.jpg"); Image img = Image.FromFile(fileDirectory + "\\" + fileName); pictureBox.Image = img; statusLabel.Text = null; } if (stopCommand == true) { fileStream.Close(); opFile.Close(); File.Delete(filePathTextBox.Text + "\\" + fileName); statusLabel.Text = "Stop"; } // 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); }
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) { UAVConnector uavConn = new UAVConnector(); uavConn.Connect("localhost", 8802, 0); while (true) { byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; uint packetNum = (uint)packet[0] + (uint)(packet[1] << 8); Console.Write("#" + packetNum + " "); for (int i = 2; i < packetSize; i++) { Console.Write(packet[i] + " "); } Console.WriteLine(); } FileStream fileStream = new FileStream("output.jpg", FileMode.Create); BinaryWriter opFile = new BinaryWriter(fileStream); uint lastPacketNum = 500; bool startImage = false; long numBytes = 0; while (true) { Console.WriteLine("Getting data..."); byte[] packet = uavConn.GetDataBytes(); int packetSize = packet.Length; uint packetNum = (uint)packet[0] + (uint)(packet[1] << 8); Console.Write("#" + packetNum + " "); for(int i = 2; 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 = 2; i < packetSize; i++) { opFile.Write(packet[i]); numBytes++; } if (packetNum == 1157) break; lastPacketNum = packetNum; } Console.WriteLine("Done!"); opFile.Close(); Console.ReadLine(); }