UART buffering with hyperterminal
blazen
Posts: 1
Hello all,
I'm having some troubles importing data to make decisons with.
The program below is supposed to buffer data from hyperterminal and make a decison based on what is in the buffer.
When comparing the recived data to a string, the decison is made incorrectly.
The only thing I can figure out is that the buffered data contains and extra byte which I think is not allowing the data comparison to result correctly.
Here is the Program:
import stamp.core.*;
public class testSerial2 {
· final static int SERIAL_TX_PIN = CPU.pin10;· // transmitter
· final static int SERIAL_RX_PIN = CPU.pin9;· // receiver
· final static int light = CPU.pin4;
· static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, Uart.dontInvert,
······························· Uart.speed9600, Uart.stop1 );
· static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
······························· Uart.speed9600, Uart.stop1 );
· static StringBuffer buffer = new StringBuffer(5);
· static char c;
· static void bufferMessage(){
··· c = 0xff;
··· while (c != '\r'){
····· if(rxUart.byteAvailable()){
······· c = (char)rxUart.receiveByte();
······· buffer.append(c);
····· }
··· }
· }
· public static void main(){
· while (true) {
····· buffer.clear();
····· txUart.sendString("ENTER a command, the press enter: \n\r");
····· bufferMessage();
····· txUart.sendString("The COMMAND you sent was: \n\r");
····· txUart.sendString(buffer.toString());
····· txUart.sendString("\n\r");
· · if ( buffer.equals("on")) {
·········· lightsON();
·········· txUart.sendString("lights are on");
·········· txUart.sendString("\n> ");
·········· buffer.clear();
········································ }
····· if ( buffer.equals("off")) {
·········· lightsOFF();
·········· txUart.sendString("lights are off");
·········· txUart.sendString("\n> ");
·········· buffer.clear();
······························· }
···· else· {
······· txUart.sendString("Unknown command: ");
······· txUart.sendString(buffer.toString());
··········· }
···································· }
·············· }
· public static void lightsON(){
······· CPU.writePin(light,true);
······· return;
····························· }
· public static void lightsOFF(){
······· CPU.writePin(light,false);
······· return;
······························ }
}
Any suggestions?
·
I'm having some troubles importing data to make decisons with.
The program below is supposed to buffer data from hyperterminal and make a decison based on what is in the buffer.
When comparing the recived data to a string, the decison is made incorrectly.
The only thing I can figure out is that the buffered data contains and extra byte which I think is not allowing the data comparison to result correctly.
Here is the Program:
import stamp.core.*;
public class testSerial2 {
· final static int SERIAL_TX_PIN = CPU.pin10;· // transmitter
· final static int SERIAL_RX_PIN = CPU.pin9;· // receiver
· final static int light = CPU.pin4;
· static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, Uart.dontInvert,
······························· Uart.speed9600, Uart.stop1 );
· static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
······························· Uart.speed9600, Uart.stop1 );
· static StringBuffer buffer = new StringBuffer(5);
· static char c;
· static void bufferMessage(){
··· c = 0xff;
··· while (c != '\r'){
····· if(rxUart.byteAvailable()){
······· c = (char)rxUart.receiveByte();
······· buffer.append(c);
····· }
··· }
· }
· public static void main(){
· while (true) {
····· buffer.clear();
····· txUart.sendString("ENTER a command, the press enter: \n\r");
····· bufferMessage();
····· txUart.sendString("The COMMAND you sent was: \n\r");
····· txUart.sendString(buffer.toString());
····· txUart.sendString("\n\r");
· · if ( buffer.equals("on")) {
·········· lightsON();
·········· txUart.sendString("lights are on");
·········· txUart.sendString("\n> ");
·········· buffer.clear();
········································ }
····· if ( buffer.equals("off")) {
·········· lightsOFF();
·········· txUart.sendString("lights are off");
·········· txUart.sendString("\n> ");
·········· buffer.clear();
······························· }
···· else· {
······· txUart.sendString("Unknown command: ");
······· txUart.sendString(buffer.toString());
··········· }
···································· }
·············· }
· public static void lightsON(){
······· CPU.writePin(light,true);
······· return;
····························· }
· public static void lightsOFF(){
······· CPU.writePin(light,false);
······· return;
······························ }
}
Any suggestions?
·
Comments
static void bufferMessage(){
··· c = 0xff;
··· while (c != '\r'){
····· if(rxUart.byteAvailable()){
······· c = (char)rxUart.receiveByte();
········switch (c) {
········· case '\r': System.out.print("<CR>");
······················ break;
········· case '\n': System.out.print("<LF>");
······················ break;
········· default: System.out.print((char)c);
······· }
······· if (c!='\n') buffer.append(c); //appends also '\r' but skips '\n'
····· }
··· }
· }
My guess is that '\r' is appended to buffer.
So use if (buffer.equals("on\r"))
regards peter
·