Shop OBEX P1 Docs P2 Docs Learn Events
Need help with Java code: Receiving a Byte of Serial Data ... — Parallax Forums

Need help with Java code: Receiving a Byte of Serial Data ...

JMLStamp2pJMLStamp2p Posts: 259
edited 2005-09-24 22:18 in General Discussion
·Hello to all, I would like someone familiar with Java to look at this bit of code. I am new to Java and this is my first try at receiving data serially.
·I want to receive data only on pin (0), at 9600 baud using (1) stop Bit. I would like to store the byte in a varible called "speechCode", retreive·· that·incoming Byte and Print it to the screen. I do not want to invert the data ...
Thankyou for your help,
JMLStamp2p



· import stamp.core.*;
· public class Main_Receiver{
· final static int dirReceive = 0;
· final static int speed = 9600;
· final static boolean dontInvert = true;
· final static int stop1 = 0;
· final static int dataPin = CPU.pin0;
· static Uart rxUart = new Uart ( Uart.dirReceive, CPU.pin0, Uart.dontInvert, Uart.speed9600, Uart.stop1 );
· static StringBuffer buffer = new StringBuffer(128);
· static int SpeechCode;
· static void bufferMessage(){
··· SpeechCode = 0xff;
··· while (SpeechCode != '\r'){
····· if (rxUart.byteAvailable()){
······· SpeechCode = (int)rxUart.receiveByte();
······· buffer.append(SpeechCode);
····· }
··· }
· }
· public static void main(){
· System.out.println((int) rxUart.receiveByte());
··· }
· }

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-09-23 15:13
    You don't need to specify Uart constants in your main class.

    · import stamp.core.*;
    · public class Main_Receiver{
    //· final static int dirReceive = 0;
    //· final static int speed = 9600;
    //· final static boolean dontInvert = true;
    //· final static int stop1 = 0;
    · final static int dataPin = CPU.pin0;
    · static Uart rxUart = new Uart ( Uart.dirReceive, dataPin, Uart.dontInvert, Uart.speed9600, Uart.stop1 );
    · static StringBuffer buffer = new StringBuffer(128);
    · static int SpeechCode;

    · static void bufferMessage(){
    ··· SpeechCode = 0xff;
    ··· //treat \r as newline
    ··· while (SpeechCode != '\n'){
    ····· if (rxUart.byteAvailable()){
    ······· SpeechCode = (int)rxUart.receiveByte();
    ······· if (SpeechCode == '\r') SpeechCode = '\n'; //easier for printing
    ······· buffer.append(SpeechCode);
    ······· System.out.print(SpeechCode); //display incoming message
    ····· }
    ··· }
    · }
    · public static void main(){
    ·· ·while (true) { //receive and display ALL messages
    ····· buffer.clear(); //clear buffer
    ····· bufferMessage(); //receive and display one·incoming message
    ····}
    ··}

    }

    Code looks good. Changed a few lines.
    Regards peter
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-09-24 22:18
    Peter, thank you for your help. The support that Parallax gives makes working with your products a pleasure!
    JMLStamp2p ...
Sign In or Register to comment.