Programming the Serial Port on Speedway Revolution Reader

In this article, we'll show how to use the serial port from your custom application on the Speedway Revolution reader. The Speedway Revolution reader has two serial ports: The first is labeled 'Console' and is accessible through an RJ-45 jack on the rear panel. This serial port is primarily used for the RShell interface. It's referred to as /dev/ttyS0 in the operating system. The second serial port (/dev/ttyS1) is unused and can be accessed through the DB-15 GPIO port. Your cable should be wired in a null modem configuration, with the RX and TX wires swapped.

Now, let's get to some code. Here is a serial port example in C that asks the user for their name and then replies with a 'Hello'.

In this example, we set the port to raw input mode (line 40). This allows you to read bytes as they become available. Otherwise, data is sent over the port one line at a time. Line buffering would have worked better in this example, but we wanted to demonstrate the raw input mode, which you're more likely to use. You may want to change this setting depending on your application. Another thing to note is the receive buffer size (line 57). We're using a single character buffer to simplify the example. In practice you should use a larger buffer. The Speedway Revolution reader runs an embedded distribution of Linux. If your serial port programming needs are more advanced, take a look at this great online resource for Linux serial programming.

Sample Code:

void serialSetup(int fd)
    struct termios options;
    // Set serial port to blocking mode.
    fcntl(fd, F_SETFL, 0);
    // Get the current options for the port.
    tcgetattr(fd, &options);
    // Set the tx and rx baud rates to 115,200.
    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);
    // Enable the receiver and set local mode.
    options.c_cflag |= (CLOCAL | CREAD);
    // 8 data bits, no parity, one stop bit.
    // 8N1
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    // No hardware flow control.
    options.c_cflag &= ~CRTSCTS;
    // No software flow control.
    options.c_iflag &= ~(IXON | IXOFF | IXANY);
    // Raw input mode. No line buffering.
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    // Raw output mode.
    options.c_oflag &= ~OPOST;
    // Flush the input buffer
    tcflush(fd, TCIFLUSH);
    // Set the new options for the port.
    tcsetattr(fd, TCSANOW, &options);
int main()
    int fd;
    char dev[] = "/dev/ttyS1";
    char prompt[] = "Enter your name and press return: ";
    char buf = '\0';
    char name[255];
    char reply[255];
    int i = 0;
    // Open the serial port.
    // Returns -1 if there is an error.
    fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
    // Check if open was successful.
    if (fd >= 0)
        // Successfully opened the serial port.
        // Configure the port.
        // Send the prompt
        write(fd, prompt, strlen(prompt));
        // Read serial port one char at a time
        // until a newline is received.
        while (buf != '\r')
            read(fd, &buf, 1);
            if (buf != '\r')
                name[i] = buf;
        // Terminate the name string.
        name[i] = '\0';
        // Say hello
        sprintf(reply, "Hello %s.", name);
        write(fd, reply, strlen(reply));
        // Close the serial port.
        // Failed to open the serial port
        perror("Cannot open serial port. ");


Was this article helpful?
1 out of 1 found this helpful



Article is closed for comments.