Hi Everyone:

   I was writing a C code to read telemetry data from the arducopter. The below is my code.

   It is receiving data from the arducopter, because the line "printf("%02x ", (unsigned char)temp);" does print some hex data and the data is the same to what I saw in GTKterm ( a tool to moniter serial communication)

   but the data read cannot be decoded, since "mavlink_parse_char(MAVLINK_COMM_0, temp, &msg, &status)" always returns 0. which means the channel "MAVLINK_COMM_0" is not initialized.

I checked "mavlink_helpers.h" and found it is because
mavlink_get_channel_status( MAVLINK_COMM_0) returns 0.  I do not know how to solve the problem. I appreciate it if anyone can help.

Thank you in advance.

Yucong Lin

// below is my code

#include <stdio.h>

#include <string.h>

#include <fcntl.h>

#include <errno.h>

#include <termios.h>

#include <unistd.h>
#include <mavlink.h>

#define BUFFER_LENGTH 2041

int fd1;

int fd2;

uint8_t buff[BUFFER_LENGTH];
int i = 0;
    
uint8_t temp = 0;

int rd,nbytes;

struct termios options;

int main()

{

fd1=open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

if (fd1 == -1 )

{

perror("open_port: Unable to open /dev/ttyUSB0 – ");

}

else

{

fcntl(fd1, F_SETFL,0);

printf("Port 1 has been sucessfully opened and %d is the file description\n",fd1);

}

tcgetattr(fd1, &options);

    /*
     * Set the baud rates to B57600...
     */

    cfsetispeed(&options, B57600);
    cfsetospeed(&options, B57600);

    /*
     * Enable the receiver and set local mode...
     */

    options.c_cflag |= (CLOCAL | CREAD | CS8);
    
    /*
     * Set the new options for the port...
     */

    tcsetattr(fd1, TCSANOW, &options);

    mavlink_message_t msg;
    mavlink_status_t status;
 
for(;;){
    memset(buff, 0, BUFFER_LENGTH);
                     
        rd=read(fd1,buff,BUFFER_LENGTH);
        if (rd > 1)
                    {
            // Something received - print out all bytes and parse packet
                        
            for (i = 0; i < rd; i++)
            {
                temp = buff[i];
                printf("%02x ", (unsigned char)temp);
                    if (mavlink_parse_char(MAVLINK_COMM_0, temp, &msg, &status))
                             
                {
                    // Packet received
                    printf("\nReceived packet: SYS: %d, COMP: %d, LEN: %d, MSG ID: %d\n", msg.sysid, msg.compid, msg.len, msg.msgid);
                }
                                
            }
                        
            printf("Bytes read are %d \n",rd);
         }
        
}
close(fd1);
}

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • @yucong please try Peter Holland, btw he loves python ....
This reply was deleted.