Go Down

Topic: Arduino recieving hexadecimal data (Read 2 times) previous topic - next topic

AWOL

What is the last index of command?
"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.

Thyvo


AWOL

"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.

Thyvo


And how big is command?


command is 6 big, (maybe I can set it to 5 and remove command[6] = '\0';)

PaulS

#19
Dec 28, 2011, 05:33 pm Last Edit: Dec 28, 2011, 05:41 pm by PaulS Reason: 1
Code: [Select]
  strings[0] = strtok(command, " ");
  strings[1] = strtok(NULL, " ");

should become:
Code: [Select]
  strings[0] = strtok(command, " ");
  strings[1] = strtok(NULL, " ");
  Serial.print("Code: ");
  Serial.println(strings[0]);
  Serial.print("Value: ");
  Serial.println(strings[1]);

so you can see what was received and parsed.

Quote
command is 6 big, (maybe I can set it to 5 and remove command[6] = '\0'

Absolutely, unequivocally, no! command had been 10 elements. Why did you change it?

Code: [Select]
  led = atoi(strings[0]);
  power = strtol(strings[1],&pEnd,16);
  Serial.println(strings[1]); //just for debugging

It would make a lot more sense to print led and power!

Thyvo


Code: [Select]
  strings[0] = strtok(command, " ");
  strings[1] = strtok(NULL, " ");

should become:
Code: [Select]
  strings[0] = strtok(command, " ");
  strings[1] = strtok(NULL, " ");
  Serial.print("Code: ");
  Serial.println(strings[0]);
  Serial.print("Value: ");
  Serial.println(strings[1]);

so you can see what was received and parsed.

Quote
command is 6 big, (maybe I can set it to 5 and remove command[6] = '\0'

Absolutely, unequivocally, no! command had been 10 elements. Why did you change it?

Code: [Select]
  led = atoi(strings[0]);
  power = strtol(strings[1],&pEnd,16);
  Serial.println(strings[1]); //just for debugging

It would make a lot more sense to print led and power!


I did what you said, and I found out by setting the serial monitor to no line endings, alwase responds

basically everything on the arduino side works now, grate, thanks for the help and information, I learned a few new things, thanks :D

now I can start programming my software in java for it, still need to figure out how that works

el_supremo

Your main problem is that you aren't reading and discarding the linefeed at the end of each line. You also pass a string address to strtol but don't use it - better to pass NULL.

Use this at the beginning of loop():
Code: [Select]

    if (Serial.available() >= 6 ) { // wait for 6 characters
        for (int i=0; i < 5; i++)
            command[i] = Serial.read();
        command[5] = '\0';
        // Throw away the \n
        Serial.read();


        strings[0] = strtok(command, " ");
        strings[1] = strtok(NULL, " ");

        delay(10);
        led = atoi(strings[0]);
        power = strtol(strings[1],NULL,16);


It waits for 6 chars, stores the first 5 and then throws away the sixth one (the linefeeed).
This code is still going to blow up if you don't enter exactly 5 chars plus linefeed.
If you are always going to enter something of the form "03 7F", you don't need strtok at all. The "led" value is always at &command[0] and the power value is at &command[3].

Pete

GoForSmoke

Murphy was an optimist, some of you guys could learn from that. Where do you check for errors? Serial transmission has no guarantees.

If it can go wrong, it will go wrong at the worst possible moment.


I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up