Arduino串口的正确打开方式

  • 串口配置:波特率115200、8位数据、无校验位、1个停止位
    参考 https://www.arduino.cc/en/Serial/Begin

代码块

/* Open serial port /dev/ttyACM0 */
    int fd; /* File descriptor for the port */
    fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);

/* Configure */
    struct termios options;

    /* Get the current options for the port... */
    tcgetattr(fd, &options);

    /* Set the baud rates to 115200... */
    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);

    /* 8 bits, no parity, one stop bit */
    options.c_cflag &= ~PARENB;
    options.c_cflag |= CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

    /* Canonical mode */
    options.c_lflag |= ICANON;

    /* Set the new options for the port... */
    tcsetattr(fd, TCSANOW, &options);

/* Read Data */
    #define LEN 512
    char buf[LEN]
    read(fd, buf, LEN); /* Don't forget to erase buf */

      • 代码块


你可能感兴趣的:(嵌入式)