VEX TTL Port send serial Data through XBEE

I have successfully Connected an xbee wireless node to the serial port two on the vex controller. I am connected @ 38400. when I send data from vex to pc I can get literal data by using WriteSerialPort2(‘H’); for example. when I try to pass a variable I either get a question mark or a random character. Can anyone please explain how I should pass variables or strings through to the PC? any help would be greatly appreciated.

Thanks ahead of time.

Are you sure that’s the right baud rate? Random characters can be a result of an incorrect baud rate.

So, you are saying that if you send a literal like ‘H’, it comes through OK, but if you send a variable, it comes through as a random character?

I think the problem may be that you are sending a number that is interpreted by the receiver as ASCII. For example, if you do WriteSerialPort2(65), the receiver would see an ‘A’.

If you convert the numeric value to ASCII before you send it, it should come through OK. Take a look at sprintf() and related number-to-string conversion functions.

Cheers,

  • Dean

So I am sorry that I am not getting this, any chance you could point me to a resource that contains some real world examples. I understand the I TO A function, but even including stdlib.h file (MPLAB) doesn’t get it working or maybe I am not using it right.

*I am trying:

int num = 123;
char buffer[5];

itoa(buffer, num);

WriteSerialPortTwo(buffer);*

Am I supposed to convert, and then WriteSerialPortTwo(“%s\n”,buffer); I am not sure why I am not getting any results using this method. does the output need to be in a loop? Please guide me with an example I am ready to rip my hair out. This is day 4 of trying to get something readable from my vex to my PC.

Thank you again for your help!

Yes, you are on the right track. WriteSerialPortTwo() will send only one character at a time. If you loop through the array, it should work. Try:

int num = 123;
int i;
char buffer[5];

itoa(buffer, num);

for (i=0; i<5; i++) {
    WriteSerialPortTwo(buffer*);
}

The way that you can tell you need to do this is by looking at the “API.h” header at the declaration of WriteSerialPortTwo(unsigned char). The fact that its one argument is an unsigned char means it can only take one character at a time. In C, when you use an array as an argument, it just uses a pointer to the array (its address in RAM). If the receiving function isn’t expecting a pointer, it treats the numeric value of the pointer as if it was the actual number being passed in, thus the garbage character. I’m actually surprised this isn’t giving you a “type mismatch” error when you compile.

Hope this works for you!

Cheers,
-Dean*

The VEX DDT software defaults to a baud rate of 115200 BAUD, 8 Data Bits and 1 Sop Bit, so you either have to changed the VEX serial I/O Baud rate down to to 38400 Baud or configure the XBEE to 115200 Baud, which it is capable of handling.

Thanks for the reply, I understand the baud rate differences. All that is setup just fine, I can pass literals just not values from variables. I need to figure out how to write an integer such as from an accelerometer (i.e. - int = 305) on the vex micro-controller side into an array] so array[0] = 3 array[1] = 0, and array[2] = 5. anyone have any idea how to accomplish this?

I know in PHP there is a function called **explode(variable, delimiter) **

Is there a similar function in C for MPLAB?

I am am just not understanding how to send a byte at a time from the VEX controller to my pc.

Thanks for the continued support.

Did my post above not help?
If not, what results did you get?

Cheers,

  • Dean

Well I tried setting up the receiver with python and getting it to translate the incoming data. I have tried sending over various things, from binary, to decimal, to letters. Maybe I am just pedaling backwards. I would like to know how to send the results of a variable to an array before sending, or is this not necessary?

Receiver end:

  1. build string
  2. when complete, spit it out.

My results appear like:

Ø–±-

-ØØ-+Ø

—±Ø

I was also getting hex stuff.

\0x00\0x00\0x00\0x00\0x00\0x00

the first couple would be different than all zeroes, and than change to all zeroes.

Maybe it is sending right, but I do not know how to convert back on the receiver side. I have tried using int(variable) with no cigar. Well off to work.

Thank you for your help!

What Baud rate did you use on the Vex Controller?

What happens if you use Windows Hyperterminal or the IFI Bootloader serial terminal to receive data from the XBEE receiver?

I have been using GTK-Term which is a dumb terminal for linux. I have since been using Python so I can convert the characters being sent from the VEX controller into a readable format. I have been confused about the entire process. Now that I have finally figured it out, it is apparent what my problem was. The VEX controller is sending over ASCII characters and were not being recognized or readable by the dumb terminal or Python in raw format. I have been playing with ways to interpret on the receiving end. It is as simple as converting the read variable from ASCII to Decimal or into whatever format is needed. I wasn’t clear on whether the data being received was one character or the whole integer being sent over. It is indeed the whole integer value. (i.e. - Accel_Y = 255 ) the received portion is also the equivalent of Accel_Y = 255. so if you send over 4 values, you get all 4 values.

Vex side:

WriteSerialPortTwo(accel_y);
Wait (200);

Python Side:


import serial
import binascii

while True:
    if( ser_port.inWaiting() )
        x = ( ser_port.read() )
        y = binascii.b2a_hex(x)
        VALUE = int(y, 16)
        print ("Sensor Value = " + VALUE)