Pages: [1]   Go Down
Author Topic: Help converting serial commands to a usefull string.  (Read 709 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
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
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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


Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if(incomming = true){

That assigns, not compares. You want:

Code:
    if(incomming == true){

And preferably:

Code:
    if(incoming == true){

Or even, since testing for true is redundant:

Code:
    if (incoming) {

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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:
    if(incoming = true){
      if(index < 19) // One less than the size of the array
      {
        inChar = Serial1.read(); // Read a character

within the statement:

Code:
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!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: