uart 超声波传感器数据读取

  • 传感器选择

    淘宝上搜索 US-100 , 价格大概在17块人民币左右。
  • 读取数据的代码如下:

    // include/aplex_tty.h
    #ifndef _APLEX_TTY_H__                                                          
    #define _APLEX_TTY_H__                                                          
                                                                                
    #include <stdio.h>                                                              
    #include <stdlib.h>                                                             
    #include <string.h>                                                             
    #include <sys/types.h>                                                          
    #include <sys/stat.h>                                                           
    #include <fcntl.h>                                                              
    #include <unistd.h>                                                             
    #include <termios.h>                                                            
    #include <string.h>                                                             
    #include <errno.h>                                                              
    #include <unistd.h>                                                             
                                                                                
    int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop);             
    float read_dis(void);                                                           
                                                                                
    #endif        

    // src/aplex_tty.c      
    #include "../include/aplex_tty.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;   //CLOCAL:忽略modem控制线  CREAD:打开接受者
        newtio.c_cflag &= ~CSIZE;           //字符长度掩码。取值为:CS5,CS6,CS7或CS8
                                                                                
        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); // INPACK:启用输入奇偶检测;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 460800:                                                                
            cfsetispeed(&newtio, B460800);                                          
            cfsetospeed(&newtio, B460800);                                          
            break;                                                                  
        default:                                                                    
            cfsetispeed(&newtio, B9600);                                            
            cfsetospeed(&newtio, B9600);                                            
            break;                                                                  
        }                                                                           
                                                                                
        if( nStop == 1 )                                                            
            newtio.c_cflag &=  ~CSTOPB;     //CSTOPB:设置两个停止位,而不是一个     
        else if ( nStop == 2 )                                                      
            newtio.c_cflag |=  CSTOPB;                                              
                                                                                
        newtio.c_cc[VTIME]  = 0;            //VTIME:非cannoical模式读时的延时,以十分之一秒位单位
        newtio.c_cc[VMIN] = 0;              //VMIN:非canonical模式读到最小字符数    
        tcflush(fd,TCIFLUSH);               // 改变在所有写入 fd 引用的对象的输出都被传输后生效,所有已接受但未读入的输入都在改变发生前丢弃。
        if((tcsetattr(fd,TCSANOW,&newtio))!=0) //TCSANOW:改变立即发生               
        {                                                                           
            perror("com set error");                                                
            return -1;                                                              
        }                                                    
        printf("set done!\n\r");                                                    
        return 0;                                                                   
    }                                                                               
                                                                                
    float read_dis(void)                                                            
    {                                                                               
        float distant = 0;                                                          
        int tty_fd, retval;                                                         
        int write_val = 0x55;                                                       
        char read_val[2];                                                           
                                                                                
        tty_fd = open("/dev/ttyO0", O_RDWR | O_NOCTTY | O_NDELAY);                  
        if (tty_fd < 0)                                                             
        {                                                                           
            perror("open tty error ");                                              
            return -1;                                                              
        }                                                                           
                                                                                
        retval = set_opt(tty_fd, 9600, 8, 'N', 1);                                  
        if (retval < 0)                                                             
        {                                                                           
            perror("set error");                                                    
            return -1;                                                              
        }                                                                           
                                                                                    
        retval = write(tty_fd, &write_val, sizeof(int));                            
        if (retval < 0)                                                             
        {                                                                           
            perror("write error");                                                  
            return -1;                                                              
        }      
                                                                                
        usleep(100000);                                                             
                                                                                
       retval = read(tty_fd, read_val, 2);                                         
        if (retval < 0)                                                             
        {                                                                           
            perror("read errno");                                                   
            return -1;                                                              
        }                                                                           
                                                                                
        distant = read_val[0] * 256 + read_val[1] ;                                 
                                                                                
        close(tty_fd);                                                              
                                                                                
        return distant;                                                             
    }      

    // main/main.c
    #include "../include/aplex_tty.h"                                               
                                                                                
    int main(void)                                                                  
    {                                                                               
        float read_dis_num = 0;                                                     
                                                                                
        read_dis_num = read_dis();                                                  
                                                                                
        printf(" distant : %f\n", read_dis_num / 1000);                             
                                                                                
        return 0;                                                                   
    }                                          
posted @ 2017-08-07 13:54  陈富林  阅读(353)  评论(0编辑  收藏  举报