uart RX/TX测试方法

1.连接方法

       若debug板的RX/TX没有反转,开发板跟debug板连接方式如下:
                  RX - TX,TX - RX, GND - GND

      若debug板的RX/TX有反转,开发板跟debug板连接方式如下:

                  RX - RX,TX - TX,GND - GND

2.测试开发板的TX功能

      开发板通过debug板连接PC,PC打开uart调试工具

      在开发板端echo "test" > /dev/ttyS2,PC端uart调试工具可以收到test字符

            需要注意的是PC端的uart调试工具选择的波特率需要跟开发板默认设置的波特率一致,否则字符无法正常显示。

3.测试开发板的RX功能

    开发板通过debug板连接PC,PC打开uart调试工具

    开发板跑如下读对应ttySx的测试程序:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int main(void)
{
    int fd, ret;

    fd = open("/dev/ttyS2", O_RDWR|O_NOCTTY|O_NONBLOCK);
    if (fd < 0)
    {
        perror("open ttyS2");
        return 1;
    }

    fcntl(fd, F_SETFL, 0); //閲嶈涓哄牭濉炵姸鎬? 鍘绘帀O_NONBLOCK
    //
    struct termios opts;
    tcgetattr(fd, &opts); //鎶婂師璁剧疆鑾峰彇鍑烘潵锛屽瓨鏀惧湪opts

    //璁剧疆娉㈢壒鐜?
    cfsetispeed(&opts, B115200);
    cfsetospeed(&opts, B115200);

    opts.c_cflag |= CLOCAL|CREAD; //蹇界暐modem鎺у埗绾? 鍚姩鎺ユ敹鍣?

    // 8N1
    opts.c_cflag &= ~PARENB; 
    opts.c_cflag &= ~CSTOPB;
    opts.c_cflag |= CS8;

    opts.c_cflag &= ~CRTSCTS; //鍏抽棴纭欢娴佹帶

    opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //raw input
    opts.c_oflag &= ~OPOST; // raw output          

    tcsetattr(fd, TCSANOW, &opts);  
    //
    char data[1024];

    while(1)
    {
        ret = read(fd, data, sizeof(data));
        if(ret > 0)
        {
            data[ret] = 0;
            printf("got : %s\n", data);
        }
        else
        {
            printf("not got data ret: %d\n", ret);
        }
        memset(data,0,sizeof(data));
        //sleep(1);

    }
    close(fd);
    return 0;
}

          PC通过uart调试工具发送字符,开发板可以正常收到字符:

uart设置波特率:

设置demo程序如下:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>

int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
    struct termios newtio,oldtio;
    if  ( tcgetattr( fd,&oldtio)  !=  0) 
    { 
        perror("SetupSerial 1");
        return -1;
    }
    bzero( &newtio, sizeof( newtio ) );
    newtio.c_cflag  |=  CLOCAL | CREAD; 
    newtio.c_cflag &= ~CSIZE; 

    switch( nBits )
    {
    case 7:
        newtio.c_cflag |= CS7;
        break;
    case 8:
        newtio.c_cflag |= CS8;
        break;
    }

    switch( nEvent )
    {
    case 'O':                     //奇校验
        newtio.c_cflag |= PARENB;
        newtio.c_cflag |= PARODD;
        newtio.c_iflag |= (INPCK | ISTRIP);
        break;
    case 'E':                     //偶校验
        newtio.c_iflag |= (INPCK | ISTRIP);
        newtio.c_cflag |= PARENB;
        newtio.c_cflag &= ~PARODD;
        break;
    case 'N':                    //无校验
        newtio.c_cflag &= ~PARENB;
        break;
    }

switch( nSpeed )
    {
    case 2400:
        cfsetispeed(&newtio, B2400);
        cfsetospeed(&newtio, B2400);
        break;
    case 4800:
        cfsetispeed(&newtio, B4800);
        cfsetospeed(&newtio, B4800);
        break;
    case 9600:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    case 115200:
        cfsetispeed(&newtio, B115200);
        cfsetospeed(&newtio, B115200);
        break;
    case 1500000:
        cfsetispeed(&newtio, B1500000);
        cfsetospeed(&newtio, B1500000);
        break;
    default:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    }
    if( nStop == 1 )
    {
        newtio.c_cflag &=  ~CSTOPB;
    }
    else if ( nStop == 2 )
    {
        newtio.c_cflag |=  CSTOPB;
    }
    newtio.c_cc[VTIME]  = 0;
    newtio.c_cc[VMIN] = 0;
    tcflush(fd,TCIFLUSH);
    if((tcsetattr(fd,TCSANOW,&newtio))!=0)
    {
        perror("com set error");
        return -1;
    }
    printf("set done!\n");
    
    return 0;
}

int open_port(int fd,int comport)
{
    char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
    long  vdisable;
    if (comport==1)
    {    fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);
        if (-1 == fd)
        {
            perror("Can't Open Serial Port");
            return(-1);
        }
        else 
        {
            printf("open ttyS0 .....\n");
        }
    }
    else if(comport==2)
    {    fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
        if (-1 == fd)
        {
            perror("Can't Open Serial Port");
            return(-1);
        }
        else 
        {
            printf("open ttyS1 .....\n");
        }    
    }
    else if (comport==3)
    {
        fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);
        if (-1 == fd)
        {
            perror("Can't Open Serial Port");
            return(-1);
        }
        else 
        {
            printf("open ttyS2 .....\n");
        }
    }
    if(fcntl(fd, F_SETFL, 0)<0)
    {
        printf("fcntl failed!\n");
    }
    else
    {
        printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
    }
    if(isatty(STDIN_FILENO)==0)
    {
        printf("standard input is not a terminal device\n");
    }
    else
    {
        printf("isatty success!\n");
    }
    printf("fd-open=%d\n",fd);
    return fd;
}

int main(void)
{
    int fd,fdtest;
    int nread,i;
    unsigned int ispeed,ospeed;
    //char buff[]="Hello\n";
    struct termios tmptio;

    if((fd=open_port(fd,2))<0)
    {
        perror("open_port error");
        return;
    }
    if((i=set_opt(fd,1500000,8,'N',1))<0)
    {
        perror("set_opt error");
        return;
    }
#if 0
    if((fd=open_port(fdtest,1))<0)
    {
        perror("open_port error");
        return;
    }
#endif
    if( tcgetattr( fd,&tmptio)  !=  0) 
    { 
        perror("SetupSerial 1");
        return -1;
    }
    ospeed = cfgetospeed(&tmptio);
    ispeed = cfgetispeed(&tmptio);
    
    printf("ospeed: %d,ispeed: %d\n",ospeed,ispeed);
    //printf("fd=%d\n",fd);

    //nread=read(fd,buff,8);
    //printf("nread=%d,%s\n",nread,buff);
    close(fd);
    return;
}

可以用dump_stack();印出call flow如下:

tty_mode_ioctl->set_termios->tty_set_termios->ms_uart_set_termios
 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值