private void sendEndOftransmision() { sp.Write(new byte[] { (byte)0x04 }, 0, 1); Packet endpacket = new Packet(); endpacket.isend = true; endpacket.longpacket = false; endpacket.packetnum = 0; endpacket.data = new byte[128]; endpacket.createPacket(); sp.Write(endpacket.packet, 0, endpacket.packet.Length); return; }
public void sendBinaryFile(byte[] bytes, string filename) { readbinary = true; sp.Write("AT+pBINARYUPLOAD"); sendNewLine(); waitfor('C'); ushort packetnum = 0; Packet initpacket = new Packet(); initpacket.isinit = true; initpacket.packetnum = packetnum; initpacket.filename = filename; initpacket.filelength = bytes.Length; if (filename.Length > 125) { initpacket.longpacket = true; } else { initpacket.longpacket = false; } initpacket.createPacket(); sp.Write(initpacket.packet, 0, initpacket.packet.Length); waitforack(); waitfor('C'); MemoryStream ms = new MemoryStream(bytes); byte[] temparr = new byte[1024]; Packet sendPacket; long numpack = Math.Abs(((long)ms.Length) / ((long)1024)); //long leftover = ms.Length % 1024; BinaryReader br = new BinaryReader(ms); while (ms.Position != ms.Length) { progressUpdate(packetnum, Convert.ToInt32(numpack)); _waitforack = false; packetnum++; temparr = br.ReadBytes(1024); sendPacket = new Packet(); sendPacket.packetnum = packetnum; sendPacket.longpacket = true; sendPacket.isinit = false; sendPacket.data = temparr; sendPacket.createPacket(); sp.Write(sendPacket.packet, 0, sendPacket.packet.Length); waitforack(); } sendEndOftransmision(); waitforack(); readbinary = false; waitforline(); }
public void sendBinaryFile(byte[] bytes,string filename) { readbinary = true; sp.Write("AT+pBINARYUPLOAD"); sendNewLine(); waitfor('C'); ushort packetnum = 0; Packet initpacket = new Packet(); initpacket.isinit = true; initpacket.packetnum = packetnum; initpacket.filename = filename; initpacket.filelength = bytes.Length; if (filename.Length > 125) initpacket.longpacket = true; else initpacket.longpacket = false; initpacket.createPacket(); sp.Write(initpacket.packet, 0, initpacket.packet.Length); waitforack(); waitfor('C'); MemoryStream ms = new MemoryStream(bytes); byte[] temparr = new byte[1024]; Packet sendPacket; long numpack = Math.Abs(((long)ms.Length) / ((long)1024)); //long leftover = ms.Length % 1024; BinaryReader br = new BinaryReader(ms); while (ms.Position != ms.Length) { progressUpdate(packetnum, Convert.ToInt32(numpack)); _waitforack = false; packetnum++; temparr = br.ReadBytes(1024); sendPacket = new Packet(); sendPacket.packetnum = packetnum; sendPacket.longpacket = true; sendPacket.isinit = false; sendPacket.data = temparr; sendPacket.createPacket(); sp.Write(sendPacket.packet, 0, sendPacket.packet.Length); waitforack(); } sendEndOftransmision(); waitforack(); readbinary = false; waitforline(); }
public void sendBinaryFile(byte[] bytes, string filename) { readbinary = true; ErrorIdentified = false; FlashResult = false; //SendToSMAChars("AT+pBINARYUPLOAD"); //sp.Write("AT+pBINARYUPLOAD"); //sendNewLine(); waitfor('C'); ushort packetnum = 0; Packet initpacket = new Packet(); initpacket.isinit = true; initpacket.packetnum = packetnum; initpacket.filename = filename; initpacket.filelength = bytes.Length; if (filename.Length > 125) { initpacket.longpacket = true; } else { initpacket.longpacket = false; } initpacket.createPacket(); Thread.Sleep(250); sp.Write(initpacket.packet, 0, initpacket.packet.Length); Thread.Sleep(350); waitforack(); _waitforack = false; waitfor('C'); MemoryStream ms = new MemoryStream(bytes); byte[] temparr = new byte[1024]; Packet sendPacket; long numpack = Math.Abs(((long)ms.Length) / ((long)1024)); //long leftover = ms.Length % 1024; BinaryReader br = new BinaryReader(ms); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); TimeSpan ts = stopWatch.Elapsed; while ((ms.Position != ms.Length) && ts.TotalMilliseconds <= 60000) { ts = stopWatch.Elapsed; //progressUpdate(packetnum, Convert.ToInt32(numpack)); _waitforack = false; packetnum++; temparr = br.ReadBytes(1024); sendPacket = new Packet(); sendPacket.packetnum = packetnum; sendPacket.longpacket = true; sendPacket.isinit = false; sendPacket.data = temparr; sendPacket.createPacket(); sp.Write(sendPacket.packet, 0, sendPacket.packet.Length); waitforack(); //Source.textBox1.Invoke(Source.textBoxUpdateDelegate, new Object[] { Source.textBox1, "\r\n" + "Packet ID " + packetnum.ToString() + " Out of: " + NumOfPackets.ToString(), true }); _waitforack = false; Thread.Sleep(20); } if (ts.TotalMilliseconds >= 60000) { return; } //sendEndOftransmision(); while (!EndConditionIdentified && !ErrorIdentified) { if (Encoding.Default.GetString(inputdata).Contains(EndCondition)) { EndConditionIdentified = true; FlashResult = true; } if (Encoding.Default.GetString(inputdata).Contains("No responce from SenseAir.")) { ErrorIdentified = true; FlashResult = false; } Thread.Sleep(2000); sendEndOftransmision(); /*while ((sp.BytesToRead < 100) && (!FlashResult) ) * { * Thread.Sleep(2000); * if ((!Encoding.Default.GetString(inputdata).Contains("Transmitting file to SenseAir"))) * { * sendEndOftransmision(); * } * }*/ while ((sp.BytesToRead > 0)) { int NumOfChars = sp.BytesToRead; sp.Read(inputdata, bytesreceived, NumOfChars); bytesreceived += NumOfChars; //Source.textBox1.Invoke(Source.textBoxUpdateDelegate, new Object[] { Source.textBox1, Encoding.Default.GetString(inputdata, bytesreceived - NumOfChars, NumOfChars), true }); } } //Thread.Sleep(15000); //waitforack(); _waitforack = false; readbinary = false; //waitforline(); sp.DataReceived -= new SerialDataReceivedEventHandler(sp_DataReceived); //Source.textBox1.Invoke(Source.textBoxUpdateDelegate, new Object[] { Source.textBox1, "\r\n" + "Upload File Ended.", true }); }