linux中串口如何支持非标准波特率B28800?

 转贴请注明出处,文章来自: 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,
    },
};
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值