serparser
serial format input-output (mwx::serial_parser)
Used for serial format input/output. It has an internal buffer that holds the interpreted binary series. On input, the series is stored in the internal buffer one byte at a time according to the read format, and becomes complete when the interpretation of the series is complete. Conversely, on output, the buffer is output from the internal buffer according to the specified output format.
Three class names are defined according to the memory buffer handling method (alloc
).
constants (format type)
The type of format to pass in the initialization parameter of begin()
. There are two types here: ASCII format and binary format.
constant | type |
---|---|
| ASCII format |
| binary format |
Binary format is generally more complicated to handle than ASCII format, including the necessary tools and confirmation methods. Normally, ASCII format should be used.
About format
ASCII format
The ASCII format is a way to represent a sequence of data in binary as a string.
For example, 00A01301FF123456
in ASCII format is expressed as follows. The beginning is :
, B1
is the checksum, and the end is [CR:0x0d][LF:0x0a]
.
:00A01301FF123456B1[CR][LF]
You can omit the terminating checksum. Replace the CRLF series from the checksum with X
. This is useful when you want to send data for experiments, etc., although it is less vulnerable to wrong data series due to garbled characters.
:00A01301FF123456X
Definition.
====== | Bytes of original data | Bytes | Description |
---|---|---|---|
header | 1 | ||
Data part | N | 2N | Each byte of the original data is represented by two ASCII characters (A-F in upper case).
For example, 0x1F is represented as |
checksum | 2 | The sum of each byte of the data part is calculated in 8-bit width and taken as 2's complement. In other words, the sum of each byte of the data part plus the checksum byte is calculated to be 0 in 8-bit width.
The checksum byte is represented by two ASCII characters.
For example, in | |
footer | 2 | [CR] (0x0D) [LF] (0x0A) |
Binary Format
Normally, ASCII format should be used.
However, to check the transmission and reception in experiments, it is necessary to prepare a special terminal that supports binary communication, and checksum calculation is also required. It is more difficult to use than the ASCII format.
Binary format is a method of sending a sequence of data consisting of binary data with a header and a checksum.
For example, 00A01301FF123456
is expressed in binary format as follows.
0xA5 0x5A 0x80 0x08 0x00 0xA0 0x13 0x01 0xFF 0x12 0x34 0x56 0x3D
Definition.
====== | Bytes of original data | Bytes in format | Description |
---|---|---|---|
header | 2 | ||
Data Length | 2 | The data length is two bytes in big-endian format, with MSB (0x8000) set and the length of the data part specified.
For example, if the length of the data part is 8 bytes, specify | |
Data part | N | N | Specifies the original data. |
Checksum | 1 | Calculates the XOR of each byte of the data part.
For example, if the data part is | |
footer | (1) | The checksum is effectively the end of the line. The output from the radio module will be appended with |
Methods
declaration, begin()
The declaration specifies the memory allocation class. Since this specification is complicated, an alias definition is used as described above.
Class name (alias definition) Memory Allocation | Contents |
| specify an already existing buffer with |
| allocate a buffer of N bytes internally |
| allocate a heap of the size specified by the parameters of the |
Calls the begin()
method according to the memory allocation class.
serparser_attach
The buffer specified by p
is used in the [format] (ser_parser.md#nitsuite) specified by ty
. The maximum length of the buffer is specified by max_siz
and the effective data length of the buffer by siz
.
This definition is used especially when you want to format output data columns (see >>
operator).
serparser_local<N> - Allocate internal buffer
Initialize with the format specified by ty
.
serparser_heap - Allocated to heap
Initialize the heap by allocating the size specified by siz
to the heap in the format specified by ty
.
Once allocated, heap space cannot be released.
get_buf()
Returns an internal buffer. The buffer will be of type smplbuf<uint8_t, alloc>
.
parse()
Processes input characters. Receives a single byte of input string of formatted input and interprets it according to the format. For example, in ASCII format, it receives a series like :00112233X
as input. X, one byte at a time, and when the last
X` is entered, the interpretation of the format is completed and reported as done.
The parameter to parse()
is the input byte, and the return value is true
if the interpretation is complete.
When parse()
reports that reading is complete, the next parse()
will return to the reading-in-progress status.
例
operator bool()
If true
, reading is completed by parse()
; if false
, interpretation is in progress.
Example (parse() example can be rewritten as follows)
<< operator
Outputs the internal buffer to the stream (Serial) in a formatted format.
Example
最終更新