Hi,
Try this code, is working on USART0, 41 and 44 pins on J3A2 header. For USART2 change the port to “/dev/ttyTHS2”, I have not tried on USART2 but should work.
The code send’s “START” to serial port and waits for a response, tested between jetson and F4 discovery board.
#include <stdio.h>
#include <termios.h> //header contains the definitions used by the terminal I/O interfaces
#include <unistd.h> //read() write() close()
#include <fcntl.h>
#include <string.h>
//Serial port defines and variables:
#define BAUDRATE B9600
#define SERIAL_PATH “/dev/ttyTHS0”
int serial_fd;
int serial_read_ret, serial_write_ret;
struct termios tty;
char serial_buffer_send = “START”;
char serial_buffer_recv[1024] = “”;
int main() {
printf(“Program to write a string to the serial port and read a string from it.\n”);
fflush(stdout);
//Try opening serial port
serial_fd = open(SERIAL_PATH,O_RDWR|O_NOCTTY);
if(serial_fd == -1) { //Checks the availability of the Serial Port
printf(“Failed.\n”);
fflush(stdout);
return 0;
} else {
printf(“Success.\n”);
fflush(stdout);
cfsetospeed (&tty, B9600);
cfsetispeed (&tty, B9600);
// Setting other Port Stuff
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_lflag = 0; // no signaling chars, no echo, no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read block
tty.c_cc[VTIME] = 1; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
tty.c_oflag &= ~OPOST; // make raw
/* Flush Port, then applies attributes */
tcflush( serial_fd, TCIFLUSH );
if ( tcsetattr ( serial_fd, TCSANOW, &tty ) != 0)
{
printf(“Failed settings.\n”);
}
}
//sends
serial_write_ret = write(serial_fd,serial_buffer_send,strlen(serial_buffer_send));
printf(“Sent to serial port: %s\n”,serial_buffer_send);
while(1)
{
serial_read_ret = read(serial_fd,serial_buffer_recv,sizeof(serial_buffer_recv));
if(serial_read_ret < 1 )
printf(“-”);
else
printf(“Read from serial port: %s\n”,serial_buffer_recv);
}
serial_read_ret = close(serial_fd); //Close the serial port
printf(“Serial port closed.\n\n”);
return 0;
}
David Fernandes