linux下非标准波特率的设置和使用
2014-05-14 13:35
246 查看
转载于 http://blog.chinaunix.net/uid-9543173-id-1988980.html
通常,在linux下面,设置串口使用终端IO的相关函数设置,如tcsetattr等函数,linux内部有一个对常用波特率列表的索引,根据设置的波特率用底层驱动来设置异步通信芯片的寄存器
对于非标准的任意波特率需要用ioctl(fd, TIOCGSERIAL, p)和ioctl(fd, TIOCSSERIAL, p)的配合,ioctl的最后一个参数是struct serial_struct *类型,在linux/serial.h中定义。其中baud_base是基准晶振频率/16,通常是115200,你需要设的是custom_divisor这个值,最终的波特率为baud_base/custom_divisor,比如你需要28800,因为115200/4=28800,所以要设置custom_divisor=4,。
具体过程为,先设置波特率设为38400(tcsetattr),然后用TIOCGSERIAL得到当前的设置,将flags设置ASYNC_SPD_CUST位,设置custom_divisor,最后用TIOCSSERIAL设置。
使用setserial其实就是利用上述方法,来设置baud_base, custom_divisor等, 其内部实现就是使用ioctl来进行设置,
另外还可以用硬件更换晶振,根据比例来达到使用一些非标准的波特率的目的.
参考: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,
},
};
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chenzhixin/archive/2008/03/12/2172524.aspx
通常,在linux下面,设置串口使用终端IO的相关函数设置,如tcsetattr等函数,linux内部有一个对常用波特率列表的索引,根据设置的波特率用底层驱动来设置异步通信芯片的寄存器
对于非标准的任意波特率需要用ioctl(fd, TIOCGSERIAL, p)和ioctl(fd, TIOCSSERIAL, p)的配合,ioctl的最后一个参数是struct serial_struct *类型,在linux/serial.h中定义。其中baud_base是基准晶振频率/16,通常是115200,你需要设的是custom_divisor这个值,最终的波特率为baud_base/custom_divisor,比如你需要28800,因为115200/4=28800,所以要设置custom_divisor=4,。
具体过程为,先设置波特率设为38400(tcsetattr),然后用TIOCGSERIAL得到当前的设置,将flags设置ASYNC_SPD_CUST位,设置custom_divisor,最后用TIOCSSERIAL设置。
使用setserial其实就是利用上述方法,来设置baud_base, custom_divisor等, 其内部实现就是使用ioctl来进行设置,
另外还可以用硬件更换晶振,根据比例来达到使用一些非标准的波特率的目的.
参考: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,
},
};
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chenzhixin/archive/2008/03/12/2172524.aspx
相关文章推荐
- 剖析非标准波特率的设置和使用于Linux操作系统中
- linux下非标准波特率的设置和使用
- linux设置非标准波特率200000bit/s
- linux下Oracle通过设置大内存页解决使用swap分区问题
- 使用SecureCRT对Linux vim进行颜色设置
- Linux用户管理:设置用户口令的使用期限
- 使用Java Service Wrapper设置tomcat作为linux服务并且开机自动启动
- linux下使用date命令设置时间
- Linux使用系统以太网卡的安装及设置装备陈设-1
- 安装、设置和使用SSH Secure shell 实现windows 登录linux
- linux 设置进程使用的最大文件描述符的个数
- linux下Oracle通过设置大内存页解决使用swap分区问题
- 关于在linux平台上使用java.awt.*包的设置
- 使用Java Service Wrapper设置tomcat作为linux服务并且开机自动启动
- 在 linux 下 使用代码(获取/设置)环境变量
- 【转】使用SecureCRT对Linux vim进行颜色设置【可用的.vimrc文件内容】
- linux设置目录和文件使用权限
- [Linux]使用 ifupdown 的逻辑接口定义进行高级网络设置
- Linux下的输入法 ibus 下载设置和使用
- 我常使用linux下的 vim 样式设置