/// <summary> /// Returns a specific Ssm2Packet type based on given content. /// Does not validate the packet, so manually call the Check method afterwards! /// </summary> /// <param name="bytes"> /// A <see cref="System.Byte[]"/> containing the packet. /// The packet must start at index 0 though the array may be larger than needed. /// </param> /// <returns> /// A <see cref="Ssm2Packet"/> subclass or null if the packet type could not be recognized. /// </returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> public static Ssm2Packet NewFromBytes(byte[] bytes) { if (bytes == null) { throw new ArgumentNullException(); } // check generic minimum size in order to get command byte if (bytes.Length < PacketSizeMin) { throw new ArgumentOutOfRangeException("bytes.Length < Minimum (6)"); } Ssm2Packet p; // Read type directly from command byte (5th byte). // Each derived class constructor checks its requirements. switch ((Ssm2Command)bytes[(int)Ssm2PacketIndex.Command]) { case Ssm2Command.ReadAddressesRequestA8: p = new Ssm2ReadAddressesRequest(bytes); break; case Ssm2Command.ReadAddressesResponseE8: p = new Ssm2ReadAddressesResponse(bytes); break; case Ssm2Command.WriteAddressRequestB8: p = new Ssm2WriteAddressRequest(bytes); break; case Ssm2Command.WriteAddressResponseF8: p = new Ssm2WriteAddressResponse(bytes); break; case Ssm2Command.InitRequestBF: p = new Ssm2InitRequest(bytes); break; case Ssm2Command.InitResponseFF: p = new Ssm2InitResponse(bytes); break; case Ssm2Command.ReadBlockRequestA0: p = new Ssm2ReadBlockRequest(bytes); break; case Ssm2Command.ReadBlockResponseE0: p = new Ssm2ReadBlockResponse(bytes); break; default: return(null); } p.FromBytes(bytes); return(p); }
public void Parse1() { byte[] packetData = TestPacket1; var p = new Ssm2WriteAddressRequest (); p.FromBytes (packetData); Assert.AreEqual (packetData.Length, p.Size, "Size"); Assert.AreEqual (true, p.Check (), "Check()"); Assert.AreEqual (Ssm2Device.Engine10, p.Destination, "Destination"); Assert.AreEqual (Ssm2Device.DiagnosticToolF0, p.Source, "Source"); Assert.AreEqual (Ssm2Command.WriteAddressRequestB8, p.Command, "Command"); Assert.AreEqual (0x6f, p.Address, "Address"); Assert.AreEqual (0x2, p.Data, "Data"); }
public void Parse1() { byte[] packetData = TestPacket1; var p = new Ssm2WriteAddressRequest(); p.FromBytes(packetData); Assert.AreEqual(packetData.Length, p.Size, "Size"); Assert.AreEqual(true, p.Check(), "Check()"); Assert.AreEqual(Ssm2Device.Engine10, p.Destination, "Destination"); Assert.AreEqual(Ssm2Device.DiagnosticToolF0, p.Source, "Source"); Assert.AreEqual(Ssm2Command.WriteAddressRequestB8, p.Command, "Command"); Assert.AreEqual(0x6f, p.Address, "Address"); Assert.AreEqual(0x2, p.Data, "Data"); }
/// <summary> /// Returns a specific Ssm2Packet type based on given content. /// Does not validate the packet, so manually call the Check method afterwards! /// </summary> /// <param name="bytes"> /// A <see cref="System.Byte[]"/> containing the packet. /// The packet must start at index 0 though the array may be larger than needed. /// </param> /// <returns> /// A <see cref="Ssm2Packet"/> subclass or null if the packet type could not be recognized. /// </returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> public static Ssm2Packet NewFromBytes(byte[] bytes) { if (bytes == null) throw new ArgumentNullException (); // check generic minimum size in order to get command byte if (bytes.Length < PacketSizeMin) throw new ArgumentOutOfRangeException ("bytes.Length < Minimum (6)"); Ssm2Packet p; // Read type directly from command byte (5th byte). // Each derived class constructor checks its requirements. switch ((Ssm2Command)bytes[(int)Ssm2PacketIndex.Command]) { case Ssm2Command.ReadAddressesRequestA8: p = new Ssm2ReadAddressesRequest (bytes); break; case Ssm2Command.ReadAddressesResponseE8: p = new Ssm2ReadAddressesResponse (bytes); break; case Ssm2Command.WriteAddressRequestB8: p = new Ssm2WriteAddressRequest (bytes); break; case Ssm2Command.WriteAddressResponseF8: p = new Ssm2WriteAddressResponse (bytes); break; case Ssm2Command.InitRequestBF: p = new Ssm2InitRequest (bytes); break; case Ssm2Command.InitResponseFF: p = new Ssm2InitResponse (bytes); break; case Ssm2Command.ReadBlockRequestA0: p = new Ssm2ReadBlockRequest (bytes); break; case Ssm2Command.ReadBlockResponseE0: p = new Ssm2ReadBlockResponse (bytes); break; default: return null; } p.FromBytes (bytes); return p; }