//DoneWithCrutch public static BodyFormat ReceiveFullBody(SerialPort serialPort, int length, int timeout = 1000) { byte[] rawData = new byte[HeaderFormat.getHeaderFormatSize() + length + 4]; serialPort.WriteTimeout = timeout; Utils.ReadData(serialPort, rawData, HeaderFormat.getHeaderFormatSize() + length + 4); Debug.WriteLine($"Data header received"); //Crutch headerByteArrayCrutch = rawData.Take(HeaderFormat.getHeaderFormatSize()).ToArray(); byte[] bodyTmp = rawData.Skip(HeaderFormat.getHeaderFormatSize()).Take(length + 4).ToArray(); BodyFormat dataWithCrc = new BodyFormat(bodyTmp); //Crutch return(dataWithCrc); }
//Done public static TransmittedData ReceiveData(SerialPort serialPort) { TransmittedData recievedData = new TransmittedData(0, 0, new byte[5000000]); HeaderFormat header; BodyFormat body; while (true) { Thread.Sleep(10); try { header = ReceiveHeader(serialPort); } catch (Exception) { serialPort.DiscardInBuffer(); Debug.WriteLine("Wrong format Header"); continue; } if (!HeaderFormat.CheckHeaderCrc(header)) { serialPort.DiscardInBuffer(); Debug.WriteLine("Wrong CRC Header"); continue; } if (header.PacketNum + 1 == TransmittedData.packetCounter) { serialPort.DiscardInBuffer(); Debug.WriteLine("Packet Number is invalid"); Send.SendACK(serialPort, TransmittedData.packetCounter); } Send.SendACK(serialPort, TransmittedData.packetCounter + 1); if (header.Length == 0) { recievedData.MessageCode = header.MessageCode; recievedData.Length = header.Length; return(recievedData); } break; } while (true) { HeaderFormat recievedHeader; try { body = ReceiveFullBody(serialPort, header.Length); recievedHeader = new HeaderFormat(headerByteArrayCrutch); } catch (Exception) { serialPort.DiscardInBuffer(); Debug.WriteLine("Wrong format Header"); continue; } if (!BodyFormat.CheckBodyCrc(body.Data, body.Crc)) { serialPort.DiscardInBuffer(); Debug.WriteLine("When receiving Full Data package, Header CRC is invalid"); continue; } if (recievedHeader.PacketNum + 1 == TransmittedData.packetCounter) { serialPort.DiscardInBuffer(); Debug.WriteLine("Packet Number is invalid"); Send.SendACK(serialPort, TransmittedData.packetCounter); } if (recievedHeader.Length == 0 || !BodyFormat.CheckBodyCrc(body.Data, body.Crc)) { serialPort.DiscardInBuffer(); Debug.WriteLine("CRC of Full Body is invalid"); continue; } Send.SendACK(serialPort, TransmittedData.packetCounter + 1); recievedData.MessageCode = recievedHeader.MessageCode; recievedData.Length = recievedHeader.Length; recievedData.Data = body.Data; break; } TransmittedData.packetCounter++; return(recievedData); }
//Done public static void SendFullBody(SerialPort serialPort, byte[] header, byte[] rawData) { byte[] fullDataWithCrc = Utils.appendTwoArrays(header, BodyFormat.PrepareBodyToSend(rawData)); serialPort.Write(fullDataWithCrc, 0, fullDataWithCrc.Length); Debug.WriteLine($"Full body sended"); }