linux中串口如何支援非標準傳輸速率B28800?

來源:互聯網
上載者:User
 轉貼請註明出處,文章來自: blog.csdn.net/chenzhixin

參考:http://blog.ednchina.com/seam_liu/7181/post.aspx

#include <termios.h>
#include <sys/ioctl.h>
#include <linux/serial.h>

struct serial_t {
    int     fd;
    char    *device;/*/dev/ttyS0,...*/

    int     baud;
    int     databit;/*5,6,7,8*/
    char    parity;/*O,E,N*/
    int    stopbit;/*1,2*/
    int    startbit;/*1*/
   
    struct termios    options;
};

//設定為特訴傳輸速率,比如28800
int serial_set_speci_baud(struct serial_t *tty,int baud)
{
    struct serial_struct ss,ss_set;

    cfsetispeed(&tty->options,B38400);
    cfsetospeed(&tty->options,B38400);

    tcflush(tty->fd,TCIFLUSH);/*handle unrecevie char*/
    tcsetattr(tty->fd,TCSANOW,&tty->options);

    if((ioctl(tty->fd,TIOCGSERIAL,&ss))<0){
        dprintk("BAUD: error to get the serial_struct info:%s/n",strerror(errno));
        return -1;
    }

    ss.flags = ASYNC_SPD_CUST;
    ss.custom_divisor = ss.baud_base / baud;

    if((ioctl(tty->fd,TIOCSSERIAL,&ss))<0){
        dprintk("BAUD: error to set serial_struct:%s/n",strerror(errno));
        return -2;
    }

    ioctl(tty->fd,TIOCGSERIAL,&ss_set);
    dprintk("BAUD: success set baud to %d,custom_divisor=%d,baud_base=%d/n",
            baud,ss_set.custom_divisor,ss_set.baud_base);

    return 0;
}

用法:只要指定serial_t的baud就可以了

static struct serial_t __seri_conf[] = {
    [0] = {//connect with b board, ttyS0
        .device = "/dev/ttyS0",
        .baud = 28800,

        .databit = 8,
        .parity = 'N',
        .stopbit = 1,
    },
};

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.