# pktparser

pktparser(parser\_packet) performs content interpretation on the byte sequence converted by [serparser](/latest_en/api-reference/classes/ser_parser.md) performs content interpretation on the byte sequence converted by [serparser](/latest_en/api-reference/classes/ser_parser.md).

```cpp
serparser_heap parser_ser;

void setup() {
    // init ser parser (heap alloc)
    parser_ser.begin(PARSER::ASCII, 256);
}

void loop() {
    int c;
    while ((c = Serial.read()) >= 0) {
        parser_ser.parse(c);
        if (parser_ser.available()) {
            // get buffer object
            auto&& payl = parser_ser.get_buf();
            // identify packet type
            auto&& typ = identify_packet_type(payl.begin(), payl.end());
            
            // if packet type is TWELITE standard 0x81 message
            if (typ == E_PKT::PKT_TWELITE) {
                pktparser pkt; // packet parser object
                // analyze packet data
                typ = pkt.parse<TwePacketTwelite>(payl.begin(), payl.end());
                
                if (typ != E_PKT::PKT_ERROR) { // success!
                    // get data object
                    auto&& atw = pkt.use<TwePacketTwelite>();
                    
                    // display packet inforamtion
                    Serial << crlf << format("TWELITE: SRC=%08X LQI=%03d "
                        , app.u32addr_src, app.u8lqi);
	                  Serial << " DI1..4="
	                      << atw.DI1 ? 'L' : 'H' << atw.DI2 ? 'L' : 'H' 
                        << atw.DI3 ? 'L' : 'H' << atw.DI4 ? 'L' : 'H';
                }
            }
        }
    }
}
```

The above example interprets [standard application 0x81 message](https://wings.twelite.info/how-to-use/parent-mode/receive-message/app_twelite). parser\_ser object converts the message input from Serial into a byte string. This byte string is first identified by [`identify_packet_type()`](https://github.com/monowireless/doc_MWX_jp/blob/latest_e/api-reference/classes/pktparser/identify_packet_type.md) to determine the type of the message [`E_PKT`](/latest_en/api-reference/classes/pktparser/e_pkt.md). Once the message type is determined, the message is parsed by `.parse<TwePacketTwelite>()`. The parsed result will be of type `TwePacketTwelite`, and `.use<TwePacketTwelite>()` is the procedure to extract this object. The `TwePacketTwelite` type is a class, but it refers to its member variables directly as a structure.

### parse\<T>

```cpp
template <class T>
E_PKT parse(const uint8_t* p, const uint8_t* e)
```

Parses a sequence of bytes.

The `T` specifies the packet type to be parsed. For example, `TwePacketTwelite` is specified for 0x81 messages in standard applications.

`p` and `e` specify the next to the beginning and the end of the byte sequence.

The return value is of type `E_PKT`. In case of an error, `E_PKT::PKT_ERROR` is returned.

### user\<T>

```
template <class T> 
T& use()
```

Returns a reference to an object corresponding to the packet type of the interpreted byte sequence. It can be called if `parse<T>` was executed beforehand and there were no errors.

The `T` can be the same type as the one executed with `parse<T>`, or a `TwePacket` from which only basic information can be obtained.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mwx.twelite.info/latest_en/api-reference/classes/pktparser.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
