Hello there, I'm here to tell you how to interact with Telos Motes specially, On Windows or Linux from scratch.
I assume you have a very good experience with C programming language(Surely, You can use different languages), also with network packets structures, in addition to how to write and read buffers from USB Port in a specified rate. Both on Microsoft Windows and Linux.
I assume you have a very good experience with C programming language(Surely, You can use different languages), also with network packets structures, in addition to how to write and read buffers from USB Port in a specified rate. Both on Microsoft Windows and Linux.
There's no need to read all the tutorials written by the TinyOS team from beginning, Just install the TestSerial Application on the mote.
If you do not know how to install it on the mote, read only their installation tutorials well, you will have to change some files of the Linux Kernel, then recompile it, install it on your machine. Transfer code. I did not try it myself, takes time, But easy. Then you will be able to upload your compiled nesC code to the motes.
Now after installing the TestSerial application, Connect your Mote to your machine.
-For Windows, Check which COM Port is specified for this mote, and start listening from it using the Application Listen.java.
-For Linux, You have to google how to write and read through USB ports.
-You have to include the tinyos.jar file before compilation.
But this file contains .class files, it is not for debugging, so get the source code of these classes from the link below, So you can run your own code.
-You can get the full installation here of both TinyOSs
and also the native .java files.
-There's no need for SerialForwader application installation.
Now, It is the time to talk with the mote.
-The TestSerial Application uses the packet number "0x44".
and I am sure you are asking about the Header, We now don't care about the payload, as later you are going to change it based on what your application needs.
The Frame Header for Packet number 0x44 is like the below:
Native C code(Not fully tested, I do not gurantee that it's reliable):
//========================================
(Start of code)
#include stdio.h
#include conio.h
#include stdlib.h
#include windows.h
typedef unsigned char oneByte;
typedef unsigned short twoBytes;
#include stdio.h
#include conio.h
#include stdlib.h
#include windows.h
typedef unsigned char oneByte;
typedef unsigned short twoBytes;
#define LOBYTE(w) ((oneByte)(((long)(w)) & 0xff))
#define HIBYTE(w) ((oneByte)((((long)(w)) >> 8) & 0xff))
#define PACKET_BYTE_ESCAPE 0x7d
#define PACKET_BYTE_SYNC 0x7e
// Escape.
#define PacketNeedsEscape(bData) (bData==PACKET_BYTE_SYNC || bData==PACKET_BYTE_ESCAPE)
oneByte tempBuffer[50];
oneByte finalBuffer[50];
twoBytes myCRC=0;
//Header Set.
int index=0;
tempBuffer[index++]=126; //Sync. Byte.
tempBuffer[index++]=68; //Packet Type.
tempBuffer[index++]=SeqNumber++; //sequence number. Initially is Zero, Incremented for each packet, range from 0 to 255.
tempBuffer[index++]=0; //Packet format Dispatch. Currently is Zero.
tempBuffer[index++]=LOBYTE(DestAddress); //Dest. Address.
tempBuffer[index++]=HIBYTE(DestAddress); //Dest. Address.
tempBuffer[index++]=LOBYTE(SrcAddress); //Source Address.
tempBuffer[index++]=HIBYTE(SrcAddress); //Source Address.
tempBuffer[index++]=PayloadLength; //Data Length.
tempBuffer[index++]=GroupID; //Group ID.
tempBuffer[index++]=amType; //AM Type.
//========================================
// Just add two Bytes holding a integer number to tempBuffer, which will be represented in Binary on the leds.
// Check for special Bytes Algorithm, and assigning the values to finalBuffer array.counter=1;
for(int i=1;i<50;i++)
{ if(PacketNeedsEscape(tempBuffer[i]))
{ finalBuffer[counter]=PACKET_BYTE_ESCAPE;
counter++; finalBuffer[counter]=(tempBuffer[i] ^ 0x20); //X-ORing with 0x20
counter++;
}
else
{
finalBuffer[counter]=tempBuffer[i];
counter++;
}
}
//Calculate the CRC, add it to the Byte array, It takes two Bytes.
//Here's my algorithm for calculation a valid CRC:
//CRC calculation Algorithm.
for(int i=1;i<11+PayloadLength;i++)
{
if(finalBuffer[i]==0x7d)
{
myCRC=calculateCRC(myCRC,0x7e);
i++;
}
else
myCRC=calculateCRC(myCRC,finalBuffer[i]); }
//Do not forget to add the Last Sync. Byte.
(End of code)
-------------------------------------------------------------------------------------------
Then you are ready to write on the USB port.
Using any programming language, Just write this array(tempBuffer) to the buffer.
-Your port should be look like this on Windows (COMX)
Such that X has a range from 1 to a higher.
For Linux, Please refer to how to write and read from USB Port.
-You should now watch your Leds representing the Number you have already sent.
Congratulations!
------------------------------------------------------------------------------------------------------
Troubleshooting Problems:
-Writing on the port but nothing happens?
Possible Solutions:
-Set the valid Baudrate which is 115200 for Telos motes, for other types you can get the values from the table included in their tutorials.
-The amType value should matches the one which is included in the Makefile of the .nc Files of the TestSerial Application.
-Did you calculate the CRC well?
-Also do not forget to declare the Byte array you are writing to the port as a signed one.
-Do not send lots of packets after each others, Your eyes may not watch the leds toggling in a normal sequence as they do it rapidly, so add a time consuming line between every packet you are sending, but do not forget to comment it after debugging.
-Example:
-So now, Write your own code, send your payload as you need, Then write another nesC code which matches the code on the PC.
-Your .nc Code should have a statement that toggles any led, so you can debug easily.
-Copy the compiled .nc code to the mote using Linux.
-Write on the Buffer as we did, watch the leds.
Did it act as expected?
then well done :)
It is a good feeling when blind hardware acts as you told it exactly, Really I enjoyed it.
Never hesitate to contact me for help here: abdulrahman.yasser@ieee.org
Thanks for reading!
No comments:
Post a Comment