linux开发板串口和stm32单片机串口通信,Linux给单片机发送没有问题,linux接受单片机发来的数据就有问题,我的测试程序是想在终端上实时打印接受的数据,但是实际上什么都没有打印,而且关闭单片机的一瞬间终端会跳出几个错的字符,以下是我的linux测试程序,和单片机发送字节的程序
串口设置:波特率9600,8个数据位,无校验,一个停止位,已检查没有问题
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#define SERIAL_PORT "/dev/ttySTM3"
#define BAUD_RATE B9600
int main(void)
{
int serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
if (serial_fd < 0) {
perror("open");
exit(EXIT_FAILURE);
}
struct termios options;
tcgetattr(serial_fd, &options);
cfsetispeed(&options, BAUD_RATE);
cfsetospeed(&options, BAUD_RATE);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; // No parity
options.c_cflag &= ~CSTOPB; // One stop bit
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; // Eight data bits
tcsetattr(serial_fd, TCSANOW, &options);
while (1) {
char buf[255 + 1];
ssize_t n_read = read(serial_fd, buf, sizeof(buf) - 1);
if (n_read > 0) {
buf[n_read] = '\0';
printf("%s", buf);
fflush(stdout);
} else if (n_read < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
usleep(1000);
} else {
perror("read");
exit(EXIT_FAILURE);
}
}
}
close(serial_fd);
return 0;
}
单片机程序
#include "sys.h"
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "AD.h"
#include "LED.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
USART2_Init();
while(1)
{
USART_SendData(USART2, 'A');
Delay_ms(500);
USART_SendData(USART2, 'B');
Delay_ms(500);
USART_SendData(USART2, 'C');
Delay_ms(500);
}
}
单片机这边的程序确定数据发送出来了吗?用串口调试助手测试过了吗?