Hi!
I've configured my Bela so UART4 is activated. I have the Tx pin of an arduino connected to P9-11 (UART4 Rx), through a level shifter so it comes out at 3.3v. The grounds are connected too.
I've set the baud rate to 38400.
I found some example code on the internet - and it successfully sends data out. I can also see my serial data being received by the Bela if I run this command in the terminal -
screen -S serial /dev/ttyS4 38400
However, when I run my program, I don't see anything displayed. Could anyone tell me why?
Here's the code:
#include <Bela.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#define SERIAL_BUFFER_SIZE 255
int serialPort;
char serialBuffer[SERIAL_BUFFER_SIZE];
int testCount = 0;
void serialTest(void* arg)
{
while(!gShouldStop)
{
unsigned char cmd[] = {'H', 'i', ' ', 'C', 'a', 'r', 'l', '\n', '\0'};
write( serialPort, cmd, sizeof(cmd) -1);
/* *** READ *** */
int bytesRead = read(serialPort, serialBuffer , SERIAL_BUFFER_SIZE);
if (bytesRead > 0) rt_printf("%s\t[%i]\n", serialBuffer, bytesRead);
usleep(10000);
}
}
void startSerial()
{
speed_t baud = B38400;
serialPort = open( "/dev/ttyS4", O_RDWR | O_NONBLOCK | O_NDELAY );
if ( serialPort < 0 ) printf("UART4 not available\n");
/* set the other settings (in this case, 9600 8N1) */
struct termios settings;
tcgetattr(serialPort, &settings);
cfsetospeed(&settings, baud); /* baud rate */
settings.c_cflag &= ~PARENB; /* no parity */
settings.c_cflag &= ~CSTOPB; /* 1 stop bit */
settings.c_cflag &= ~CSIZE;
settings.c_cflag |= CS8 | CLOCAL; /* 8 bits */
settings.c_lflag = ICANON; /* canonical mode */
settings.c_oflag &= ~OPOST; /* raw output */
tcsetattr(serialPort, TCSANOW, &settings); /* apply the settings */
tcflush(serialPort, TCOFLUSH);
memset (&serialBuffer, '\0', sizeof serialBuffer); // allocate serial buffer memory
Bela_scheduleAuxiliaryTask(Bela_createAuxiliaryTask(serialTest, 50, "serialTest", NULL));
}
bool setup(BelaContext *context, void *userData)
{
startSerial();
return true;
}
void render(BelaContext *context, void *userData)
{
}
void cleanup(BelaContext *context, void *userData)
{
}