参考TinyOS官方网站学习Mote-PC serial communication
- packet source
节点与计算机的通信都基于 packet source ,计算机上的应用程序通过它接受来自节点的信息包,也可以发送信息包到节点。具体有串口、TCP Socket、SerialForwarder,etc。许多TinyOS通信工具带有“-comm”参数,通过字符串指示packet source。
例如:
监听工具Listen将串口/dev/ttyUSB0作为packet source,以micaz波特率监听节点。
- 端口测试
编译下载TestSerial程序,程序每秒向PC发送一个信息包,节点接收信息包并将其***二进制通过LED显示。Listen工具监听结果:
运行TestSerial中的Java程序:
- Send 工具--发送原始数据
节点LED显示最后一个字节的低三位。
- BaseStation
TinyOS中一个基本的但是常用的示例,功能:当节点的串口接收到消息,它将该消息通过无线发送出去;当节点通过无线接收到信息时,则将该消息发送到串口。Led0闪烁:节点使用无线发送消息;Led1闪烁:节点发送消息到串口;Led2闪烁:丢包。
将BaseStation与BlinkToRadio配合使用,可看到BaseStation节点Led1闪烁,用Listen工具监听数据包:
数据包各字段含义具体参考TinyOS官方网站信息。
-
Errors & Qusetions
Solution: manually remove the BlinkToRadioMsg.java and BlinkToRadioMsg.class files before remaking.This worked for me anyway.
-
MIG: Message Interface Generator
Listen工具仅显示二进制数据,不能表达数据的含义,而MIG工具可以从消息包中自动解析有效信息。分别修改BlinkToRadio应用程序中Makefile , BlinkToRadio.h , BlinkToRadioAppc.nc文件:
- COMPONENT=BlinkToRadioAppC
- BUILD_EXTRA_DEPS=BlinkToRadioMsg.class
- BlinkToRadioMsg.class: BlinkToRadioMsg.java
- javac BlinkToRadioMsg.java
- BlinkToRadioMsg.java:
- mig java -target=null -java-classname=BlinkToRadioMsg BlinkToRadio.h BlinkToRadioMsg -o [email protected]
- include $(MAKERULES)
- #ifndef BLINKTORADIO_H
- #define BLINKTORADIO_H
- enum {
- TIMER_PERIOD_MILLI = 250,
- AM_BLINKTORADIOMSG = 6
- };
- typedef nx_struct BlinkToRadioMsg {
- nx_uint16_t nodeid;
- nx_uint16_t counter;
- } BlinkToRadioMsg;
- #endif
- #include <Timer.h>
- #include "BlinkToRadio.h"
- configuration BlinkToRadioAppC {
- }
- implementation {
- components MainC;
- components LedsC;
- components BlinkToRadioC as App;
- components new TimerMilliC() as Timer0;
- components ActiveMessageC;
- components new AMSenderC(AM_BLINKTORADIOMSG);
- components new AMReceiverC(AM_BLINKTORADIOMSG);
- App.Boot -> MainC;
- App.Leds -> LedsC;
- App.Timer0 -> Timer0;
- App.Packet -> AMSenderC;
- App.AMSend -> AMSenderC;
- App.AMPacket -> AMSenderC;
- App.AMControl -> ActiveMessageC;
- App.Receive -> AMReceiverC;
- }
同样,将BaseStation与BlinkToRadio配合使用,将BaseStation节点连接到串口,另一个节点运行BlinkToRadio,用MsgReader工具根据MIG产生的BlinkToRadioMsg对象解析消息包:
-
SerialForwarder
以上的串口通信方式都有一个限制:只有一个PC程序可以与节点进行交互,且必须在与节点有真正的物理连接的PC上运行交互程序。SerialForwarder工具解决了这个问题,SerialForwarder程序打开信息源,然后让其他众多的应用程序再通过TCP/IP连接到SerialForwarder。In fact,SerialForwarder is the second kind of packet source. A SerialForwarder source has this syntax: [email protected]:PORT
- 先运行SerialForwarder :
这是将看到,SerialForwarder的Clients数量增加,MsgReader打印信息包。
-
Sending a packet to the serial port
- 修改BlinkToRadioAppC中的配件:
- #include <Timer.h>
- #include "BlinkToRadio.h"
- configuration BlinkToRadioAppC {
- }
- implementation {
- components MainC;
- components LedsC;
- components BlinkToRadioC as App;
- components new TimerMilliC() as Timer0;
- components SerialActiveMessageC;
- components new SerialAMSenderC(AM_BLINKTORADIOMSG);
- components new AMReceiverC(AM_BLINKTORADIOMSG);
- App.Boot -> MainC;
- App.Leds -> LedsC;
- App.Timer0 -> Timer0;
- App.Packet -> SerialAMSenderC;
- App.AMSend -> SerialAMSenderC;
- App.AMPacket -> SerialAMSenderC;
- App.AMControl -> SerialActiveMessageC;
- App.Receive -> AMReceiverC;
- }
然后编译下载到节点,节点与串口直接物理连接,使用MsgReader工具测试:
代码修改之后BlinkToRadio直接发送信息到串口。
转载于:https://blog.51cto.com/xjhznick/1152073