AsciiMassage
AsciiMassage V1.7
AsciiMassage允许发送和接收消息。更准确地说,它是一个微控制器消息打包器和解析器,适用于任何类型的流(串行、UDP 等)的 ASCII 格式。消息消息始终以地址字符串开头,后跟用户定义数量的元素(字节、整数、长整数、浮点数或字符串)。地址字符串用于路由消息。
这是 Massage API 的 ASCII 实现。
https://github.com/SofaPirate/AsciiMassage
完整的类文档可以在“docs”文件夹中或在此处在线找到。
将库添加到代码顶部并实例化一个名为“inbound”的 AsciiMassageParser:
# include < AsciiMassageParser.h >
AsciiMassageParser inbound;
在loop()内部使用parse()解析串行流。如果 parse() 返回 true,则按摩已完成并准备就绪。
if ( inbound.parseStream( &Serial ) ) {
// parse completed massage elements here.
}
此示例解析以地址“value”开头且包含一个 long 后跟一个 int 的消息的元素:
// Does the massage's address match "value"?
if ( inbound.fullMatch ( " value " ) ) {
// Get the first long.
long ms = inbound. nextLong ();
// Get the next int.
int an0 = inbound. nextInt ();
}
完整的代码块如下:
# include < AsciiMassageParser.h >
AsciiMassageParser inbound;
// [...]
void loop () {
if ( inbound. parseStream ( &Serial ) ) {
// parse completed massage elements here.
// Does the massage's address match "value"?
if ( inbound. fullMatch ( " value " ) ) {
// Get the first long.
long ms = inbound. nextLong ();
// Get the next int.
int an0 = inbound. nextInt ();
}
}
// [...]
}
将库添加到代码顶部并实例化一个名为“inbound”的 AsciiMassageParser:
# include < AsciiMassageParser.h >
AsciiMassageParser inbound;
在loop()之前添加一个函数,当收到消息时将调用该函数
void readMessage () {
// parse completed massage elements here.
}
在loop()内部使用parse()解析串行流,并在收到消息时运行“readMessage”
inbound.parseStream( &Serial , readMessage );
完整的代码块如下:
# include < AsciiMassageParser.h >
AsciiMassageParser inbound;
// [...]
void readMessage () {
// parse completed massage elements here.
// Does the massage's address match "value"?
if ( inbound. fullMatch ( " value " ) ) {
// Get the first long.
long ms = inbound. nextLong ();
// Get the next int.
int an0 = inbound. nextInt ();
}
}
void loop () {
inbound. parseStream ( &Serial , readMessage );
// [...]
}
将库添加到代码顶部并实例化一个名为“outbound”的 AsciiMassagePacker:
# include < AsciiMassagePacker.h >
AsciiMassagePacker outbound;
打包一个按摩,然后通过串口蒸:
outbound.beginPacket( " value " ); // Start a packet with the address called "value".
outbound.addLong( millis() ); // Add the milliseconds.
outbound.addInt( analogRead( 0 ) ); // Add a reading of analog 0.
outbound.streamPacket(&Serial); // End the packet and stream it.