#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
int main() {
int serial_port_num;
char serial_port[15];
printf("Select a serial port (0/1/3/4): ");
scanf("%d", &serial_port_num);
sprintf(serial_port,"/dev/ttyS%d",serial_port_num);
int serial_fd;
serial_fd = open(serial_port, O_RDWR | O_NOCTTY);
if (serial_fd == -1) {
perror("Failed to open serial port");
return 1;
}
struct termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(serial_fd, &tty) != 0) {
perror("Error from tcgetattr");
return 1;
}
int bt_num =3;
int baud_val=B115200;
printf(" baud rate select is \n 1: 9600 \n 2: 19200\n 3: 38400\n 4: 57600\n 5: 115200\n");
scanf("%d", &bt_num);
switch(bt_num)
{
case 1:
cfsetospeed(&tty, B9600);
cfsetispeed(&tty, B9600);
baud_val =9600;
break;
case 2:
cfsetospeed(&tty, B19200);
cfsetispeed(&tty, B19200);
baud_val =19200;
break;
case 3:
cfsetospeed(&tty, B38400);
cfsetispeed(&tty, B38400);
baud_val =38400;
break;
case 4:
cfsetospeed(&tty, B57600);
cfsetispeed(&tty, B57600);
baud_val =57600;
break;
case 5:
cfsetospeed(&tty, B115200);
cfsetispeed(&tty, B115200);
baud_val =115200;
break;
default:
cfsetospeed(&tty, B115200);
cfsetispeed(&tty, B115200);
baud_val =B115200;
break;
}
printf("baud rate is %d\n",baud_val);
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
if (tcsetattr(serial_fd, TCSANOW, &tty) != 0) {
perror("Error from tcsetattr");
return 1;
}
char tx_buffer[] = "please send data\n";
int tims=10;
while(tims){
tims--;
ssize_t bytes_written = write(serial_fd, tx_buffer, sizeof(tx_buffer));
if (bytes_written < 0) {
perror("Error writing to serial port");
close(serial_fd);
return 1;
}
printf("\rtx_buffer: \n %s ", tx_buffer);
usleep(100000);
char rx_buffer[256];
int bytes_read = read(serial_fd, rx_buffer, sizeof(rx_buffer));
if (bytes_read > 0) {
rx_buffer[bytes_read] = '\0';
printf("\rrx_buffer: \n %s ", rx_buffer);
} else {
printf(" \n");
}
}
close(serial_fd);
return 0;
}
引用: lugl4313820 发表于 2024-3-7 07:10 版主大佬亲自上阵评测,学习了,这板子下载程序是不是特别麻烦呀?
谢谢鼓励。
下载很简单的,专门一个工具,把编译的文件加载进去用usb口烧录就行了。
和单片机差不多。只是分了一些文件 环境env文件、uboot、 内核、文件系统、工具等等,反正可以一次烧录进去。