Jump to content
43oh

ReadASCIIString


Recommended Posts

Hi --

 

I have been trying to use Serial.parseInt() -- but can't get it to work.  It appears to read on digit only.  

 

So, I thought, try an example.  However, ReadASCIIString does;t work, either, at least in my hands :-(

 

Using Tiva Launchpad.  Any advice welcome.  

 

Thanks,

David

 

Link to post
Share on other sites

yes, i have the same proble i can't use parseInt or parseFloat, i did an aplication with bluethoot and rbg, and i need to read the values of my cell phone, but i need to read only whit parseInt. it didn't works.

 

dpharris how i can do? the problem is to i have an arduino and the same program works, i think to should give support these functions.

Link to post
Share on other sites

i was reviewing this program:

void setup()
{
  Serial.begin(9600);
  pinMode(RED_LED, OUTPUT);
  digitalWrite(RED_LED, HIGH);
}

void loop()
{
  while( Serial.available() > 0 )
  {
    byte inc = Serial.parseInt();
      Serial.println(inc);
  }
}

and when i used for the first time, and send a number from one to ten, in the serial monitor appears the correct number, example, if
you send 9,0,0 in the serial monitor appears 9 but the others not appear, and you need to press reset if you want to do once again.
i think to in some library the declaration of the variables are wrong, and not return the correct value. try to run the example and see what happens please
im not a good programer but is what i found

Link to post
Share on other sites

Thank you for reporting this issue! This was a very fundamental bug in HardwareSerial. 

1: peek() would loop forever if the RX buffer was empty.

2: read() would increase the buffer read pointer even if no byte was read because the buffer was empty.

 

Both of these issues have been fixed and pushed up to github:

Patch: https://github.com/energia/Energia/commit/b95f5f346a320bf8d06f4d4281ec31d0d62f9cff

 

Easiest way to fix this in your installation is:

1: Download the patched file from https://raw.githubusercontent.com/energia/Energia/master/hardware/lm4f/cores/lm4f/HardwareSerial.cpp

2: In your Energia folder browse to hardware/lm4f/cores/lm4f

3: Rename HardwareSerial.cpp to HardwareSerial.org

4: Copy the file you downloaded under 1 to this folder.

5: Restart Energia.

 

Thanks again for the report!

 

Robert

Link to post
Share on other sites

i did the modifications that you told me, and then i loaded the SerialASCIIString but it didn't work, i think that when you its used Serial.read inside the funtion of Serial.available() it doesn't work. i think that the funtion Serial.read, is not recognizing '\n'. because i did this program excluding '\n', and it worked. and in the serial monitor appears the correct value and the RGB led change its color,

 

const int redPin = RED_LED;
const int greenPin = GREEN_LED;
const int bluePin = BLUE_LED;

void setup() {
 Serial.begin(9600);
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 pinMode(bluePin, OUTPUT);

}

void loop() {
while (Serial.available() > 0) {
  int red = Serial.parseInt();
  int green = Serial.parseInt();
  int blue = Serial.parseInt();
  // look for the newline. That's the end of your
  // sentence:
  if (Serial.read() == '\n') {
    Serial.println("finish");
  }

  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);

  Serial.print(red, HEX);
  Serial.print(green, HEX);
  Serial.println(blue, HEX);
 }
}

 

 i don't know whats happening

 

// pins for the LEDs:
const int redPin = RED_LED;
const int greenPin = GREEN_LED;
const int bluePin = BLUE_LED;

void setup() {
 // initialize serial:
 Serial.begin(9600);
 // make the pins outputs:
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 pinMode(bluePin, OUTPUT);

}

 

 

 

example of SerialASCIIString

void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {

  // look for the next valid integer in the incoming serial stream:
  int red = Serial.parseInt();
  // do it again:
  int green = Serial.parseInt();
  // do it again:
  int blue = Serial.parseInt();

  // look for the newline. That's the end of your
  // sentence:

  if (Serial.read() == '\n') {
  // constrain the values to 0 - 255
  // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
  red = constrain(red, 0, 255);
  green = constrain(green, 0, 255);
  blue = constrain(blue, 0, 255);

  // fade the red, green, and blue legs of the LED:
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);

  // print the three numbers in one string as hexadecimal:
  Serial.print(red, HEX);
  Serial.print(green, HEX);
  Serial.println(blue, HEX);
  }

 }
}

 in the example you need to get '\n' but because it doesn't detect '\n' it never execute the if condition

Link to post
Share on other sites

In the Energia Serial monitor there is a little drop down box left of the baudrate setting drop down box. Make sure that that is set to "Newline". By default it is set to "No line ending" which means that it does not send '\n'. When you set it to "Newline" it will send the '\n' and you should be able to run the Sketch without problems.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...