Ejemplo n.º 1
0
        //Done
        public static void SendHeader(SerialPort serialPort, int transmissionCode, int length, int packetNum, int timeout = 10000)
        {
            int curr_timeout = serialPort.WriteTimeout;

            serialPort.WriteTimeout = timeout;
            byte[] header = HeaderFormat.HeaderByteFormat(transmissionCode, length, packetNum);
            serialPort.Write(header, 0, sizeof(int) * 4);
            Debug.WriteLine($"Data header sended");
            serialPort.WriteTimeout = curr_timeout;
        }
Ejemplo n.º 2
0
        public static byte[] HeaderByteFormat(HeaderFormat header)
        {
            var memoryStream = new MemoryStream();
            var binaryWriter = new BinaryWriter(memoryStream);

            binaryWriter.Write(header.MessageCode);
            binaryWriter.Write(header.Length);
            binaryWriter.Write(header.PacketNum);
            binaryWriter.Write(header.Crc);
            binaryWriter.Close();
            return(memoryStream.ToArray());
        }
Ejemplo n.º 3
0
        //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);
        }
Ejemplo n.º 4
0
        //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);
        }
Ejemplo n.º 5
0
        //Done
        public static HeaderFormat ReceiveHeader(SerialPort serialPort, int timeout = 1000)
        {
            HeaderFormat header = new HeaderFormat(ReceiveRawHeader(serialPort, timeout));

            return(header);
        }
Ejemplo n.º 6
0
        //dOnE
        public static bool SendData(SerialPort serialPort, int transmissionCode, int length, byte[] body, int timeout = 1000)
        {
            HeaderFormat header;

            serialPort.WriteTimeout = timeout;
            while (true)
            {
                //bruh
                Thread.Sleep(10);
                //bruh
                SendHeader(serialPort, transmissionCode, length, TransmittedData.packetCounter, timeout);
                try
                {
                    header = Receive.ReceiveHeader(serialPort);
                }
                catch (Exception)
                {
                    serialPort.DiscardInBuffer();
                    Debug.WriteLine("Error occured, retrying in sendinng data Header");
                    continue;
                }
                if (!(HeaderFormat.CheckHeaderCrc(header)))
                {
                    serialPort.DiscardInBuffer();
                    Debug.WriteLine("Header CRC is invalid");
                    continue;
                }
                if (header.PacketNum + 1 == TransmittedData.packetCounter)
                {
                    serialPort.DiscardInBuffer();
                    Debug.WriteLine("Packet Number is invalid");
                    continue;
                }
                break;
            }

            TransmittedData.packetCounter++;
            if (length == 0)
            {
                return(true);
            }

            while (true)
            {
                SendFullBody(serialPort, HeaderFormat.HeaderByteFormat(transmissionCode, length, TransmittedData.packetCounter), body);
                try
                {
                    header = Receive.ReceiveHeader(serialPort);
                }
                catch (Exception)
                {
                    serialPort.DiscardInBuffer();
                    Debug.WriteLine("Error occured, retrying in sendinng Full Data");
                    continue;
                }
                if (!(HeaderFormat.CheckHeaderCrc(header)))
                {
                    serialPort.DiscardInBuffer();
                    Debug.WriteLine("When receiving Full Data package, Header CRC is invalid");
                    continue;
                }
                if (header.PacketNum + 1 == TransmittedData.packetCounter)
                {
                    serialPort.DiscardInBuffer();
                    Debug.WriteLine("Packet Number is invalid");
                    continue;
                }
                TransmittedData.packetCounter++;
                return(true);
            }
        }
Ejemplo n.º 7
0
 //Done
 public static void SendACK(SerialPort serialPort, int packetNum, int timeout = 10000)
 {
     byte[] rawData = HeaderFormat.HeaderByteFormat(Transmission.ACK, 0, packetNum);
     serialPort.Write(rawData, 0, rawData.Length);
     Debug.WriteLine("Acknowledge sended");
 }
Ejemplo n.º 8
0
        public static bool CheckHeaderCrc(HeaderFormat headerReceived)
        {
            HeaderFormat headerComputed = new HeaderFormat(headerReceived.MessageCode, headerReceived.Length, headerReceived.PacketNum);

            return(headerReceived.Crc == headerComputed.Crc);
        }