The Ubuntu serial library uses

1. Installation: sudo apt-get install ros-kinetic-serial
2. Detailed introduction of the library: http://docs.ros.org/en/kinetic/api/serial/html/classserial_1_1Serial.html#afa2c1f9114a37b7d140fc2292d1499b9
3. How to use the library
(1) Namespace of the library: serial
(2) Serial class: Serial ser Related header file: serial.h
(3) Initialization variable type:
port: const std::string
baud rate: uint32_t
read and write timeout time: Timeout
byte size: bytesize_t
stop bit size: stopbits_t
flow control: flowcontrol_t
(4) configure serial port

    //配置串口:serial_port_ = "/dev/ttyUSB0"
    ser.setPort(serial_port_);
    //配置波特率:baudrate_ = 115200
    ser.setBaudrate(baudrate_);
    //配置读写超时时间:simpleTimeout(1000)为1000毫秒
    serial::Timeout to = serial::Timeout::simpleTimeout(1000);
    ser.setTimeout(to);
    //设置字节大小
    //serial::eightbits:8位 serial::fivebits:5位  serial::sevenbits:7位 serial::sixbits:6位
     ser.setBytesize(serial::eightbits);
     //设置奇偶校验位//serial::parity_even :偶校验  serial::parity_odd :奇校验 serial::parity_mark :校验位始终为1 serial::parity_space :校验位始终为0 serial::parity_none :无校验      
    ser.setParity(serial::parity_none);
    //设置停止位 serial::stopbits_one :1位 serial::stopbits_one_point_five :1.5位 serial::stopbits_two :2位  
    ser.setStopbits(serial::stopbits_one);
    //打开串口
    ser.open();
    //关闭串口
    ser.close();
    //将中断条件设置为给定级别,默认为true
    ser.setBreak(a); //a为true或false
    //将DTR捂手设置为给定级别,默认为true
    ser.setDTR(a); //a为true或false
    //将RTS捂手设置为给定级别,默认为true
    ser.setRTS(a); //a为true或false
    //串行口的流量控制
    ser.setFlowcontrol(a); //a为serial::flowcontrol_none或者serial::flowcontrol_software或者serial::flowcontrol_hardware
    //获取缓冲区的字符数
    size_t available();
    size_t data_size = ser.available();
    //清空缓冲区
    void flush();//清空所有
    ser.flush();
    void flushInput();//清空输入
    ser.flushInput();
    void flushOutput();//清空输出
    ser.flushOutput();
    //获取波特率
    uint32_t ba = ser.getBaudrate();
    bytesize_t ba = ser.getBaudrate();
    //获取流控制
    flowcontrol_t fc  = ser.getFlowcontrol();
    //获取奇偶校验
    parity_t pt = ser.getParity();
    //获取端口
    std::string port = ser.getPort();
    //获取停止位
    stopbits_t st = ser.getStopbits();
    //获取读写超时时间
    Timeout tt = ser.getTimeout();
    //获取是否打开串口
    bool isopen = ser.isOpen();
    //设置发生RS-232中断信号
    ser.sendBreak(duration);//duration 位int 类型
    //其他
    bool getCD();
    bool getCTS();
    bool getDSR();
    bool getRI();

(5) Read and write control
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/yyl80/article/details/109294541