⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart_test.c

📁 arm2440下
💻 C
字号:
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#define FALSE    -1
#define TRUE        0

int speed_arr[] = { B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, //
                               B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
                              115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, };

void set_speed(int fd, int speed)
{
    int i;
    int status;
    struct termios Opt;   //定义一个结构
    tcgetattr(fd, &Opt);//用来得到机器原端口的默认设置
    for(i = 0; i < sizeof(speed_arr) / sizeof(int); i++)
    {
        if(speed == name_arr[i])//判断传进来是否相等
        {
            tcflush(fd, TCIOFLUSH);//刷新输入输出缓冲
            //cfsetispeed(&Opt, speed_arr);//这里分别设置
            //cfsetospeed(&Opt, speed_arr);
            cfsetispeed(&Opt, speed_arr[i]);
            cfsetospeed(&Opt, speed_arr[i]);
            status = tcsetattr(fd, TCSANOW, &Opt);//立刻把波特率设置真正写到串口中去
            if(status != 0)
            {
                perror("tcsetattr fd1");//设置错误 
            }
            return;
        }
        tcflush(fd, TCIOFLUSH);//得到机器设置
    }
}

int set_Parity(int fd, int databits, int stopbits, int parity)
{
    struct termios options;
    if(tcgetattr(fd, &options) != 0)//首先读取系统默认设置,必须
    {
        perror("SetupSerial 1");
        return(FALSE);
    }
    options.c_cflag &= ~CSIZE;//设置c_cflag选项不按位数据位掩码
    switch(databits)//数据位
    {
        case 7:
            options.c_cflag |= CS7; //设置c_cflag选项数据位为7位
            break;
        case 8:
            options.c_cflag |= CS8;//8 bits
            break;
        default:
            fprintf(stderr, "Unsupported data size\n");
            return(FALSE);
    }

     switch(parity)//设置奇偶校验,c_cflag  c_iflag有效
     {
         case 'n':
         case 'N'://no parity
             options.c_cflag &= ~PARENB;  //clear parity enable
             options.c_iflag &= ~INPCK;     //enable parity checking
             break;
         case 'o':
         case 'O':  //奇校验,INPCK检查校验
             options.c_cflag |= (PARODD | PARENB);//设置奇校验
             options.c_iflag | INPCK;  //disnable parity checking
             break;
         case 'e':
         case 'E'://偶校验
             options.c_cflag |= PARENB;//enable parity
             options.c_cflag &= ~PARODD;//转换为偶校验
             options.c_iflag |= INPCK;//disnable parity checking
             break;
         default:
             fprintf(stderr, "Unsupported parity\n");
             return(FALSE);
     }

     switch(stopbits)//设置停止位,影响表示位是c_cflag
     {
         case 1:
             options.c_cflag &= ~CSTOPB;
             break;
         case 2:
             options.c_cflag |= CSTOPB;
             break;
         default:
             fprintf(stderr, "Unsupported stop bits\n");
             return(FALSE);
     }

     if(parity != 'n')//设置输入是否进行校验
          options.c_iflag |= INPCK;

     options.c_cc[VTIME] = 150;//15 seconds
     options.c_cc[VMIN] = 0;
     tcflush(fd,TCIFLUSH);//刷新和立刻写进去

     if(tcsetattr(fd, TCSANOW, &options) != 0)
     {
         perror("SetupSerial 3");
         return(FALSE);
     }

     return(TRUE);
}


int OpenDev(char *Dev)
{
        int       fd = open( Dev, O_RDWR ); 
        //| O_NOCTTY | O_NDELAY        
        if (-1 == fd)        
        {                         
                perror("Can't Open Serial Port");
                return -1;                
        }        
        else        
                return fd;
}


int main(int arge, char **argv)
{
    int fd;
    int nread;
    char buff[512];
    int loopi;
    int loopj;
    //char *dev = "/dev/ttyS0";
    char *dev="/dev/ttySAC1";
    fd = OpenDev(dev);
    if(fd >= 0)
    {
        set_speed(fd, 115200);
    }
    else
    {
        printf("Can't Open Serial Port!\n");
        exit(0);
    }
    if (set_Parity(fd,8,1,'N')== FALSE) //设置8,1,n 注意,这里和上面要和下位机相符才可能通信
    {
        printf("Set Parity Error\n");
        exit(1);
    }
    while(1)
    {
        while((nread = read(fd, buff, 512)) > 0)
        {
            printf("\nLen %d\n", nread);
            buff[nread + 1] = '\0';
            printf("\n%s",buff);

            for(loopi = 0; loopi < 1000; loopi++)
                for(loopj = 0; loopj < 10000; loopj++);
            
            write(fd,buff,nread);
        }
    }
    
}



















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -