/////////////////////////// //receive byte Array via XModem using either Checksum or CRC error detection /// /////////////////////// public byte[] XModemReceive(bool useCRC, XModemHandler stop) { //since we don't know how many bytes we receive it's the //best solution to use a MemoryStream System.IO.MemoryStream buf = new System.IO.MemoryStream(); int packetno = 1; int retry = 0; int i, c; double kb = 0; if (useCRC) // send and use CRC { this.writeByte(C); } else //send and don't use CRC { this.writeByte(NAK); } while (retry < 16) { try { c = port.ReadByte(); } catch { c = 0x00; } if (c == SOH || c == STX) { #region SOH/STX retry = 0; bufsz = 128; // default buffer size if (c == STX) { bufsz = 1024; // buffer size for Xmodem1K } #region fill packet with datastream xbuff[0] = (byte)c; for (i = 0; i < (bufsz + (useCRC ? 1 : 0) + 3); i++) { xbuff[i + 1] = (byte)this.port.ReadByte(); } #endregion if (stop.IsCanceled) { break; } if (xbuff[1] == (byte)(~xbuff[2]) && xbuff[1] == packetno && check(useCRC, xbuff, 3, bufsz)) { //add buffer to memory stream buf.Write(xbuff, 3, bufsz); this.writeByte(ACK); #region fire event & increment packet number //128 or 1024 Byte per package double d = bufsz; d = d / 1024; kb = (kb + d); if (this.PacketReceived != null) { PacketReceived(this, null); } packetno++; if (packetno >= 256) { packetno = 0; } #endregion } else { this.writeByte(NAK); } #endregion } else if (c == EOT) { #region EOT port.DiscardInBuffer(); this.writeByte(ACK); //convert from memory stream to byte[] return(buf.ToArray()); #endregion } else if (c == CAN) { #region CAN if (port.ReadByte() == CAN) { port.DiscardInBuffer(); this.writeByte(ACK); return(null); //canceled by remote } #endregion } else { retry++; port.DiscardInBuffer(); this.writeByte(NAK); } }//while() //timeout this.writeByte(CAN); this.writeByte(CAN); this.writeByte(CAN); port.DiscardInBuffer(); return(null); }
/////////////////////////// //receive byte Array via XModem using either Checksum or CRC error detection /// /////////////////////// public byte[] XModemReceive(bool useCRC, XModemHandler stop) { //since we don't know how many bytes we receive it's the //best solution to use a MemoryStream System.IO.MemoryStream buf = new System.IO.MemoryStream(); int packetno = 1; int retry = 0; int i, c; double kb = 0; if (useCRC) // send and use CRC this.writeByte(C); else //send and don't use CRC this.writeByte(NAK); while (retry < 16) { try { c = port.ReadByte(); } catch { c = 0x00; } if (c == SOH || c == STX) { #region SOH/STX retry = 0; bufsz = 128; // default buffer size if (c == STX) bufsz = 1024; // buffer size for Xmodem1K #region fill packet with datastream xbuff[0] = (byte)c; for (i = 0; i < (bufsz + (useCRC ? 1 : 0) + 3); i++) { xbuff[i + 1] = (byte)this.port.ReadByte(); } #endregion if (stop.IsCanceled) { break; } if (xbuff[1] == (byte)(~xbuff[2]) && xbuff[1] == packetno && check(useCRC, xbuff, 3, bufsz)) { //add buffer to memory stream buf.Write(xbuff, 3, bufsz); this.writeByte(ACK); #region fire event & increment packet number //128 or 1024 Byte per package double d = bufsz; d = d / 1024; kb = (kb + d); if (this.PacketReceived != null) PacketReceived(this, null); packetno++; if (packetno >= 256) packetno = 0; #endregion } else { this.writeByte(NAK); } #endregion } else if (c == EOT) { #region EOT port.DiscardInBuffer(); this.writeByte(ACK); //convert from memory stream to byte[] return buf.ToArray(); #endregion } else if (c == CAN) { #region CAN if (port.ReadByte() == CAN) { port.DiscardInBuffer(); this.writeByte(ACK); return null; //canceled by remote } #endregion } else { retry++; port.DiscardInBuffer(); this.writeByte(NAK); } }//while() //timeout this.writeByte(CAN); this.writeByte(CAN); this.writeByte(CAN); port.DiscardInBuffer(); return null; }