主题 : linux中串口如何支持非标准波特率B28800? 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 12357
精华: 0
发帖: 2
金钱: 10 两
威望: 2 点
综合积分: 4 分
注册时间: 2010-01-05
最后登录: 2010-02-02
楼主  发表于: 2010-01-05 13:51

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

管理提醒: 本帖被 qq2440 从 micro2440技术交流专区 移动到本区(2013-11-18)
想在2440板上配置串口1为28800,请高手指点?
下面是网上的一个例子,
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,
    },
};


此例子在2410板的2.4内核可行,但在2440板2.6内核中,提示配置参数无效,请高手指点?
wyong521@sohu.com
级别: 新手上路
UID: 12357
精华: 0
发帖: 2
金钱: 10 两
威望: 2 点
综合积分: 4 分
注册时间: 2010-01-05
最后登录: 2010-02-02
1楼  发表于: 2010-01-15 11:07
问题已解决。
级别: 新手上路
UID: 13511
精华: 0
发帖: 2
金钱: 10 两
威望: 2 点
综合积分: 4 分
注册时间: 2010-01-25
最后登录: 2010-04-10
2楼  发表于: 2010-01-25 14:02
我也遇到同样的问题,怎么解决的?
级别: 新手上路
UID: 13574
精华: 0
发帖: 14
金钱: 75 两
威望: 15 点
综合积分: 28 分
注册时间: 2010-01-26
最后登录: 2014-03-17
3楼  发表于: 2010-02-02 22:47
加上
#include <linux/serial.h>
级别: 新手上路
UID: 13511
精华: 0
发帖: 2
金钱: 10 两
威望: 2 点
综合积分: 4 分
注册时间: 2010-01-25
最后登录: 2010-04-10
4楼  发表于: 2010-02-06 16:31
3楼的,#include <linux/serial.h> 已经加过了
级别: 新手上路
UID: 43717
精华: 0
发帖: 3
金钱: 25 两
威望: 5 点
综合积分: 6 分
注册时间: 2011-04-19
最后登录: 2016-09-05
5楼  发表于: 2011-08-21 21:25
测试端口为/dev/s3c2440_serial1
内核为2.6.28
端口在标准波特率没有问题。
非标准波特率中
调用这段
if((ioctl(tty->fd,TIOCSSERIAL,&ss))<0){
        dprintk("BAUD: error to set serial_struct:%s\n",strerror(errno));
        return -2;
    }
返回无效错误,在x86上运行通过。