#include #include #include #include #include #include // set correct serial path #define SERIAL_PORT "/dev/ttyS2" int main() { int serial_port; struct termios tty; // open serial_port = open(SERIAL_PORT, O_RDWR | O_NOCTTY); if (serial_port < 0) { perror("Error opening serial port"); return 1; } // Configuration of serial memset(&tty, 0, sizeof(tty)); if (tcgetattr(serial_port, &tty) != 0) { perror("Error getting serial port attributes"); close(serial_port); return 1; } cfsetospeed(&tty, B921600); // set correct baud rate cfsetispeed(&tty, B921600); tty.c_cflag |= (CLOCAL | CREAD); tty.c_cflag &= ~PARENB; tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CSIZE; tty.c_cflag |= CS8; // write configration to serial if (tcsetattr(serial_port, TCSANOW, &tty) != 0) { perror("Error setting serial port attributes"); close(serial_port); return 1; } // loop test while (1) { // receive data char received_data[64]; if (read(serial_port, &received_data, sizeof(char)*64)) { // get data printf("Received: %s", received_data); } // Only send "Hello, UART!" char data_to_send[] = "Hello, UART!\n"; write(serial_port, data_to_send, strlen(data_to_send)); // sleep 100ms usleep(100000); } // close close(serial_port); return 0; }