⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lesson6.html

📁 tinyos中文手册,是根据tinyos系统自带手册翻译过来的,虽然质量不好,但是对英文不强的人还是有用的
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en"><html><head>  <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">  <meta content="Microsoft FrontPage 5.0" name="GENERATOR">  <title>Lesson 6: Displaying Data on a PC</title></head><body vlink="#005bb7" link="#005bb7" bgcolor="#f8f8ff">&nbsp;<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><font face="tahoma,arial,helvetica"><font size="-1">Lesson 6: Displaying Data on a PC</font></font></b>      <p><font face="tahoma,arial,helvetica">Last updated 23 September2003</font></p>      </td>    </tr>  </tbody></table><p>To goal of this lesson is to integrate the sensor network with a PC,allowing us to display sensor readings on the PC as well as tocommunicate from the PC back to the motes. First, we'll introduce thebasic tools used to read sensor network data on a desktop over theserial port. Next we'll demonstrate a Java application that displayssensor readings graphically. Finally, we'll close the communicationloopby showing how to send data back to the motes. <br>&nbsp;<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">TheOscilloscope application</font></nobr></b></td>    </tr>  </tbody></table></p><p>The mote application we use in this lesson is found in <tt><a href="../../apps/Oscilloscope">apps/Oscilloscope</a></tt>. It consistsof a single module that reads data from the photo sensor. For each 10sensor readings, the module sends a packet to the serial portcontainingthose readings. The mote only sends the packets over the serialport.&nbsp; (To see how the data can be sent over the radio see <a href="../../apps/OscilloscopeRF">apps/OscilloscopeRF</a>.) </p><p>Compile and install the <tt>Oscilloscope</tt> application on amote.You will need to connect a sensor board to the mote to get the lightreadings. Remember to set the <tt>SENSORBOARD</tt> option in <tt><a href="../../apps/Oscilloscope/Makefile">apps/Oscilloscope/Makefile</a></tt>to either <tt>micasb</tt> or <tt>basicsb</tt> depending on the typeofsensor board you have. </p><p>This application requires that the mote with the sensor be connectedto the serial port on the programming board. Note that the size of thecurrent Mica sensor board prevents you from plugging the mote and boardinto the programming board directly. One workaround is to use a shortcable to connect the programming board to the sensor board connector. </p><p>When the <tt>Oscilloscope</tt> application is running, the red LEDlights when the sensor reading is over some threshold (set to <tt>0x0300)</tt>by default in the code - you might want to change this to a highervalueif it never seems to go off in the dark). The yellow LED is toggledwhenever a packet is sent to the serial port. <br>&nbsp;<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">The'listen' tool: displaying raw packet data</font></nobr></b></td>    </tr>  </tbody></table></p><p>The first step to establishing communication between the PC and themote is to connect up your serial port cable to the programming board,and to make sure that you have Java and the <tt>javax.comm</tt>packageinstalled. After programming your mote with the <tt>Oscilloscope</tt>code, <tt>cd</tt> to the <tt>tools/java</tt> directory, and type </p><pre>&nbsp; <tt>make</tt></pre><pre>&nbsp; <tt>export MOTECOM=serial@<i>serialport:baudrate</i></tt></pre>The environment variable <tt>MOTECOM</tt> tells the java Listen tool(and most other tools too) which packets it should listen to. Here <tt>serial@<i>serialport:baudrate</i></tt>says to listen to a mote connected to a serial port, where <tt>serialport</tt>isthe serial port that you have connected the programming board to, and <tt>baudrate</tt>is the specific baudrate of the mote. For the mica and mica2dot motes,the baud rate is 19200, for the mica2 it is 57600 baud. You can alsousea mote name as the baudrate (in which case that motes baudrate isselected). So you could do any of:<br><pre>&nbsp; <tt>export MOTECOM=serial@COM1:19200 # mica baud rate<br>  export MOTECOM=serial@COM1:mica  # mica baud rate, again<br>  export MOTECOM=serial@COM2:mica2 # the mica2 baud rate, on a different serial port<br>  export MOTECOM=serial@COM3:57600 # explicit mica2 baud rate<br></tt></pre>Set <span style="font-family: monospace;">MOTECOM</span>appropriately,then run<pre>&nbsp; <tt>java net.tinyos.tools.Listen</tt></pre>You should see some output resembling the following:<pre>% java net.tinyos.tools.Listen</pre><pre>serial@COM1:19200: resynchronising<br>7e 00 0a 7d 1a 01 00 0a 00 01 00 46 03 8e 03 96 03 96 03 96 03 97 03 97 03 97 03 97 03 97 03<br>7e 00 0a 7d 1a 01 00 14 00 01 00 96 03 97 03 97 03 98 03 97 03 96 03 97 03 96 03 96 03 96 03<br>7e 00 0a 7d 1a 01 00 1e 00 01 00 98 03 98 03 96 03 97 03 97 03 98 03 96 03 97 03 97 03 97 03</pre>The program is simply printing the raw data of each packet receivedfrom the serial port.<p>Before continuning, execute <tt>unset MOTECOM</tt> to avoid forcingall java applications to use the serial port to get packets. </p><p>If you don't have the <tt>javax.comm</tt> package installedproperly, then the program will complain that it can't find the serialport. If you do not see that data lines on the screen, you may havechosen the wrong COM port or the mote may not be correctly connected tothe computer. <br>&nbsp;<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">Now whatare you seeing?</font></nobr></b></td>    </tr>  </tbody></table></p><p>The application that you are running is simply printing out thepackets that are coming from the mote. Each data packet that comes outof the mote contains several fields of data. Some of these fields aregeneric Active Message fields, and are defined in <tt><a href="../../tos/types/AM.h">tos/types/AM.h</a></tt>. The data payloadof the message, which is defined by the application, is defined in <tt><a href="../../apps/Oscilloscope/OscopeMsg.h">apps/Oscilloscope/OscopeMsg.h</a></tt>.The overall message format for the Oscilloscope application is asfollows: </p><ul>  <li> <b>Destination address</b> (2 bytes)</li>  <li> <b>Active Message handler ID</b> (1 byte)</li>  <li> <b>Group ID</b> (1 byte)</li>  <li> <b>Message length</b> (1 byte)</li>  <li> <b>Payload</b> (up to 29 bytes):</li>  <ul>    <li> <b>source mote ID</b> (2 bytes)</li>    <li> <b>sample counter</b> (2 bytes)</li>    <li> <b>ADC channel</b> (2 bytes)</li>    <li> <b>ADC data readings</b> (10 readings of 2 bytes each)</li>  </ul></ul>So we can interpret the data packet as follows:<table hspace="0" cellpadding="0" cellspacing="10" border="0">  <tbody>    <tr bgcolor="#d0d0d0">      <td><b>dest addr</b></td>      <td><b>handlerID</b></td>      <td><b>groupID</b></td>      <td><b>msg len</b></td>      <td><b>source addr</b></td>      <td><b>counter</b></td>      <td><b>channel</b></td>      <td><b>readings</b></td>      <td><br>      </td>    </tr>    <tr>      <td bgcolor="#d0d0ff">7e 00</td>      <td bgcolor="#d0d0ff">0a</td>      <td bgcolor="#d0d0ff">7d</td>      <td bgcolor="#d0d0ff">1a</td>      <td bgcolor="#d0ffd0">01 00</td>      <td bgcolor="#d0ffd0">14 00</td>      <td bgcolor="#d0ffd0">01 00</td>      <td bgcolor="#ffd0d0">96 03 97 03 97 03 98 03 97 03 96 03 97 0396 03 96 03 96 03</td>    </tr>  </tbody></table><p>Note that the data is sent by the mote in <i>little-endian</i>format; so, for example, the two bytes <tt>96 03</tt> represent asinglesensor reading with most-significant-byte <tt>0x03</tt> andleast-significant-byte <tt>0x96</tt>. That is, <tt>0x0396</tt> or 918decimal. </p><p>Here is an excerpt from <tt>OscilloscopeM.nc</tt> showing the databeing written to the packet: </p><center><table hspace="4" width="80%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0e0">      <td width="100%"><b>OscilloscopeM.nc</b>      <pre>&nbsp;&nbsp; async event result_t ADC.dataReady(uint16_t data) {<br>&nbsp;&nbsp;&nbsp; struct OscopeMsg *pack;<br><br>&nbsp;&nbsp;&nbsp; atomic {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pack = (struct OscopeMsg *)msg[currentMsg].data;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add the new sensor reading to the packet&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // and update the number of bytes in this packet&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pack-&gt;data[packetReadingNumber++] = data;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; readingNumber++; // increment total number of bytes<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbg(DBG_USR1, "data_event\n");<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // if the packet is full, send the packet out&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (packetReadingNumber == BUFFER_SIZE) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post dataTask();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; if (data &gt; 0x0300)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; call Leds.redOn();<br>&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; call Leds.redOff();<br><br>&nbsp;&nbsp;&nbsp; return SUCCESS;<br>&nbsp; }</pre>      <pre>&nbsp; task void dataTask() {<br>&nbsp;&nbsp;&nbsp; struct OscopeMsg *pack;<br>&nbsp;&nbsp;&nbsp; atomic {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pack = (struct OscopeMsg *)msg[currentMsg].data;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; packetReadingNumber = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pack-&gt;lastSampleNumber = readingNumber;<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; pack-&gt;channel = 1;<br>&nbsp;&nbsp;&nbsp; pack-&gt;sourceMoteID = TOS_LOCAL_ADDRESS;<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp; /* Try to send the packet. Note that this will return<br>&nbsp;&nbsp;&nbsp;&nbsp; * failure immediately if the packet could not be queued for<br>&nbsp;&nbsp;&nbsp;&nbsp; * transmission.<br>&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp; if (call DataMsg.send(TOS_UART_ADDR, sizeof(struct OscopeMsg),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;msg[currentMsg]))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; atomic {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; currentMsg ^= 0x1; // flip-flop between two message buffers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; call Leds.yellowToggle();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp; }</pre>      </td>    </tr>  </tbody></table></center><br>&nbsp;<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">  <tbody>    <tr bgcolor="#e0e0ff">      <td width="100%"><b><nobr><font face="arial,helvetica">TheSerialForwarder Program</font></nobr></b></td>    </tr>  </tbody></table><p>The <tt>Listen</tt> program is the most basic way of communicatingwith the mote; it directly opens the serial port and just dumps packetsto the screen. Obviously it is not easy to visualize the sensor datausing this program. What we'd really like is a better way of retrievingand observing data coming from the sensor network. </p><p>The <tt>SerialForwarder</tt> program is used to read packet datafrom a serial port and forward it over an Internet connection, so thatother programs can be written to communicate with the sensor networkover the Internet. To run the serial forwarder, cd to <tt>tools/java</tt>and run the program </p><pre>&nbsp; java net.tinyos.sf.SerialForwarder -comm serial@COM1:&lt;baud rate&gt;</pre>This will open up a GUI window that looks similar to the following:<center><p><img height="300" width="500" src="imgs/SerialForwarder.JPG"></p></center><p>The <tt>-comm</tt> argument tells <tt>SerialForwarder </tt>tocommunicate over serial port <span style="font-family: monospace;">COM1</span>.<tt></tt>The<span style="font-family: monospace;">-comm</span> argumentspecifies wherethe packets <span style="font-family: monospace;">SerialForwarder</span>should forward come from, using the same syntax as the <span style="font-family: monospace;">MOTECOM</span> environment variableyousaw above (you can run '<span style="font-family: monospace;">javanet.tinyos.packet.BuildSource</span>' to get a list of valid sources).Unlike most other programs, SerialForwarder does not pay attention tothe <span style="font-family: monospace;">MOTECOM</span> environmentvariable; you must use the <span style="font-family: monospace;">-comm</span>argument to specify the packet source (The rationale is that you wouldtypically set <span style="font-family: monospace;">MOTECOM</span> tospecify a serial forwarder, and that serial forwader should talk to,e.g., a serial port. You wouldn't want the <span style="font-family: monospace;">SerialForwarder</span> to talk toitself...). </p><p>The <tt>&lt;baud rate&gt;</tt> argument tells <tt>SerialForwarder </tt>tocommunicate at specified baud rate. <tt> </tt></p><p><tt>SerialForwarder </tt>does not display the packet data itself,but rather updates the packet counters in the lower-right hand cornerofthe window. Once running, the serial forwarder listens for networkclient connections on a given TCP port (9001 is the default), andsimplyforwards TinyOS messages from the serial port to the network clientconnection, and vice versa. Note that multiple applications can connectto the serial forwarder at once, and all of them will receive a copy ofthe messages from the sensor network. </p><p>More information is available on <span style="font-family: monospace;">SerialForwarder</span> and packetsources is found in the <a href="../serialcomm/index.html">SerialForwarder Documentation</a><br>&nbsp;<table hspace="4" width="100%" cellpadding="3" cellspacing="2" border="0">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -