Pages: 1 [2]   Go Down
Author Topic: Arduino recieving hexadecimal data  (Read 1484 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the last index of command?
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the last index of command?
Code:
command[6] = '\0';
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And how big is command?
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And how big is command?

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  strings[0] = strtok(command, " ");
   strings[1] = strtok(NULL, " ");
should become:
Code:
  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:
  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!
« Last Edit: December 28, 2011, 11:41:59 am by PaulS » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  strings[0] = strtok(command, " ");
   strings[1] = strtok(NULL, " ");
should become:
Code:
  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:
  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 smiley-grin

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

Offline Offline
Edison Member
*
Karma: 35
Posts: 1419
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    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
 
Logged

Where are the Nick Gammons of yesteryear?

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4019
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Examples can be found in your IDE.

Pages: 1 [2]   Go Up
Jump to: