<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Linux%2FCustom_Buad_Rate</id>
	<title>Linux/Custom Buad Rate - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Linux%2FCustom_Buad_Rate"/>
	<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Linux/Custom_Buad_Rate&amp;action=history"/>
	<updated>2026-05-09T01:22:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://aznot.com/index.php?title=Linux/Custom_Buad_Rate&amp;diff=4214&amp;oldid=prev</id>
		<title>Kenneth: Created page with &quot;== Usage ==   screen /dev/ttyUSB3   linux-custom-baudrate /dev/ttyUSB3 115200   linux-custom-baudrate /dev/ttyUSB3 195360  # linux-custom-baudrate: actual baudrate is 60000000...&quot;</title>
		<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Linux/Custom_Buad_Rate&amp;diff=4214&amp;oldid=prev"/>
		<updated>2017-03-13T21:21:05Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Usage ==   screen /dev/ttyUSB3   linux-custom-baudrate /dev/ttyUSB3 115200   linux-custom-baudrate /dev/ttyUSB3 195360  # linux-custom-baudrate: actual baudrate is 60000000...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Usage ==&lt;br /&gt;
&lt;br /&gt;
 screen /dev/ttyUSB3&lt;br /&gt;
&lt;br /&gt;
 linux-custom-baudrate /dev/ttyUSB3 115200&lt;br /&gt;
&lt;br /&gt;
 linux-custom-baudrate /dev/ttyUSB3 195360&lt;br /&gt;
 # linux-custom-baudrate: actual baudrate is 60000000 / 307 = 195439.734375&lt;br /&gt;
&lt;br /&gt;
== Source ==&lt;br /&gt;
&lt;br /&gt;
linux-custom-baudrate.c:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;termio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;err.h&amp;gt;&lt;br /&gt;
#include &amp;lt;linux/serial.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static int rate_to_constant(int baudrate) {&lt;br /&gt;
#define B(x) case x: return B##x&lt;br /&gt;
	switch(baudrate) {&lt;br /&gt;
		B(50);     B(75);     B(110);    B(134);    B(150);&lt;br /&gt;
		B(200);    B(300);    B(600);    B(1200);   B(1800);&lt;br /&gt;
		B(2400);   B(4800);   B(9600);   B(19200);  B(38400);&lt;br /&gt;
		B(57600);  B(115200); B(230400); B(460800); B(500000); &lt;br /&gt;
		B(576000); B(921600); B(1000000);B(1152000);B(1500000); &lt;br /&gt;
	default: return 0;&lt;br /&gt;
	}&lt;br /&gt;
#undef B&lt;br /&gt;
}    &lt;br /&gt;
&lt;br /&gt;
/* Open serial port in raw mode, with custom baudrate if necessary */&lt;br /&gt;
int serial_open(const char *device, int rate)&lt;br /&gt;
{&lt;br /&gt;
	struct termios options;&lt;br /&gt;
	struct serial_struct serinfo;&lt;br /&gt;
	int fd;&lt;br /&gt;
	int speed = 0;&lt;br /&gt;
&lt;br /&gt;
	/* Open and configure serial port */&lt;br /&gt;
	if ((fd = open(device,O_RDWR|O_NOCTTY)) == -1)&lt;br /&gt;
		return -1;&lt;br /&gt;
&lt;br /&gt;
	speed = rate_to_constant(rate);&lt;br /&gt;
&lt;br /&gt;
	if (speed == 0) {&lt;br /&gt;
		/* Custom divisor */&lt;br /&gt;
		serinfo.reserved_char[0] = 0;&lt;br /&gt;
		if (ioctl(fd, TIOCGSERIAL, &amp;amp;serinfo) &amp;lt; 0)&lt;br /&gt;
			return -1;&lt;br /&gt;
		serinfo.flags &amp;amp;= ~ASYNC_SPD_MASK;&lt;br /&gt;
		serinfo.flags |= ASYNC_SPD_CUST;&lt;br /&gt;
		serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;&lt;br /&gt;
		if (serinfo.custom_divisor &amp;lt; 1) &lt;br /&gt;
			serinfo.custom_divisor = 1;&lt;br /&gt;
		if (ioctl(fd, TIOCSSERIAL, &amp;amp;serinfo) &amp;lt; 0)&lt;br /&gt;
			return -1;&lt;br /&gt;
		if (ioctl(fd, TIOCGSERIAL, &amp;amp;serinfo) &amp;lt; 0)&lt;br /&gt;
			return -1;&lt;br /&gt;
		if (serinfo.custom_divisor * rate != serinfo.baud_base) {&lt;br /&gt;
			warnx(&amp;quot;actual baudrate is %d / %d = %f&amp;quot;,&lt;br /&gt;
			      serinfo.baud_base, serinfo.custom_divisor,&lt;br /&gt;
			      (float)serinfo.baud_base / serinfo.custom_divisor);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	fcntl(fd, F_SETFL, 0);&lt;br /&gt;
	tcgetattr(fd, &amp;amp;options);&lt;br /&gt;
	cfsetispeed(&amp;amp;options, speed ?: B38400);&lt;br /&gt;
	cfsetospeed(&amp;amp;options, speed ?: B38400);&lt;br /&gt;
	cfmakeraw(&amp;amp;options);&lt;br /&gt;
	options.c_cflag |= (CLOCAL | CREAD);&lt;br /&gt;
	options.c_cflag &amp;amp;= ~CRTSCTS;&lt;br /&gt;
	if (tcsetattr(fd, TCSANOW, &amp;amp;options) != 0)&lt;br /&gt;
		return -1;&lt;br /&gt;
&lt;br /&gt;
	return fd;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    if (serial_open(argv[1], atoi(argv[2])) &amp;lt; 0)&lt;br /&gt;
    {&lt;br /&gt;
        fprintf(stderr, &amp;quot;failed\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    pause();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Source: https://jim.sh/ftx/files/linux-custom-baudrate.c&lt;br /&gt;
&lt;br /&gt;
Reference:&lt;br /&gt;
* linux - C code for non-standard baud rate on Debian/Raspberry Pi - Stack Overflow - http://stackoverflow.com/questions/23492088/c-code-for-non-standard-baud-rate-on-debian-raspberry-pi&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Compile ===&lt;br /&gt;
&lt;br /&gt;
 gcc linux-custom-baudrate.c -o linux-custom-baudrate&lt;br /&gt;
&lt;br /&gt;
 cp linux-custom-buadrate /usr/local/sbin/&lt;/div&gt;</summary>
		<author><name>Kenneth</name></author>
	</entry>
</feed>