How to set-up serial communication without Mavlink

Hi folk!

I am a beginner and I would like to connect, via serial, an external generic device not implementing the MavLink protocol. Should I use the Serial 4/5? If yes, how to read/write single incoming/outconing bytes?

Thanks in advance for your support.

I have been trying since two days without any result but the following

nsh> px4_simple_app
INFO  [px4_simple_app] Hi!
INFO  [px4_simple_app] Opening port
INFO  [px4_simple_app] number of bytes read is -1

INFO  [px4_simple_app] 

INFO  [px4_simple_app] Closing Serial communication
INFO  [px4_simple_app] exiting

This is my current px4_simple_app.

#include <px4_config.h>
#include <px4_tasks.h>
#include <px4_posix.h>
#include <unistd.h>
#include <stdio.h>
#include <poll.h>
#include <string.h>
#include <uORB/uORB.h>

__EXPORT int px4_simple_app_main(int argc, char *argv[]);

int px4_simple_app_main(int argc, char *argv[])
{
PX4_INFO("Hi!");

int bytes;
    char buffer[10];
    char uart_name[] = "/dev/ttyS0";
PX4_INFO("Opening port");
int serial_fd = open(uart_name, O_RDWR | O_NOCTTY);
if (serial_fd < 0) {
PX4_ERR("Failed to open port");
}

struct termios uart_config;
int termios_state;

/* Back up the original uart configuration to restore it after exit */
if ((termios_state = tcgetattr(serial_fd, &uart_config)) < 0) {
PX4_ERR("ERR GET CONF: %d\n", termios_state);
close(serial_fd);
return -1;
}

// Read from the port
    fcntl(serial_fd, F_SETFL, FNDELAY);
    bytes = read(serial_fd, &buffer, sizeof(buffer));
    PX4_INFO("number of bytes read is %d\n", bytes);
    PX4_INFO("%s\n", buffer);

PX4_INFO("Closing Serial communication");
close(serial_fd);

PX4_INFO("exiting");
return 0;
}

I’ve tried with the following HW configurations:

  1. telemetry module attached to TELEM1
  2. telemetry module attached to TELEM2
  3. FTDI attacehd to pins 2/3 of TELEM1
  4. FTDI attacehd to pins 2/3 of TELEM2

@Nicola: I haven’t yet tried running your code, I just had a quick look through it. You are only opening port /dev/ttyS0. This is the port attached to UART1 (it could be attached to another port depending on the port enumerations enumerations, but it is probably UART1).

You need to open the correct port for TELEM1 or TELEM2. If you run ls /dev/ from the NSH console on the Pixhawk, you should see a number of ttyS* ports listed. You can test each one of them in turn.

The fact that your are returning -1 seems to indicate that you do not have a valid file descriptor (see the read function in NuttX/nuttx/fs/fs_read.c) (this might be related to your having opened the incorrect port…but I am not sure).

Hi,

Any updates?

Thanks,

M