public static byte[] getData(byte[] data, ref int bytesToBeRemoved) { //Find the Start Byte var startLoc = Array.IndexOf <byte>(data, ByteDefs.START_BYTE); if (startLoc == -1) { bytesToBeRemoved = data.Length; return(null); } var endLoc = Array.IndexOf(data, ByteDefs.END_BYTE, startLoc + 1); if (endLoc == -1) { //remove the bytes leading up to the start byte if (startLoc > 0) { bytesToBeRemoved = startLoc; } else { bytesToBeRemoved = 0; } return(null); } var contents = UnescapeData(data); //Get the data without the CRC var dataBytes = new List <byte>(contents).GetRange(1, contents.Length - 4).ToArray(); var startCRCLoc = contents.Length - 3; var endCRCLoc = contents.Length - 1; var receivedCRCbytes = new List <byte>(contents).GetRange(startCRCLoc, 2).ToArray(); var receivedCRC = BitConverter.ToUInt16(receivedCRCbytes, 0); var dataCRC = Crc16.Crc16_buff(dataBytes); if (dataCRC != receivedCRC) { bytesToBeRemoved = endLoc + 1; return(null); } else { bytesToBeRemoved = endLoc + 1; return(dataBytes); } }
public static byte[] FrameData(byte[] data) { var byteList = new List <byte>(); byteList.Add(ByteDefs.START_BYTE); var crc = Crc16.Crc16_buff(data); var crcBytes = BitConverter.GetBytes(crc); var escapeData = EscapeData(data); //add the data to the list. Could use linq but didnt want the dependency foreach (byte element in escapeData) { byteList.Add(element); } byteList.Add(crcBytes[0]); byteList.Add(crcBytes[1]); byteList.Add(ByteDefs.END_BYTE); return(byteList.ToArray()); }