private Olimexino328_packet ReadNextPacket(BinaryReader BR, ref int iPosition) { Olimexino328_packet pk = new Olimexino328_packet(); pk.sync0 = 0; pk.sync1 = 0; while ((pk.sync0 != 0xa5) && (pk.sync1 != 0x5a)) { BR.BaseStream.Position = iPosition; if (iPosition > BR.BaseStream.Length - 3) { return(pk); } pk.sync0 = BR.ReadByte(); pk.sync1 = BR.ReadByte(); iPosition++; } pk.version = BR.ReadByte(); pk.count = BR.ReadByte(); pk.d1 = ReverseBytes(BR.ReadUInt16()); pk.d2 = ReverseBytes(BR.ReadUInt16()); pk.d3 = ReverseBytes(BR.ReadUInt16()); pk.d4 = ReverseBytes(BR.ReadUInt16()); pk.d5 = ReverseBytes(BR.ReadUInt16()); pk.d6 = ReverseBytes(BR.ReadUInt16()); pk.switches = BR.ReadByte(); return(pk); }
private void ParseBuffer() { while ((1 == 1) && (Buffer.Count > 0)) { // packet header available and one packet available while ((Buffer.Count > 0) && (Buffer[0] != 0xa5)) { Buffer.RemoveAt(0); } if ((Buffer.Count >= 17) && (Buffer[0] == 0xa5)) { // read packet byte[] pkg = new byte[17]; Olimexino328_packet pk = new Olimexino328_packet(); for (int i = 0; i < 17; i++) { pkg[i] = Buffer[0]; Buffer.RemoveAt(0); } if ((pkg[0] == 0xa5) && (pkg[1] == 0x5a)) { pk.sync0 = pkg[0]; pk.sync1 = pkg[1]; pk.version = pkg[2]; pk.count = pkg[3]; pk.d1 = (UInt16)(pkg[5] + (pkg[4] << 8)); pk.d2 = (UInt16)(pkg[7] + (pkg[6] << 8)); pk.d3 = (UInt16)(pkg[9] + (pkg[8] << 8)); pk.d4 = (UInt16)(pkg[11] + (pkg[10] << 8)); pk.d5 = (UInt16)(pkg[13] + (pkg[12] << 8)); pk.d6 = (UInt16)(pkg[15] + (pkg[14] << 8)); pk.switches = pkg[16]; rtfv1.Pop(pk.d1); rtfv2.Pop(pk.d2); rtfv3.Pop(pk.d3); rtfv4.Pop(pk.d4); rtfv5.Pop(pk.d5); rtfv6.Pop(pk.d6); } else { Debug.WriteLine("Invalid packet received!"); } } else { break; } } }
private Olimexino328_packet ReadNextPacket(BinaryReader BR, ref int iPosition) { Olimexino328_packet pk = new Olimexino328_packet(); pk.sync0 = 0; pk.sync1 = 0; while ((pk.sync0 != 0xa5) && (pk.sync1 != 0x5a)) { BR.BaseStream.Position = iPosition; if (iPosition > BR.BaseStream.Length-3) { return pk; } pk.sync0 = BR.ReadByte(); pk.sync1 = BR.ReadByte(); iPosition++; } pk.version = BR.ReadByte(); pk.count = BR.ReadByte(); pk.d1 = ReverseBytes(BR.ReadUInt16()); pk.d2 = ReverseBytes(BR.ReadUInt16()); pk.d3 = ReverseBytes(BR.ReadUInt16()); pk.d4 = ReverseBytes(BR.ReadUInt16()); pk.d5 = ReverseBytes(BR.ReadUInt16()); pk.d6 = ReverseBytes(BR.ReadUInt16()); pk.switches = BR.ReadByte(); return pk; }
private void ParseBuffer() { while ((1 == 1) && (Buffer.Count > 0)) { // packet header available and one packet available while ((Buffer.Count > 0) && (Buffer[0] != 0xa5)) Buffer.RemoveAt(0); if ((Buffer.Count >= 17) && (Buffer[0] == 0xa5)) { // read packet byte[] pkg = new byte[17]; Olimexino328_packet pk = new Olimexino328_packet(); for (int i = 0; i < 17; i++) { pkg[i] = Buffer[0]; Buffer.RemoveAt(0); } if ((pkg[0] == 0xa5) && (pkg[1] == 0x5a)) { pk.sync0 = pkg[0]; pk.sync1 = pkg[1]; pk.version = pkg[2]; pk.count = pkg[3]; pk.d1 = (UInt16)(pkg[5] + (pkg[4] << 8)); pk.d2 = (UInt16)(pkg[7] + (pkg[6] << 8)); pk.d3 = (UInt16)(pkg[9] + (pkg[8] << 8)); pk.d4 = (UInt16)(pkg[11] + (pkg[10] << 8)); pk.d5 = (UInt16)(pkg[13] + (pkg[12] << 8)); pk.d6 = (UInt16)(pkg[15] + (pkg[14] << 8)); pk.switches = pkg[16]; rtfv1.Pop(pk.d1); rtfv2.Pop(pk.d2); rtfv3.Pop(pk.d3); rtfv4.Pop(pk.d4); rtfv5.Pop(pk.d5); rtfv6.Pop(pk.d6); } else { Debug.WriteLine("Invalid packet received!"); } } else { break; } } }