Shop OBEX P1 Docs P2 Docs Learn Events
UART buffering with hyperterminal — Parallax Forums

UART buffering with hyperterminal

blazenblazen Posts: 1
edited 2006-03-15 03:28 in General Discussion
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?



·

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-03-15 03:28
    Check what you receive
    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
    ·
Sign In or Register to comment.