Home > ham radio, Linux > Key ham radio transceiver from command line

Key ham radio transceiver from command line

Related to the hamfax project, i have been asked how to key a ham radio transmitter that is connected to the serial port. So far this is fairly untested, i only put together what i think would solve the problem. Please use this as input for your own solutions if it helps you. If something is wrong, i would appreciate corrections.

Basically, this is just a simple program to make use of the circuit at http://www.baycom.org/~tom/pcf/ptt_circ/ptt.html#serial

C program serial_rts_set.c

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>

  Usage: serial_rts_set device_file sleep/seconds
  e.g.   serial_rts_set /dev/ttyS0 10000

int main(int argc, char** argv)
  char* devfile;
  int fd;
  int rc;
  int status;
  unsigned int seconds;

  if(argc<3) {
     puts("Not enough arguments.\n\n"
          "Usage: serial_rts_set devicefile timeout\n"
          "devicefile   Device file for serial port, e.g. /dev/ttyS0\n"
          "timeout      Timeout in seconds\n");

  devfile = argv[1];

  seconds = 0;
  sscanf(argv[2], "%d", &seconds);

  fd = open(devfile, O_RDWR);
  if(fd == -1) {
    perror("Could not open device file: ");

  rc = ioctl(fd, TIOCMGET,&status);
  if(rc == -1) {
    perror("Error during TIOCMGET: ");

  /* Set the RTS bit to raise the RTS line */
  /* The reverse operation would be status&=~TIOCM_RTS; */

  rc = ioctl(fd, TIOCMSET, &status);
  if(rc == -1) {
    perror("Error during TIOCMSET: ");


Compile and use it with:

gcc -o serial_rts_set serial_rts_set.c

Test with

./serial_rts_set /dev/ttyS0 5

to set the RTS line for 5 seconds.

Running from a shell script


# Start the RTS set program in the background and remember
# its process id (PID) for stopping it later. $! is the PID of the
# most recently executed background command.
# The time passed to sleep_rts_set has to be longer than the time
# that is actually used for keying the transmitter.
serial_rts_set /dev/ttyS0 20 &

# Do whatever is necessary while the transmitter is keyed
sleep 10

# Stop the transmit by stopping the RTS set program
kill $RTS_PID

References and more information

Categories: ham radio, Linux Tags: , ,
  1. October 17, 2009 at 4:03 pm

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

  2. October 18, 2009 at 9:36 am


  1. No trackbacks yet.

Leave a Reply to chsc Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s