zoukankan      html  css  js  c++  java
  • 串口编程485

    对于波特率的设置通常使用cfsetospeed和cfsetispeed函数来完成。获取波特率信息是通过cfgetispeed和 cfgetospeed函数来完成的。

      cfsetospeed函数

      头文件:

      #include

      函数原型:

      int cfsetospeed(struct termios *termptr, speed_t speed);

      参数:

      struct termios *termptr - 指向termios结构的指针

      speed_t speed - 需要设置的输出波特率

      返回值:

      如果成功返回0,否则返回-1

      cfsetispeed函数

      头文件:

      #include

      函数原型:

      int cfsetispeed(struct termios *termptr, speed_t speed);

      参数:

      struct termios *termptr - 指向termios结构的指针

      speed_t speed - 需要设置的输入波特率

      返回值:

      如果成功返回0,否则返回-1

      cfgetospeed函数

      头文件:

      #include

      函数原型:

      speed_t cfgetospeed(const struct termios *termptr);

      参数:

      const struct termios - 指向termios结构的指针

      返回值:

      返回输出波特率

      cfgetispeed函数

      头文件:

      #include

      函数原型:

      speed_t cfgetispeed(const struct termios *termptr);

      参数:

      const struct termios *termptr - 指向termios结构的指针

      返回值:

      返回输入波特率

      波特率常量:

      CBAUD 掩码

      B0 0波特

      B50 50波特

      B75 75波特

      B110 100波特

      B134 134波特

      B150 150波特

      B200 200波特

      B300 300波特

      B600 600波特

      B1200 1200波特

      B1800 1800波特

      B2400 2400波特

      B9600 9600波特

      B19200 19200波特

      B38400 38400波特

      B57600 57600波特

      B115200 115200波特

    example:

    int Uart_Open(char *dev_name)
    {
    int fd;
    //int c=0, res;
    #if SET_UART
    struct termios oldtio, newtio;
    #endif


    printf("Start... ");
    fd = open(dev_name, O_RDWR|O_NOCTTY);

    if (fd < 0) {
    perror(dev_name);
    exit(1);
    }

    printf("Open... ");
    #if SET_UART
    tcgetattr(fd, &oldtio);
    bzero(&newtio, sizeof(newtio));

    newtio = oldtio;

    cfsetispeed(&newtio, BAUDRATE);
    cfsetospeed(&newtio, BAUDRATE);
    cfmakeraw(&newtio);

    tcflush(fd, TCIOFLUSH);
    tcsetattr(fd, TCSANOW, &newtio);
    #else
    system("stty sane 115200 raw -echo -crtscts parodd < /dev/tts/0");
    #endif
    return fd;

    }

  • 相关阅读:
    大数据之路week07--day05 (一个基于Hadoop的数据仓库建模工具之一 HIve)
    大数据之路week07--day04 (Linux 中查看文件内容的关键字处)
    大数据之路week07--day04 (YARN,Hadoop的优化,combline,join思想,)
    hdu 1575 Tr A(矩阵快速幂,简单)
    hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
    zoj 2974 Just Pour the Water (矩阵快速幂,简单)
    LightOj 1065
    LightOj 1096
    poj 1006 生理周期(中国剩余定理)
    POJ 2251 Dungeon Master(广搜,三维,简单)
  • 原文地址:https://www.cnblogs.com/pengkunfan/p/3816896.html
Copyright © 2011-2022 走看看