public int[] getPacket(byte[] someFileArray) { boolean gotAPacket = false; // whether the first byte was 0x7E int packetLength = -1; // length of the dataArray int[] thisdataArray = null; // the dataArray to return int checksum = -1; // the checksum as received int localChecksum = -1; // the chacksum as we calculate it if (DEBUG) System.out.println("fileIndex: " + fileIndex); if (DEBUG) System.out.println("file length: " + fileArray.length); // make sure you have a 0x7E and at least three bytes if (getNextByte() == 0x7E && fileArray.length > 2) { gotAPacket = true; } // if the header byte is good, try the rest: if (gotAPacket) { // read two bytes if (bytesAvailable() >= 2) { int lengthMSB = getNextByte(); // high byte for length of packet int lengthLSB = getNextByte(); // low byte for length of packet // convert to length value packetLength = (lengthLSB + (lengthMSB << 8)); if (DEBUG) System.out.println("length: " + packetLength); } // read bytes until you've reached the length if (bytesAvailable() >= packetLength && packetLength > 0) { // make an array to hold the data frame: thisdataArray = new int[packetLength]; // read all the bytes except the last one into the dataArray array: for (int thisByte = 0; thisByte < packetLength; thisByte++) { thisdataArray[thisByte] = getNextByte(); if (DEBUG) System.out.print(parent.hex(thisdataArray[thisByte], 2) + " "); } if (bytesAvailable() >= 1) { // get the checksum: checksum = getNextByte(); } // calculate the checksum of the received dataArray: localChecksum = checkSum(thisdataArray); // if they're not the same, we have bad data: if (localChecksum != checksum) { if (DEBUG) System.out.println("bad checksum. Local: " + parent.hex(localChecksum % 256)); if (DEBUG) System.out.println(" remote: " + parent.hex(checksum)); // if the checksums don't add up, clear the dataArray array: thisdataArray = null; } } } // makes a nice printing for debugging: if (DEBUG) System.out.println(); // return the data frame. If it's null, you got a bad packet. return thisdataArray; }
public void sendPacket(int[] thisFrame) { // calculate the length of the total array // header byte + length (2 bytes) + command + checksum: int packetLength = thisFrame.length + 4; // set up the array you'll actually send: int[] packet = new int[packetLength]; packet[0] = 0x7E; // get the high byte and the low byte of the length of the frame: packet[1] = thisFrame.length / 256; packet[2] = thisFrame.length % 256; // add the frame to the packet: for (int b = 0; b < thisFrame.length; b++) { packet[b + 3] = thisFrame[b]; } // checksum the command: packet[packetLength - 1] = checkSum(thisFrame); if (DEBUG) System.out.print("< "); // send it out the serial port: for (int c = 0; c < packet.length; c++) { port.write(packet[c]); if (DEBUG) System.out.print(parent.hex(packet[c], 2) + " "); } if (DEBUG) System.out.println(); }
public int[] getPacket() { boolean gotAPacket = false; // whether the first byte was 0x7E int packetLength = -1; // length of the dataArray int[] thisdataArray = null; // the dataArray to return int checksum = -1; // the checksum as received int localChecksum = -1; // the chacksum as we calculate it // read bytes until you get a 0x7E port.clear(); // flush the buffer so that no old data comes in while (port.available() < 1) {; // do nothing while we wait for the buffer to fill } // this is a good header. Get ready to read a packet if (port.read() == 0x7E) { gotAPacket = true; } // if the header byte is good, try the rest: if (gotAPacket) { // read two bytes while (port.available() < 2) {; // wait until you get two bytes of length } int lengthMSB = port.read(); // high byte for length of packet int lengthLSB = port.read(); // low byte for length of packet // convert to length value packetLength = (lengthLSB + (lengthMSB << 8)); // sanity check: if the packet is too long, bail. if (packetLength > 200) { System.out.println("Length (" + packetLength + ") too long, discarding packet."); return null; } if (DEBUG) System.out.print("> [" + packetLength + "]: "); // read bytes until you've reached the length while (port.available() < packetLength) {; // do nothing while we wait for the buffer to fill } // make an array to hold the data frame: thisdataArray = new int[packetLength]; // read all the bytes except the last one into the dataArray array: for (int thisByte = 0; thisByte < packetLength; thisByte++) { thisdataArray[thisByte] = port.read(); if (DEBUG) System.out.print(parent.hex(thisdataArray[thisByte], 2) + " "); } while (port.available() < 1) {; // do nothing while we wait for the buffer to fill } // get the checksum: checksum = port.read(); // calculate the checksum of the received dataArray: localChecksum = checkSum(thisdataArray); // if they're not the same, we have bad data: if (localChecksum != checksum) { if (DEBUG) System.out.println(); System.out.print( "Bad checksum, discarding packet. Local: " + parent.hex(localChecksum % 256)); System.out.println(", Remote: " + parent.hex(checksum) + ", Length: " + packetLength); // if the checksums don't add up, clear the dataArray array: thisdataArray = null; } } // makes a nice printing for debugging: if (DEBUG) System.out.println("!"); // return the data frame. If it's null, you got a bad packet. return thisdataArray; }