I don’t know if it’s what you want but I hope it can help, here is my code for UART you just may change BAUDRATE and uart_target :
// Arduino const
#define NSERIAL_CHAR 256
#define VMINX 1
#define BAUDRATE B9600
#define PI 3.14159265
// Arduino read
const char *uart_target = "/dev/ttyACM0";
unsigned char rx_buffer[VMINX];
unsigned char serial_message[NSERIAL_CHAR];
int usnread;
int rx_length;
int nread = 0;
int Rpos = 0;
char Tc = 'x';
string arduinoOut;
int fid = -1;
bool iderror = false;
// Arduino write
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;
int count = -1;
void readArduino()
{
memset(serial_message, 0, 255);
usnread = 0;
tcflush(fid, TCIOFLUSH);
nread = 0;
//while (fid != -1 && usnread<3){
rx_length = read(fid, (void*)rx_buffer, VMINX);
serial_message[nread] = rx_buffer[0];
//printf("Event %d, rx_length=%d, Read=%s\n", nread+1, rx_length, rx_buffer );
while(rx_buffer[0]!='R'){
rx_length = read(fid, (void*)rx_buffer, VMINX);
serial_message[++nread] = rx_buffer[0];
}
Rpos = nread;
for(int i = 0;i<4;i++){
rx_length = read(fid, (void*)rx_buffer, VMINX);
serial_message[++nread] = rx_buffer[0];
}
//printf("R found at %i \n",Rpos); //printf("Received: %s \n", serial_message);
char Tc = (char)serial_message[Rpos-1];
if(Tc=='0'){USid = 0;}else if(Tc=='1'){USid = 1;}else if(Tc=='2'){USid = 2;}else if(Tc=='3'){USid = 3;}else if(Tc=='4'){USid = 4;}else {iderror = true;printf("error USid not computed Tc char = %c \n",Tc);}
//printf("usid int = %i \n",USid);
if(iderror == false){
for(int i=0;i<4;i++){
usdata[USid][i]=(char)serial_message[Rpos+1+i];
}
usnread+=1;
printf("New US %i = %s | ",USid,usdata[USid].c_str());
}else{iderror = false;}
//}
}
int writeArduino()
{
p_tx_buffer = &tx_buffer[0];
for(int i=0;i<arduinoIn.length();i++){
*p_tx_buffer++ = arduinoIn[i];
}
//printf("fid 1=%d\n", fid );
if (fid != -1){
count = write(fid, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
//usleep(1000); // .001 sec delay
//printf("Count = %d\n", count);
if (count < 0) perror("Arduino UART TX error\n");
}
}
int setupArduinoUART()
{
struct termios port_options; // Create the structure
tcgetattr(fid, &port_options); // Get the current attributes of the Serial port
fid = open(uart_target, O_RDWR | O_NOCTTY );
tcflush(fid, TCIFLUSH);
tcflush(fid, TCIOFLUSH);
if (fid == -1){perror("Could not open Arduino");ArduinoSerial=false;}
else{
ArduinoSerial=true;
port_options.c_cflag &= ~PARENB; // Disables the Parity Enable bit(PARENB),So No Parity
port_options.c_cflag &= ~CSTOPB; // CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit
port_options.c_cflag &= ~CSIZE; // Clears the mask for setting the data size
port_options.c_cflag |= CS8; // Set the data bits = 8
port_options.c_cflag &= ~CRTSCTS; // No Hardware flow Control
port_options.c_cflag |= CREAD | CLOCAL; // Enable receiver,Ignore Modem Control lines
port_options.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable XON/XOFF flow control both input & output
port_options.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Non Cannonical mode
port_options.c_oflag &= ~OPOST; // No Output Processing
port_options.c_lflag = 0; // enable raw input instead of canonical,
port_options.c_cc[VMIN] = VMINX; // Read at least 1 character
port_options.c_cc[VTIME] = 0; // Wait indefinetly
cfsetispeed(&port_options,BAUDRATE); // Set Read Speed
cfsetospeed(&port_options,BAUDRATE); // Set Write Speed
int att = tcsetattr(fid, TCSANOW, &port_options);
if (att != 0 ){printf("ERROR in Setting Arduino port attributes");}
else{printf("SERIAL DUE Port Good to Go.\n");}
tcflush(fid, TCIFLUSH);
tcflush(fid, TCIOFLUSH);
}
}
int killArduinoUART()
{close(fid);}
I that don’t work at high baudrate you may indeed need to twerk kernel