Reading from Telem2 on Pixhawk

I am creating a custom flight mode on the Pixhawk for a quadcopter, and I need to read in a value from Telem2 (UART D). I’m not a great C++ programmer, so much of my work is based on existing code (mostly Loiter). My problem is that I cannot find any example of reading in values from the Telem pins using UART. According to dev.ardupilot, there should be a ‘read byte from UART’ function, but I can’t find it in any libraries like AP_HAL. I’ve googled stuff like ‘reading UART c++’, but it seems like most people are using functions from libraries on whatever device they are using.

If anyone has any input about how I can read in UART bytes, I would really appreciate it.

Just for reference; what I’m doing so far is based off of the UART test example

// setup the UART
static void setup_uart(AP_HAL::UARTDriver *uart, const char *name)
{
    if (uart == NULL) {
        // that UART doesn't exist on this platform
        return;
    }
    uart->begin(57600);
}
setup_uart(hal.uartD, "uartD"); // telemetry 2 at 57600 with default buffer sizes

int read_uart(AP_HAL::UARTDriver *uart, const char *name)
{
    if (uart == NULL) {
        // that UART doesn't exist on this platform
        return;
    }
    uart->read();//Here is where I need to figure out how to read from this uart object
}

[b]Hi,
i’m working on a serial wireless connection between copters and my programming skills are not deep.

But i found out that yout can use the uart read function like this…

the read() function reads only 1 character!!!

so you make like this(here in a 50hz user hook)[/b]

static int pos;
static char buf[8];
char ch = 1; // here i had to give him a start value, don’t know exactly why
/* maybe it works with: char ch[1]; */

ch = hal.uartC->read(); // reads a character coming from uart…
buf[pos++] = ch;
if(pos >= 7)
pos = 0;

the code i posted before runs not properly, i know [color=#FF8040]this[/color] works in the init()…

while(hal.uartC->read() != ‘a’)
{
hal.console->print(".");
hal.scheduler->delay(10);
}
hal.console->print(“a empfangen…\n”);

hope this helps!

[quote=“mondare”][b]Hi,
i’m working on a serial wireless connection between copters and my programming skills are not deep.

But i found out that yout can use the uart read function like this…

the read() function reads only 1 character!!!

so you make like this(here in a 50hz user hook)[/b]

static int pos;
static char buf[8];
char ch = 1; // here i had to give him a start value, don’t know exactly why
/* maybe it works with: char ch[1]; */

ch = hal.uartC->read(); // reads a character coming from uart…
buf[pos++] = ch;
if(pos >= 7)
pos = 0;[/quote
I’d love to hear an update on linking.

Sent from my iPhone using Tapatalk

Like this you can send required data…

#if 1
	char            c = 0;
	static char   buf[16];
	static unsigned int i;
      while(hal.uartC->available() > 0 && c != '\n' && i < sizeof(buf)){
		c 		 = hal.uartC->read();
		buf[i++] = c;
	}
	if(i > 0 && (buf[i-1] == '\n' || i >= sizeof(buf))){
		buf[i-1] = 0;
		i        = 0;
		if(strcmp(buf, "baro"    )==0){
			b_baro        = true;
		}
		if(strcmp(buf, "gspeed"  )==0){
			b_gps_speed   = true;
		}	
		if(strcmp(buf, "gcourse" )==0){
			b_gps_course  = true;
		}	
	} 
#endif