嵌入式爱好者

查看: 118|回复: 1

[已解决] RK3588串口问题

[复制链接]

1

主题

3

帖子

10

积分

RK3588通行证

扫一扫,手机访问本帖
发表于 2024-6-18 20:22:08 | 显示全部楼层 |阅读模式
本帖最后由 Kevin6788 于 2024-6-19 11:05 编辑

3588板子刚上电,串口外设只能发,但是收不到数据,然后这个时候在命令行里调用了说明手册提供的命令行串口测试程序接收完一次数据后,我调用自己的程序就能正常接收数据了,
命令行是fltest_uarttest -d /dev/ttyS9
自己串口测试代码如下,大佬帮忙看看
回复

使用道具 举报

1

主题

3

帖子

10

积分

RK3588通行证

 楼主| 发表于 2024-6-18 20:23:13 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>

int main() {
  int fd;
  struct termios options;
  char buffer[255];
  int bytes_read;

  // 打开串口设备
  fd = open("/dev/ttyCH341USB0", O_RDWR | O_NOCTTY | O_NDELAY );
  if (fd == -1) {
        perror("open_port: Unable to open serial port - ");
        return(-1);
  }

  // 获取并配置串口选项
  tcgetattr(fd, &options);
  cfsetispeed(&options, B115200); // 输入波特率
  cfsetospeed(&options, B115200); // 输出波特率
  options.c_cflag |= (CLOCAL | CREAD); // 开启接收
  options.c_cflag &= ~CSIZE; // 清除当前数据位设置
  options.c_cflag |= CS8;    // 8位数据位
  options.c_cflag &= ~PARENB; // 关闭校验位
  options.c_cflag &= ~CSTOPB; // 1位停止位
  options.c_cc[VTIME] = 0; // 非阻塞读取
  options.c_cc[VMIN] = 0; // 读取10个字符后返回
  tcflush(fd,TCIFLUSH);
  tcsetattr(fd, TCSANOW, &options);

  // 发送数据
  const char data_to_send[] = "Hello, serial port!\n";

  write(fd, data_to_send, strlen(data_to_send));
  // 接收数据
  while(1)
  {
        //        sleep(1);
        bytes_read = read(fd, buffer, sizeof(buffer));
        //printf("hello\n");
        //while(1)
        //{}
        if (bytes_read > 0)
        {
          buffer[bytes_read] = '\0'; // 确保字符串以null结尾
          printf("bytes_read:%d,Received: '%s'\n",bytes_read, buffer);
          bytes_read = 0;
          //break;
        }
  }

  // 关闭串口
  close(fd);

  return 0;
}
点评回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋| 飞凌嵌入式 ( 冀ICP备12004394号-1 )

GMT+8, 2024-6-27 02:20

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表