1

enter image description hereUsing LabVIEW I am sending string of data through Xbee like,

  • u123
  • r64
  • r89
  • u34
  • r179

And my arduino is receiving this data. I want my arduino to read this data as a string and I want to extract numbers from these strings and feed it into two different servos. For example from u123 I want to extract 123 and feed it to servo1 and from r64 I want to extract 64 and feed it to servo2 and so on.

Arduino IDE Serial Monitor is showing nothing but Arduino's Xbee is receiving and sending data. Don't know why :(

  • It's not clear what you're asking. Is "Arduino IDE Serial Monitor is showing nothing..." an additional question or did my answer not address your question? What are you expecting to happen? What did you try to accomplish that? What happened instead? – JRobert Oct 30 '15 at 19:48
1

First collect all of the characters up to the new-line (`\n' or line-feed) into an array of characters, and put a zero byte after the last one.

Since It looks like all of the strings you need to parse consist of one a single letter followed by the value you want, value = atoi(inputArray[1]; will return the value as a 16-bit int. If it could be larger than 32767 but less than 65536 (probably not if you're driving a servo with it), then cast the value to unsigned.

If there might be an unknown number of characters (non-digits) preceeding the value, the C function size_t strcspn(const char *string, const char *cset) will return the length of the leading sub-string of string that does not contain characters in cset. So value = atoi(inputArray[strcspn(inputArray, "0123456789")]); will do the same thing, but calculating the array index of the first digit in the string.

(For completeness, note there is a companion function, size_t strspn(const char *str, const char *cset);, that returns the leading sub-string of 'string' consisting only of characters in cset).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.