Configure and use non-standard baud rates in Linux

Source: Internet
Author: User

The development of Linux has exceeded Microsoft's imagination in recent years, and has caught up with Microsoft's trend. As a result, most people begin to learn about Linux. Generally, in Linux, set the serial port to use function settings related to terminal I/O, such as tcsetattr. in Linux, there is an index to the list of common baud rates, and the register of the asynchronous communication chip is set with the underlying Driver Based on the configured baud rate.

For non-standard random baud rates, the ioctl (fd, TIOCGSERIAL, p) and ioctl (fd, TIOCSSERIAL, p) must be used together. The last parameter of ioctl is the strucsct serial_struct * type, in Linux/serial. h. Here, baud_base is the baseline crystal oscillator Frequency/16, usually 115200. You need to set the custom_divisor value. The final baud rate is baud_base/custom_divisor. For example, you need 28800 because 115200/4 = 28800, therefore, set custom_divisor = 4 ,.

The specific process is to first set the baud rate to 38400 (tcsetattr), then use TIOCGSERIAL to get the current settings, set the flags ASYNC_SPD_CUST bit, set custom_divisor, and finally use TIOCSSERIAL settings.

Setserial is actually used to set baud_base, custom_divisor, etc. Its internal implementation is to use ioctl for setting,

In addition, you can use hardware to replace the crystal oscillator and use some non-standard baud rates based on the ratio.

Reference: http://blog.ednchina.com/seam_liu/7181/post.aspx

 
 
  1. #include <termios.h> 
  2. #include <sys/ioctl.h> 
  3. #include <Linux/serial.h> 
  4. struct serial_t {  
  5.     int     fd;  
  6.     char    *device;/*/dev/ttyS0,...*/  
  7.     int     baud;  
  8.     int     databit;/*5,6,7,8*/  
  9.     char    parity;/*O,E,N*/  
  10.     int    stopbit;/*1,2*/  
  11.     int    startbit;/*1*/  
  12.     struct termios    options;  
  13. }; 

// Set it to the specific baud rate, such as 28800

 
 
  1. int serial_set_speci_baud(struct serial_t *tty,int baud)  
  2. {  
  3.     struct serial_struct ss,ss_set;  
  4.     cfsetispeed(&tty->options,B38400);  
  5.     cfsetospeed(&tty->options,B38400);  
  6.     tcflush(tty->fd,TCIFLUSH);/*handle unrecevie char*/  
  7.     tcsetattr(tty->fd,TCSANOW,&tty->options);  
  8.     if((ioctl(tty->fd,TIOCGSERIAL,&ss))<0){  
  9.         dprintk("BAUD: error to get the serial_struct info:%s\n",strerror(errno));  
  10.         return -1;  
  11.     }  
  12.     ss.flags = ASYNC_SPD_CUST;  
  13.     ssss.custom_divisor = ss.baud_base / baud;  
  14.     if((ioctl(tty->fd,TIOCSSERIAL,&ss))<0){  
  15.         dprintk("BAUD: error to set serial_struct:%s\n",strerror(errno));  
  16.         return -2;  
  17.     }  
  18.     ioctl(tty->fd,TIOCGSERIAL,&ss_set);  
  19.     dprintk("BAUD: success set baud to %d,custom_divisor=%d,baud_base=%d\n",  
  20.             baud,ss_set.custom_divisor,ss_set.baud_base);  
  21.     return 0;  

Usage: you only need to specify the baud of serial_t.

 
 
  1. static struct serial_t __seri_conf[] = {  
  2.     [0] = {//connect with b board, ttyS0  
  3.         .device = "/dev/ttyS0",  
  4.         .baud = 28800,  
  5.         .databit = 8,  
  6.         .parity = 'N',  
  7.         .stopbit = 1,  
  8.     },  
  9. }; 

The above is the configuration and usage of non-standard baud rates in Linux.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.