Add function encodePacket and test.