Baud Rate Simulator

Sometimes you need things in life to slow down. Modern terminal emulators are great tools, and they can render text very quickly. This is usually the desired effect. But there are times when you need things to come at you a little slower. I have some old vt100/ansi animations that essentially do nothing when they are rendered at full speed. I could not find any tools to simulate different baud rates, so I wrote one myself. It is written in C, and uses the nanosecond() function call to introduce the appropriate delay between printing each character. Yes, I know that with real serial connections, there is a delay between each bit rather than each byte, but for the intent of this utility, the effect is the same.

It takes one parameter, which is the desired baud rate. If you do not specify one, it will do with the defined default. It is meant to be used in a pipe.

I have tested it on Ubuntu linux and MacOS. It is very simple, and does not use any special libraries.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>


	baud - A tool to simulate a specified baud rate in a pipe.

	I wrote this to be able to play some of the old VT100/ansi animations
	at the rate of your choice.  Since most terminal emulator apps render
	the text as fast as the system can (which is effectively instantaneous),
	the animations do not actually animate.  This allows you to add
	a throttle to view as if you were on an actual serial terminal.

	It takes one parameter, which is the desired baud rate.  If you do not
	specify one, it will use the default defined below.  Extra parameters
	will be ignored.

	-- Scott Garrett
	   version 2022050401


#define defaultbaudrate	9600

int main(int argc, char *argv[])
	int baudrate;
	int c;

	if ( argc == 1 )
		baudrate = defaultbaudrate;
		baudrate = atoi(argv[1]);

	/* Cannot have a negative baud rate, and 0 makes no sense
	   (and would cause a division by zero).
	if ( baudrate <= 0 )
		printf("Invalid baud rate: %d\n", baudrate);

	/* Convert the desired baud rate into nanoseconds */
	int baud = 10000000000 / baudrate;

	/* Make sure the final value is in the valid range that nanosleep()
	if ( baud < 0 || baud > 999999999 )
		printf("Invalid baud rate: %d\n", baudrate);

	/* Now that we know the desired baud rate delay in nanoseconds,
	   load up the struct with the values.  0 seconds and the baud rate delay.
	struct timespec remaining, request = {0, baud};

	while ((c = getchar()) != EOF)
		/* Here we grab a character from stdin, write it, flush it out
		   so it is seen immediately, then pause for "remaining" nanoseconds.
		int response = nanosleep(&request, &remaining);

Leave a Comment