Hi Peter,

I think you have at-least two issues.

First, the SF uses a handshake to determine that there is another SF on the other side of the socket. You are probably receiving a 'T' then a ' ', then SF is waiting for you to respond. The following code implements the handshake:

                boolean validSF = false;
                try {
                        // Make sure we have a valid Serial Forwarder service
                        logger.info("Testing SF version");
                        ByteBuffer buffer = ByteBuffer.allocate(10);
                        int platform = 1;
                        byte[] check = new byte[20];
                        byte[] nonce = new byte[6];


                        int numRead = 0;
                        int count = 5;

                        selector.select(3000);
                        numRead = channel.read(buffer);

                        if( numRead < 2 ){
logger.error("No response from Serial Forwarder");
                        } else {
                                buffer.flip();
                                buffer.get(check, 0, numRead);
                                if(check[0] == 'X'){
                                        validSF = true;
                                        nonce[0] = 'X';
                                nonce[1] = ' ';
} else if (check[0] == 'T' && check[1] >= ' '){
                                        validSF = true;
                                        nonce[0] = 'T';
                                nonce[1] = '!';
                                } else {
logger.error("Could not verify Serial Forwarder");
                                }
                                if( validSF == true ){
                                nonce[2] = (byte) (platform       & 0xff);
                                nonce[3] = (byte) (platform >> 8  & 0xff);
                                nonce[4] = (byte) (platform >> 16 & 0xff);
                                nonce[5] = (byte) (platform >> 24 & 0xff);
                                buffer.flip();
                                buffer.put(nonce, 0 , 6);
                                buffer.flip();
                                channel.write(buffer);
                                }
                        }

                } catch (IOException e){
logger.error("Exception testing Serial Forwarder: " + e.getMessage());
                }

The second issue is that the SF protocol contains SYNC_BYTEs (0x7e) that bound messages and ESCAPE_BYTESs (0x7d) to allow escaped SYNC_BYTES in the message. You need to make sure that messages start with the SYNC byte and escaped bytes are unescaped before you parse the message. Look at net.tinyos.packet.packetizer and/or .../tools/src/sf/sfsource.c for more details.

Regards,

                                                             Chuck


At 10:32 AM 3/7/2006, Peter Burke wrote:
Hi all,
        I've seen a couple of people post similar questions re connecting
custom apps to SerialForwarder but I've had no luck finding answers as yet,
so here's yet another question on the use of SF.

My understanding of the SerialForwarder is that it can be used to stream
serial data (i.e. TOS messages) to any client sitting on the end of a TCP
connection. I'm writing a simple Java client that initially just connects to
SF, receives message packets and writes them to the console.

The problem I'm having is that I can connect to SF & initially receive data
(just 2 bytes) but then my client just sits there waiting for more data.
>From what I can see from the example apps (Oscope etc) there doesn't appear
to be anything special going on, so I'm at a loss. When I connect to the SF
through Telnet, I just see a 'T' character displayed. I've checked
SFProtocol.java but maybe I'm missing something.

I'd appreciate hearing from anyone else who's tried to hook-up to SF in
their own apps and any suggestions they might have.

I've included a small excerpt of my client code, together with the program
output.

// Output
Attempting to connect to localhost:9001
< CONNECTED TO SERIALFORWARDER >
< READ returned 2 bytes>

public void run(){
        // read from the stream
        int numBytesReturned =
inputStream.read(packet,numBytesRead,PACKET_SIZE - numBytesRead);
        System.err.println("< READ returned " + numBytesReturned + " >");

        // continue to read until we reach the end of the stream
        while(numBytesReturned != -1){

        // update the number of bytes read from the stream
        numBytesRead += numBytesReturned;

        // if we've read enough bytes to form a packet, decode its contents
        if(numBytesRead == PACKET_SIZE){
                for(int idx = 0; idx < packet.length; idx++){
                        System.out.print( Integer.toHexString(packet[idx] &
0xFF) + " " );
                }

                // reset counter
                numBytesRead = 0;
                // leave a gap to display the next packet
                System.out.println();
        }

        // read another set of bytes
        numBytesReturned = inputStream.read(packet,numBytesRead,PACKET_SIZE
- numBytesRead);

        }// end while(!EOS)
}// end run

Thanks in advance.


This email and any files attached are intended for the addressee and may
contain information of a confidential nature.  If you are not the intended
recipient, be aware that this email was sent to you in error and you should
not disclose, distribute, print, copy or make other use of this email or its
attachments.  Such actions, in fact, may be unlawful.  In compliance with
the various Regulations and Acts, General Dynamics UK Limited reserves the
right to monitor (and examine for viruses) all emails and email attachments,
both inbound and outbound.  Email communications and their attachments may
not be secure or error- or virus-free and the company does not accept
liability or responsibility for such matters or the consequences thereof.
Registered Office: 100 New Bridge Street, London EC4V 6JA.  Registered in
England and Wales No: 1911653.
_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to