Serial Comm with Xbee on 3pi

Hello everyone, I am new.

I recently bought a 3pi and I have the following problem:

I am using the code example serial1 in where I have added from another example a method that gives the voltage of the battery. The method follows:

void bat_test()
{
	int bat = read_battery_millivolts();

	print_long(bat); // This works prints on the screen
	print("mV"); // This line also works, it prints
	memcpy_P(send_buffer, bat, 10); // my problem is here
	serial_send(send_buffer, 10); // and here

	delay_ms(300);
}

In the serial1 example I have added the above method and I have also added a new case to the switch statement:

case 'b':
			bat_test();
			break;

Therefore, when I send from the laptop a b it calls the bat_test(). The communication works, when I send a b it prints on the 3pi LCD screen but it doesnot send me back a correct buffer on my laptops console.

If you ask me why I have placed the number 10 on the buffer I do not know I was trying randomly. I think that number represents the bytes of the buffer? Without being sure.
I want to know how can I send variables from the program space to the serial port?

I know it is something easy for most of you in here, but I cannot find it.

Thank you in advance, for all your replies!

Hello.

memcpy is used to copy the elements of one array to another, but bat is an integer, not an array. You need a way to create an ASCII string representation of the integer bat (e.g. 5321 needs to become {‘5’, ‘3’, ‘2’, ‘1’}). You could write the code to do this yourself, or you could include <stdio.h> and use sprintf():

#include <stdio.h>

...

  char str[10];  // make the array long enough to hold your string + terminator char (0)!

  char len = sprintf(str, "%d mV", bat);
  if (len > 0)
    serial_send(str, len);

...

Note that I haven’t actually tried this code, so please let me know if you have any trouble with it.

- Ben

It worked perfectly fine.
Thank you so much! :smiley: