public void SpawnDownload(string fileName, string fullFileName) { GameObject spawned = Instantiate(Resources.Load("Item") as GameObject); spawned.transform.parent = downloadSpawnPoint.transform; spawned.transform.localScale = Vector3.one; RectTransform rectTransform = spawned.GetComponent <RectTransform>(); Vector3 position = rectTransform.localPosition; position.z = 0; rectTransform.localPosition = position; Text downloadName = spawned.GetComponentInChildren <Text>(); downloadName.text = fileName; if (Application.isEditor) { fileAddresses.Add(Application.dataPath + "/" + fileName); } else if (Application.platform == RuntimePlatform.Android) { fileAddresses.Add(Application.persistentDataPath + "/" + fileName); } Button downloadButton = spawned.GetComponentInChildren <Button>(); ButtonDownload download = spawned.GetComponentInChildren <ButtonDownload>(); download.downloadAssist = this; download.buttonObject = spawned.GetComponentInChildren <Button>(); downloadButton.onClick.AddListener(delegate { download.DownloadFile(downloadButton.gameObject, fullFileName); }); }
public bool YmodemUploadFile() { /* control signals */ const byte STX = 2; // Start of TeXt const byte EOT = 4; // End Of Transmission const byte ACK = 6; // Positive ACknowledgement const byte C = 67; // capital letter C /* sizes */ const int dataSize = 1024; const int crcSize = 2; /* THE PACKET: 1029 bytes */ /* header: 3 bytes */ // STX int invertedPacketNumber = 255; /* data: 1024 bytes */ byte[] data = new byte[dataSize]; /* footer: 2 bytes */ byte[] CRC = new byte[crcSize]; /* get the file */ FileStream fileStream = new FileStream(@path, FileMode.Open, FileAccess.Read); DateTime dt = DateTime.Now; byte[] ack; ack = new byte[] { 0x31 }; try { serialPort.Write(ack, 0, 1); } catch { MessageBox.Show("Exception"); } Thread.Sleep(300); try { sendYmodemInitialPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, path, fileStream, CRC, crcSize); if (serialPort.ReadByte() != ACK) { MessageBox.Show("Can't send the initial packet."); return(false); } if (serialPort.ReadByte() != C) { return(false); } /* send packets with a cycle until we send the last byte */ int fileReadCount; do { /* if this is the last packet fill the remaining bytes with 0 */ fileReadCount = fileStream.Read(data, 0, dataSize); if (fileReadCount == 0) { break; } if (fileReadCount != dataSize) { for (int i = fileReadCount; i < dataSize; i++) { data[i] = 0; } } /* calculate packetNumber */ packetNumber++; if (packetNumber > 255) { packetNumber -= 256; } ThreadFunction(); Console.WriteLine(packetNumber); /* calculate invertedPacketNumber */ invertedPacketNumber = 255 - packetNumber; /* calculate CRC */ Crc16Ccitt crc16Ccitt = new Crc16Ccitt(InitialCrcValue.Zeros); CRC = crc16Ccitt.ComputeChecksumBytes(data); /* send the packet */ sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize); /* wait for ACK */ if (serialPort.ReadByte() != ACK) { Console.WriteLine("Couldn't send a packet."); return(false); } } while (dataSize == fileReadCount); /* send EOT (tell the downloader we are finished) */ serialPort.Write(new byte[] { EOT }, 0, 1); /* send closing packet */ packetNumber = 0; invertedPacketNumber = 255; data = new byte[dataSize]; CRC = new byte[crcSize]; sendYmodemClosingPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize); /* get ACK (downloader acknowledge the EOT) */ if (serialPort.ReadByte() != ACK) { Console.WriteLine("Can't complete the transfer."); return(false); } } catch (TimeoutException) { throw new Exception("Eductor does not answering"); } finally { fileStream.Close(); } Console.WriteLine("File transfer is succesful"); TimeSpan span = DateTime.Now - dt; ButtonDownload.Invoke( new MethodInvoker ( delegate { ButtonDownload.Text = "Download Completed"; } ) ); MessageBox.Show("Download time: " + span.ToString(@"hh\:mm\:ss\.ff"), "Download successful", MessageBoxButtons.OK, MessageBoxIcon.None); ButtonDownload.Invoke ( new MethodInvoker ( delegate { ButtonDownload.Text = "Download"; } ) ); RunNewApplication(); return(true); }