Go Down

Topic: Help converting serial commands to a usefull string. (Read 935 times) previous topic - next topic

Loren

I used this example as a starting point to convert some serial data to a usable string:

http://arduino.cc/forum/index.php/topic,45629.0.html

and came up with this:

Code: [Select]

#include <UTFT.h>
#include <UTouch.h>

// Uncomment the next two lines for the Arduino Mega
UTFT        myGLCD(ITDB32S, 38,39,40,41);   // Remember to change the model parameter to suit your display module!
UTouch      myTouch(6,5,4,3,2);
extern uint8_t SmallFont[];

char inData[50]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
boolean incomming = false;

void setup()
{

  // Setup the LCD
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);

  myGLCD.clrScr();

  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRect(0, 0, 319, 13);
  myGLCD.setColor(64, 64, 64);
  myGLCD.fillRect(0, 226, 319, 240);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(255, 0, 0);
  myGLCD.print("* Loren's Operating System *", CENTER, 1);
  myGLCD.setBackColor(64, 64, 64);
  myGLCD.print("Arduino Status:  Setup", LEFT, 14);
  myGLCD.print("PI Status:  ", LEFT, 28);

  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("setup Complete");
  myGLCD.print("Arduino Status:  Ready", LEFT, 14);
}

void loop(){



  if (Serial1.available()>0){




    //Serial.println("Stuff Available");
    int inByte = Serial1.read();
    if(inByte == 94){
      Serial.println("Starting byte found");
      incomming = true;
    }

    if(inByte == 38){
      Serial.println("Ending byte found");
      String temp = inData;
      Serial.write(inData);
      myGLCD.print(temp, LEFT, 42);
      incomming = false;
      index = 0;
    }

    if(incomming = true){
      if(index < 19) // One less than the size of the array
      {
        inChar = Serial1.read(); // Read a character
        inData[index] = inChar; // Store it
        index++; // Increment where to write next
        inData[index] = '\0'; // Null terminate the string
      }




    }

  }

  if (Serial.available()){
    int inByte = Serial.read();
    Serial1.write(inByte);

  }

}






When I send the string
Quote
^Power&
through it I get this:

Code: [Select]
Starting byte found
Ending byte found
ÿÿÿÿÿÿ


I've tried every thing I can think of and find to convert "inData"  to a string, including  "String temp(inData)" and "String temp = inData"  even referencing inData directly.  What can I do to get a response that i can display on my tft from the serial command?

Thanks so much,

Loren

billroy

Code: [Select]

    if(incomming = true){
      if(index < 19) // One less than the size of the array
      {
        inChar = Serial1.read(); // Read a character


This code reads from Serial1 without checking Serial1.available() to see if any characters have arrived yet.  When you do that, Serial1.read() returns -1, which is why you see those y-umlaut characters.

Also, if it wouldn't be too much trouble, would you mind spelling "incoming" with the usual number of 'm's?

-br



Nick Gammon

Code: [Select]

    if(incomming = true){


That assigns, not compares. You want:

Code: [Select]

    if(incomming == true){


And preferably:

Code: [Select]

    if(incoming == true){


Or even, since testing for true is redundant:

Code: [Select]

    if (incoming) {


Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Loren


Code: [Select]

    if(incoming = true){
      if(index < 19) // One less than the size of the array
      {
        inChar = Serial1.read(); // Read a character


This code reads from Serial1 without checking Serial1.available() to see if any characters have arrived yet.  When you do that, Serial1.read() returns -1, which is why you see those y-umlaut characters.


I thought that containing this:
Code: [Select]

    if(incoming = true){
      if(index < 19) // One less than the size of the array
      {
        inChar = Serial1.read(); // Read a character


within the statement:

Code: [Select]
if (Serial1.available()>0){

found on line 45 would have already checked for the availability of Serial1.  After thinking this through a little further, has too much time passed by executing the other if statements?


Also, if it wouldn't be too much trouble, would you mind spelling "incoming" with the usual number of 'm's?

-br


Done!

Nick Gammon

Quote

... would have already checked for the availability of Serial1 ...


You've checked for one byte, so you better only read one without checking again.

http://www.gammon.com.au/serial
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

billroy

The first Serial1.available() properly guards the first Serial1.read().  But the code falls through and reads another character without ever checking Serial1.available() again.  If there was only one character available initially, you will be trying to read a second one before a second character is available.

The symptom when this happens is that Serial1.read() returns -1, which, when you print it, shows up as the y-umlaut character in your example output text.

-br

zoomkat

Very simple code that captures the individual characters into a useable string. I don't know what the requirements are for the tft display.

Code: [Select]

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up