- Edited
Hello. We are having a problem reading the data sent from our ESP32 to a Bela Mini. The Arduino code is simply sending sensor values to the serial port.
We know that the two devices are communicating correctly, using linux screen commands in the terminal.
But we are having issues with the read() function that is supposed to give us the values from our ESP32. It gives us a segmentation fault error.
The read() function comes from the unistd.h library and has the following documentation:
http://pubs.opengroup.org/onlinepubs/000095399/functions/read.html?fbclid=IwAR2rQ6R9qzpf89tBXctlyOH6PT6YgyVv4X-jwUn9aW8jCGFBH8VN5Mmw6wQ
#include <Bela.h>
// C library headers
#include <stdio.h>
#include <string.h>
#include <iostream>
// Linux headers
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()
int serial_port = open("/dev/ttyUSB0", O_RDWR); /* ttyUSB0 is the FT232 based USB2SERIAL Converter */
void readTouchData(void*);
AuxiliaryTask touch;
bool setup(BelaContext *context, void *userData)
{
struct termios tty;
memset(&tty, 0, sizeof (tty));
int serial_port = open("/dev/ttyUSB0", O_RDWR); /* ttyUSB0 is the FT232 based USB2SERIAL Converter */
if(serial_port == -1) /* Error Checking */
printf("\n Error! in Opening ttyUSB0 ");
else
printf("\n ttyUSB0 Opened Successfully ");
// Set in/out baud rate to be 115200
cfsetispeed(&tty, B115200); //in
cfsetospeed(&tty, B115200); //out
touch = Bela_createAuxiliaryTask(readTouchData, 50, "auxxTask");
}
void render(BelaContext *context, void *userData)
{
Bela_scheduleAuxiliaryTask(touch);
}
void readTouchData(void*){
rt_printf("HELLO");
// Allocate memory for read buffer, set size according to your needs
char read_buf [256];
memset(&read_buf, '\0', sizeof(read_buf));
// Read bytes. The behaviour of read() (e.g. does it block?,
// how long does it block for?) depends on the configuration
// settings above, specifically VMIN and VTIME
int num_bytes = read(serial_port, &read_buf, sizeof(read_buf));
// n is the number of bytes read. n may be 0 if no bytes were received, and can also be -1 to signal an error.
if (num_bytes < 0) {
rt_printf("Error reading: %s", strerror(errno));
}
}
void cleanup(BelaContext *context, void *userData)
{
}