```clike= #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <sys/ioctl.h> #include <termios.h> #define dev_uart "/dev/ttySAC3" #define test "testwrite.txt" int main(int argc , char *argv[]) { int fh, i, fd, cnt = 0, status; char c, bfr[1]; struct termios original_t, new_t; printf("I/O test for UART driver \n"); printf("Press Ctrl C to exit\n"); fh = open(dev_uart, O_RDWR); c = (fh < 0) ? 'F' : 'T'; //printf("%c\n", c ); fd = fileno(fopen(test, "w+")); write(fd, &c, 1); #ifndef __LINE_BUF__ // Disable line buffering tcgetattr(fh, &original_t); bzero(&new_t, sizeof(new_t)); memcpy(&new_t, &original_t, sizeof(new_t)); tcflush(fh, TCIFLUSH); new_t.c_cflag = (B9600 | CS8 | CREAD | CLOCAL); new_t.c_cflag &= ~(PARENB | CSTOPB); new_t.c_iflag = IGNPAR; new_t.c_oflag = 0; //new_t.c_oflag &= ~OPOST; new_t.c_lflag = 0; //new_t.c_lflag &= ~(ICANON | ECHO); //new_t.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); new_t.c_cc[VTIME] = 0; new_t.c_cc[VMIN] = 0; cfsetispeed(&new_t,B9600); cfsetospeed(&new_t,B9600); tcflush(fh, TCIFLUSH); status = tcsetattr(fh, TCSANOW, &new_t); if (status != 0) { printf("tcsetattr fd1"); return; } printf("tcsetattr /dev/ttySAC3 Success!\n"); tcflush(fh, TCIFLUSH); #endif while(1) { memset(bfr, 0, sizeof(bfr) ); printf("sizeof(bfr) : %d\n", sizeof(bfr)); bfr[0]='A'; cnt = 1; system("dmesg | grep tty"); cnt = write(fh, &bfr, cnt); if(cnt < 0){ printf("Write to UART failed!\n"); }else{ printf("Write to UART Success!\n"); } system("dmesg | grep tty"); system("busybox cat /dev/ttSAC3"); write(fd, &bfr, 1); if(cnt > 0) { printf("write %d characters: %s\n", cnt, bfr); } tcdrain(fh); sleep(2); } close(fh); close(fd); #ifndef __LINE_BUF__ // Restore line buffering tcsetattr(fh, TCSANOW, &original_t); //tcsetattr(fileno(stdin), TCSANOW, &original_t); #endif return 0; } ``` 參考資料: 推薦 --> [Linux RS-232 程式設計](http://shyuanliang.blogspot.com/2010/09/linux-rs-232.html) [Linux下串口编程简单实例](https://blog.csdn.net/u011006622/article/details/73330792) [留言觀念區](https://www.ubuntu-tw.org/modules/newbb/viewtopic.php?topic_id=96306&forum=12)