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();
        }