teensy 3.1 serial.read

shaunmorgan

New member
So if I use this code and type in to the serial window 2 numbers or 4 numbers the code seems to work just fine. Once I enter an odd number of numbers, it will return a -1 for the number I did not enter. Now what happens is the device continues to print double negative 1's as if I had entered numbers in a loop. Even if I try enabling the serial.end(); The serial does not stop and it continues to print double -1's. Tested this on a teensy 2++ and it does not act like I entered these extra -1's.

Just tested this using char for the incoming values and I get
You entered: ÿ
You entered: ÿ
You entered: ÿ
You entered: ÿ
You entered: ÿ
You entered: ÿ
You entered: ÿ
You entered: ÿ
You entered: ÿ
You entered: ÿ

Any ideas?

code


int8_t led =13;
int8_t led1=23;
int8_t led2=22;
int8_t led3=21;
int8_t incomingByte;
int8_t incomingByte2;


void setup()
{
pinMode (13,OUTPUT);
pinMode (led1,OUTPUT);
pinMode (led2,OUTPUT);
pinMode (led3,OUTPUT);
Serial.begin(9600);
}

void loop()
{
if (Serial.available() > 0 ) //then chars are in the serial buffer
{
incomingByte = Serial.read();
Serial.print("You entered: ");
Serial.println(incomingByte);
incomingByte2 = Serial.read();
Serial.print("You entered: ");
Serial.println(incomingByte2);
// Serial.end();
delay (2000);
}
}
 
Last edited:
I ran this code on a Teensy 3.1. I was unable to reproduce the problem. Here's what I see after typing "abc" and clicking Send:

abc.png
 
It might be worth mentioning I never saw "You entered: ÿ". It prints "-1", not "ÿ".

Can you check which software you're using. In Arduino, use Help > About.
 
It's probably a timing problem.
This statement:
Code:
if (Serial.available() > 0 ) //then chars are in the serial buffer
only guarantees you that there is at least one character available but you are reading two inside the if statement.
There are various ways around it. The best is to read the chars one at a time accumulating a string and then interpret the string once you reach the end of line character.
Another, not as nice, way is to use:
Code:
if (Serial.available() >= 2 ) //then at least two chars are in the serial buffer
The problem with this second method is that you need to enter strings which are a multiple of 2 chars long (including the end of line characters) otherwise it will hang at the end of the line waiting for you to type at least one more.

Pete
 
It appears like a serial buffer read problem

Ok so i changed something and i have new info to offer about the problem. So i changed the code to offer 2 of the if (Serial.available() > 0 ) peices to see what would happen. Seemed the code ran better untill i realized another problem. Upon loading the code in to the teensy i directly went to the serial window and entered 12345 and it spit out 12345 in char form no problem , "posted below" you will see that it automatically skipped "you entered1" as if it received something before i even punched anything in to the window. So i re uploaded and tested this a few times and sometimes it would not jump to the "you entered2" and other times it would. Makes me wonder what it was trying to read, that is supposedly there. So i tried placing a serial.read command up where the serial.begin is to see if maybe i needed to clear something being stored previously during the upload of the code and before the program ran. Made no difference. I am using arduino 1.0.5-r2 teensyduino 1.18.
Yes i tried a couple capacitors across the power input.

