Reading bytes from the Javelin stamp through serial port
coolbebe
Posts: 9
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;
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
the data to the pc.
regards peter
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
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:
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