Need help with Java code: Receiving a Byte of Serial Data ...
JMLStamp2p
Posts: 259
·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());
··· }
· }
·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
· 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
JMLStamp2p ...