void loop()
{
if (Serial.available() > 0 ) //then chars are in the serial buffer
{
incomingByte = Serial.read();
Serial.print("You entered1: ");
Serial.println(incomingByte);
}
if (Serial.available() > 0 )
{

incomingByte2 = Serial.read();
Serial.print("You entered2: ");
Serial.println(incomingByte2);
// Serial.end();
delay (2000);

You entered2: 49
You entered1: 50
You entered2: 51
You entered1: 52
You entered2: 53
 
hmm

ok so i changed the code around to give me some kind of answer to what is happening. Unfortunately it does not tell me much.

if i use if (Serial.available() > 1 ) it seems to happen less frequently but still happens.
So the code changes made were to do a serial.read() and assign it to an int x=0; before i ever enter the loop. It is then printed in the loop. It shows -1 is in the serial at all times which does not lead me to understand that somehow it is allowing -1 past the > 0 term, How i don't know, maybe i am missing something but thats all i can figure at this time. Will keep working on it and post a resolution if I find one.


You entered2: 49
this is what is in the serial before loop ran-1
You entered1: 50
You entered2: 51
this is what is in the serial before loop ran-1
You entered1: 52
You entered2: 53
this is what is in the serial before loop ran-1
 
I'm no expert, but I have worked on Arduino serial for some time now. I have been working on updating a TouchScreen that works at 57600 and sending out at 9600, so it is 1 in every 6th sent.
The issue I had was reading all that data from the serial port into a buffer. I did all sorts of tests, but what I ended up doing is doing it when the buffer was <=6. The serial from the touchscreen was sending 6 bytes and I know what to look for. The ending part of the code will never be the same.

Here is a snippet:

if(Serial2.available() > 6) {
//delay(60);

for(i =0; i <=6;i++) // Fill buffer with TS bytes Buffer max is 63 but reading only 6 @ a time
{
buffer = Serial2.read(); // read bytes from serial buffer
} // At this point, buffer has bytes in locations 0-6

if((buffer[1] == 0x01) && (buffer[2] == 0x00) || (buffer[2] == 0x01)) // test Report ID = 0x01 and SW = 0x00/1
{
good_data(); // test was good and off to good_data
}
else
/*
{
bad stuff, do not use
}
*/

r++; // lets clear the buffer routine.. does it work? is it needed now ?
if(r>5) {
for(dumpbuffer=1;dumpbuffer<63;dumpbuffer++){
Serial2.read();
r=0;
}
}
}

I 1st filled the buffer[] with 6 pieces of data.
But I will only work with those six, the others beyond I do not care.
If you look, I do not re-read the buffer, I instead dumped it if greater that 5 passes.
But, I do not look at the buffer as a loop or if statement. I directly read it as buffer[1], buffer[2] etc. When I did the loop (for) or if.. the buffer always was junk.
My tests are in HEX since the serial from the touchscreen is hex.

I'm thinking that your reading your serial twice and loosing what was in the serial buffer on the 2nd go around.
Also, you might want to check your Serial.print(xxx, DEC or HEX...);

I'm getting my Teensy's tomorrow, so I can try to see what I see..
 
Hi, your code is working right (was driving home the light bulb lit)

It is returning ASCII code:
You entered2: 49 = 1
You entered1: 50 = 2
You entered2: 51 = 3
etc..

The buffer holds 0 to 63 bytes (64) If I recall, the 1st location (0) is always -1.. The real byte location starts at (1). A byte is from 0 to 255 in ascii.. So the 49 ASCII is what you typed on the serial monitor on the Arduino IDE serial It is always ASCII

You need to test how many bytes are in the buffer.. use the Serial.available as length = Serial.available(); then use the length in a loop to fill a buffer buffer[length].

If you are only working with number, do a print(buffer[whatever length is]- 48); 49 - 48 = 1

I think this is it what your trying to do..
 
Hi, your code is working right (was driving home the light bulb lit)

It is returning ASCII code:
You entered2: 49 = 1
You entered1: 50 = 2
You entered2: 51 = 3
etc..

The buffer holds 0 to 63 bytes (64) If I recall, the 1st location (0) is always -1.. The real byte location starts at (1). A byte is from 0 to 255 in ascii.. So the 49 ASCII is what you typed on the serial monitor on the Arduino IDE serial It is always ASCII

You need to test how many bytes are in the buffer.. use the Serial.available as length = Serial.available(); then use the length in a loop to fill a buffer buffer[length].

If you are only working with number, do a print(buffer[whatever length is]- 48); 49 - 48 = 1

I think this is it what your trying to do..

What the problem is that i uploaded the code, went to the window , typed in three numbers, then it spit out "you entered2", this should have been read with the first read " you entered1". So it is like it skipped over the first read when nothing was entered by me. Then it would keep running in looping fashion when i never entered anything, but it would act like i entered -1 in the box and pressed enter repeatedly.
You entered2: 49
You entered1: 50
You entered2: 51
You entered1: 52
You entered2: 53
Apparently the solution was in the teensy code or arduino code. I updated to version 6 and newest teensyduino and it seems to have disappeared.
 
I'm no expert, but I have worked on Arduino serial for some time now. I have been working on updating a TouchScreen that works at 57600 and sending out at 9600, so it is 1 in every 6th sent.
The issue I had was reading all that data from the serial port into a buffer. I did all sorts of tests, but what I ended up doing is doing it when the buffer was <=6. The serial from the touchscreen was sending 6 bytes and I know what to look for. The ending part of the code will never be the same.

Here is a snippet:

if(Serial2.available() > 6) {
//delay(60);

for(i =0; i <=6;i++) // Fill buffer with TS bytes Buffer max is 63 but reading only 6 @ a time
{
buffer = Serial2.read(); // read bytes from serial buffer
} // At this point, buffer has bytes in locations 0-6

if((buffer[1] == 0x01) && (buffer[2] == 0x00) || (buffer[2] == 0x01)) // test Report ID = 0x01 and SW = 0x00/1
{
good_data(); // test was good and off to good_data
}
else
/*
{
bad stuff, do not use
}
*/

r++; // lets clear the buffer routine.. does it work? is it needed now ?
if(r>5) {
for(dumpbuffer=1;dumpbuffer<63;dumpbuffer++){
Serial2.read();
r=0;
}
}
}

I 1st filled the buffer[] with 6 pieces of data.
But I will only work with those six, the others beyond I do not care.
If you look, I do not re-read the buffer, I instead dumped it if greater that 5 passes.
But, I do not look at the buffer as a loop or if statement. I directly read it as buffer[1], buffer[2] etc. When I did the loop (for) or if.. the buffer always was junk.
My tests are in HEX since the serial from the touchscreen is hex.

I'm thinking that your reading your serial twice and loosing what was in the serial buffer on the 2nd go around.
Also, you might want to check your Serial.print(xxx, DEC or HEX...);

I'm getting my Teensy's tomorrow, so I can try to see what I see..


The first "for loop" in the snippet has an "off by one" error. It will loop 7 times, not 6, reading Serial2 once more than intended. I believe the test part of the for expression should be i < 6 rather than i <= 6.
 
Believe it or not, it does not work with it @ < 6... If I print the available it reports 7, that because (I think) the available > 6 adds the one more..

I tried all sorts of scenarios and this works fine.. Both on the Teensy 3.1 and the 2560
Go figure
 
Back
Top