Go Down

Topic: if statements and bi-directional bytes (Read 1 time) previous topic - next topic

icek

I am having programming difficulty with trying to send bytes from the Arduino when certain bytes are received over serial.  For example:

- When byte 254 is received send 5 hard coded bytes

The code looks like:

Code: [Select]
void loop() {
 if (Serial.available() > 0)  // Wait for serial input
   // Read the first byte
   startbyte = Serial.read();
   // If it's really the startbyte (254) ...
   if (startbyte == 254)  {
     Serial.write(254);
     Serial.write(123);
     Serial.write(123);
     Serial.write(1);
     Serial.write(255);}


When I run the code and look at the console I get -1 being sent instead of the hard coded bytes.  Any idea why (or better yet how to write the code properly)?

RobotGuy


icek

I tried that as well, but it didn't work.  The Serial.write command will print in byte format and I have had success with it before.  I think the If statement is off since I can print the bytes without the if statement (of course I need the if statement so I can respond when receiving the specific byte only).

lloyddean

Reading the documentation on the "Serial" objects "read" message (it's a C++ object) I see that "read" returns -1 if nothing was read, else, it returns the character read.

This implies, to me at least, that the "serial" object only handles ASCII values.  ASCII values are 7-bits representing the values 0-127 thus you'll likely never receive the value 254.

icek

Thanks, that looks like a good find.  I will see if changing the code to reading 0 will make a difference.

Groove

Quote
This implies, to me at least, that the "serial" object only handles ASCII values.


This implies nothing of the sort.
The return type of "Serial.read" is "int".
If the serial receive buffer is empty -1 (0xFFFF) is returned, otherwise, the least-significant eight bits contain the least-recent byte received.
If that byte was 254, then 0x00FE is returned.

You should perhaps be looking at what is being transmitted.
Per Arduino ad Astra

lloyddean

#6
Jun 04, 2010, 08:51 am Last Edit: Jun 04, 2010, 08:52 am by lloyddean Reason: 1
I was about to say that after looking at the source for the 'Serial' object I now no longer think the return value, an 'int', is limited to 7-bits.  But someone has chastised me already before this post.

Your code fragment doesn't seem to show the declaration of 'startbyte'.  What is it declared as?

icek

Here is the whole code with the declaration.  I also tried byte startbyte rather than int startbyte, but it didn't make a difference.

Code: [Select]

byte startbyte;       // start byte, begin reading input


void setup() {
 Serial.begin(9600);  
}

void loop() {
   if (Serial.available() > 0)  // Wait for serial input
   // Read the first byte
      startbyte = Serial.read();
   // If it's really the startbyte (254) ...
    if (startbyte == 254)  {
     Serial.write(254);
     Serial.write(123);
     Serial.write(123);
     Serial.write(1);
     Serial.write(255); }
   if (startbyte == 255) {
     Serial.write (255);
 }
}

icek

Well, I finally made it work by giving the Arduino 2 full seconds before receiving the first startbyte (I tried 100 milliseconds, but it didn't work).  So it turns out it was a timing issue.  I wasn't able to cycle through the second if statement for some reason, but that probably has to do with syntax such as using "else".  And the 254 worked fine.  Thank you for everyone's help!

jrraines

I think in your definition of startbyte you want to initialize it. I'm not sure there's a guarantee that it isn't born with a value 254;

AWOL

If startbyte has global or static scope, it will be initialised to zero.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up