1

I am using this source code for an application of reading serial data from an XBee connected to my Windows machine to the Raspberry Pi. The data being sent is the y position of my left and right thumb stick on my XBee controller, for the purpose of controlling a robot through a skid type drive (i.e. press left forward and leave right alone, the car should turn right). I am able to successfully read the Xbox controller coordinates using the Xinput API, and send the coordinates to my XCTU. I can see the correct data being sent to the XCTU console, however, when using this program (the demo_rx) http://www.teuniz.net/RS-232/ , and including a line to convert from the char to an int

int x = atoi ((char * ) buf);

the only data being output is a stream of 0's. When I don't convert it to an int, i.e. get rid of that previous line of code, the data output is a stream of random ASCII characters, which is fine, except when the y value is negative, then it becomes a standard character no matter where the control stick is in the negative axis.

I would like to figure out how to convert the data correctly, because I believe I am sending and receiving it correctly, but I don't think the the ASCII conversion is the right way to go.

EDIT: I figured out why atoi is returning 0, it's because the first character in the char buffer is an ASCII value not a number, which is why sometimes I would get actual numbers instead of just 0's.

  • atoi() expects strings with numbers (e.g. "123", "-456"). If you are giving it 'random' ASCII data then it's not going to work as expected. If there are letters in the buffer then atoi() will return 0. Ultimately the XBee is only the communications channel, you need to work out what data you are feeding in (the format of the co-ords from your controller) to the Tx side so you can decode correctly on the Rx side. You might want to add some detail around what data you are passing in. – KennetRunner Jul 21 '16 at 8:28

Your Answer

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

Browse other questions tagged or ask your own question.