Go Down

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

#### AWOL

#15
##### Dec 28, 2011, 05:09 pm
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

#16
##### Dec 28, 2011, 05:11 pm

What is the last index of command?

Code: [Select]
`command[6] = '\0';`

#### AWOL

#17
##### Dec 28, 2011, 05:14 pm
And how big is 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

#18
##### Dec 28, 2011, 05:17 pm

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 pmLast 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

#20
##### Dec 28, 2011, 05:52 pm

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

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

#### el_supremo

#21
##### Dec 28, 2011, 05:55 pm
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
Don't send me technical questions via Private Message.

#### GoForSmoke

#22
##### Dec 28, 2011, 06:38 pm
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.

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up

Please enter a valid email to subscribe