Shop OBEX P1 Docs P2 Docs Learn Events
Reading bytes from the Javelin stamp through serial port — Parallax Forums

Reading bytes from the Javelin stamp through serial port

coolbebecoolbebe Posts: 9
edited 2006-04-01 19:32 in General Discussion
I'm trying to read/recieve data from an RFID tag using the microcontroller's serial port in java using javax.comm.

this is a snippet of how im getting the byte array...but when I want to change it to char or string..it just gives me the string represetation of the raw bytes.
Please, if anyone knows if the bytes need different kinds of processing before I get them to the format that I want(String or char), let me know.
I was able to read the number using the javelin's IDE Uart class, but I need to do more work with the info I read from the RFID reader , so I have to do the reading myself using java.

Thanks for any help

Note: u can also see the kinds of outputs im getting at this address:
http://ahmadfarhat.googlepages.com/home


···· case SerialPortEvent.DATA_AVAILABLE:
··········· byte[noparse]/noparse readBuffer = new byte[noparse][[/noparse]10]; // the·array that stores the bytes from the serial port
·· int bytesRead = 0;
··
··········
·····
·· try {
··· while ( (bytesRead = inputStream.read(readBuffer)) != -1 )· {


···· //System.out.println("10 digit number: " + new String(readBuffer));
···· System.out.println("10 digit number: " + Arrays.toString(readBuffer));
···· //System.out.println("10 digit number: " + readbuffer(0));
··· }



·· } catch (IOException ioex) {
··· System.out.println ("error reading bytes from input stream");···
·· }
·· break;

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-01 18:40
    Please post the javelin code that you use to send
    the data to the pc.

    regards peter
  • coolbebecoolbebe Posts: 9
    edited 2006-04-01 19:07
    This is the only code i'm using which is to recieve the tag number from the reader. I program the javelin than i try to use java to read the tag . I don't want to use javelin IDE because i want to send the tag number to a database and javelin does not have this feature.here is the code:

    import stamp.core.*;
    import stamp.util.*;

    public class Reader1
    {
    ··· static int index = 0;

    ··· final static int rxPin1 = CPU.pin1; // Pin for serial from reader 1
    ··· static Uart rxUart1 = new Uart(Uart.dirReceive, rxPin1, Uart.dontInvert,
    ·································· Uart.speed2400, Uart.stop1);
    ··· static StringBuffer buffer = new StringBuffer(128);
    ··· static char c;


    · public static void main() {
    ·· CPU.writePin(CPU.pin0,false);//Enable Reader 1

    ·· while(true) { //loop forever
    ····· //wait for '\n'
    ····· while(true)· {
    ········ if ( rxUart1.byteAvailable())
    ········ {
    ··········· c=(char)rxUart1.receiveByte();
    ··········· if (c=='\n') break;
    ········ }
    ········· }
    ····· //read 10 bytes and display
    ····· index=0;
    ····· while (index<10) {
    ······· if (rxUart1.byteAvailable()) {
    ·········· c = (char)rxUart1.receiveByte();
    ·········· System.out.print (c);
    ·········· index++;
    ······· }
    ····· }
    ····· System.out.print('\n');
    ···· } //end loop forever
    ·· }
    ·}

    Thanks
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-04-01 19:32
    If I understand correctly, you read the 10 digit tag number with the javelin,
    then send these 10 digits to the pc for further processing.
    You use the javelin programming port for sending to the pc (28800 baud, 8N1).
    The javelin programming port has a special output format.

    Debug messages sent from the Javelin conform to the following pattern:

    0x05 0x03 <messagedata> 0x7E

    <messagedata> is the sequence of bytes being sent by the CPU.message() native method. It is byte-stuffed. This means that if <messagedata> contains a byte <b> that has the value 0x7F or 0x7E then that byte is replaced with 0x7F <b>^0x20. i.e. 0x7F followed by the orginal byte xored with 0x20.

    Terminal characters are sent to the Javelin using the following algorithm:
    1. When it is ready to receive a byte (the Terminal.getByte() native method is called) the Javelin transmits 0x11 out Sout.
    2. If the terminal has no data to send to the Javelin it replies with 0x13.
    3. If the terminal has a byte <b> to send, then it replies with 0x12 <b>.

    After the Javelin is reset it send 0x0F followed a pause followed by 0x50 (the version of the Javelin firmware).

    Running the following simple program:

    public class HelloWorld { public static void main() { System.out.println("Hello World"); } }

    will generate the following on Sout:

    0F 50 05 03 48 65 6C 6C 6F 20 57 6F 72 6C 64 7E 05 03 0D 0A 7E 05 05 7E

    Note that the sequence 0x05 0x05 0x7E indicates that the Java program has ended.


    In your case, you use
    System.out.print (c); for the 10 digits and then
    System.out.print('\n');

    So the output looks like
    05 03 <d0> 7E 05 03 <d1> 7E 05 03 <d2> 7E 05 03 <d3> 7E 05 03 <d4> 7E
    05 03 <d5> 7E 05 03 <d6> 7E 05 03 <d7> 7E 05 03 <d8> 7E 05 03 <d9> 7E
    05 03 0A 7E

    In your pc java code,
    i=0;
    while (i<11) {
    · waitfor(0x05);
    · waitfor(0x03);
    · digit[noparse][[/noparse]i++] = receivebyte(); //
    · waitfor(0x7E)
    · if (digit[noparse][[/noparse]i-1]==0x0A) break;
    }
    if (i==11) return true; //complete number received
    else return false; //partial number received;

    at that point you have your 10 digit number + closing '\n' in array digit

    regards peter
Sign In or Register to comment.