It is a bit late but I may have a solution for you.
I have managed to write a loopback program in C to test if the RaspberryPi4 is receiving any data.
Receiver.c
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>
#include <stdlib.h>
//Read Function
void receiveMessage();
//Loop Function
void loopFunctionReceivedMessage(int input);
int main()
{
loopFunctionReceivedMessage(3);
return 0;
}
void loopFunctionReceivedMessage(int input)
{
time_t start_time = time(NULL);
while(1)
{
time_t current_time = time(NULL);
double elapsed = difftime(current_time, start_time);
if(elapsed >= input)
{
receivedMessage();
start_time = current_time;
}
}
}
void receivedMessage()
{
int file, count;
if((file=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY| O_NONBLOCK))<0)
{
perror("UART: Failed to open the file.\n");
return -1;
}
fcntl(file, F_SETFL, 0);
struct termios options;
tcgetattr(file, &options);
options.c_cflag= B9600 | CS8 | CREAD | CLOCAL| CSTOPB;
options.c_iflag = ECHO | ECHONL | ICANON | IEXTEN | ISIG |IGNPAR | ICRNL;
tcflush(file, TCIFLUSH);
tcsetattr(file, TCSANOW, &options);
char receive[30];
if((count = read(file,(void*)receive,30))<0)
{
perror("UART: Failed to read from the port :(.\n");
return -1;
}
printf("Received: %s", receive);
close(file);
}
Sender.c
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#include <time.h>
//Sending Function
void sendMessage(char *input_ptr);
//LoopFunction
void loopFunctionMessage(int input);
int main(int argc, char *argv[])
{
loopFunctionMessage(3);
return 0;
}
void loopFunctionMessage(int input)
{
time_t start_time = time(NULL);
while(1)
{
time_t current_time = time(NULL);
double elapsed = difftime(current_time, start_time);
if(elapsed >=input)
{
sendMessage("Michael Jackson\n");
start_time= current_time;
}
}
}
void sendMessage(char *input_ptr)
{
printf("You have entered: %s\n",input_ptr);
int file, count;
if ((file=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY| O_NONBLOCK))<0)
{
perror("UART: Failed to open the file.\n");
return -1;
}
struct termios options;
tcgetattr(file, &options);
options.c_cflag = B9600 | CS8 | CREAD | CLOCAL| CSTOPB;
options.c_iflag = ECHO | ECHONL | ICANON | IEXTEN | ISIG |IGNPAR | ICRNL;
tcflush(file, TCIFLUSH);
tcsetattr(file, TCSANOW, &options);
char command[100];
sprintf(command, "echo -e '%s' > /dev/ttyS0",input_ptr);
system(command);
if((count = write(file, input_ptr, 70))<0)
{
perror("UART: Failed to write to the output.\n");
return -1;
}
close(file);
}