Design a site like this with WordPress.com
Get started

writetoserial – A small program that sends binary data to a serial port

While waiting for the hardware I need to get any further on my little programming project I have been experimenting with sending and receiving data from the FRDM-KL25Z through a serial port. Mostly to prepare the tools I will need to debug the system I’m planning on building, and in particular the code and algorithms I will use for file encryption. Unfortunately, sending and receiving files and binary data over a serial port proved to be a little harder to do than I had anticipated. It was possible to do this to some extent by using simple tools like ‘cat’ to write data to the serial port’s device file, but I could not get it to work reliably for binary data or files over a few kilobytes in size. It would sometimes drop data or stop halfway for no apparent reason.

Because of this I wrote a simple little program ‘writetoserial’ that reads the contents of file and writes it to a serial port as binary data. This works far more reliably and I’m now able to send both large text files and binary files to the FRDM-KL25Z.

Advertisement

How to use the serial interface to communicate with an FRDM-KL25Z in Linux

This short guide will show how to communicate with the FRDM-KL25Z using the Serial and USBSerial classes from the mbed library in Linux.

Serial and USBSerial are two different methods for creating serial connections using mbed. USBSerial creates an emulated serial port over a normal USB port while Serial uses the built in serial port (if there is one). In the case of the FRDM-KL25Z  this can be done through the virtual serial port provided by OpenSDA.

This small example will create two serial connections; one using the serial port built into OpenSDA, and one over the normal USB port. It will pass everything given to either interface on to the other interface.

#include <mbed.h>
#include <USBSerial.h>

Serial debug(USBTX, USBRX);
USBSerial usb;

int main()
{
  while(1) {
    if(usb.readable()) {
      debug.putc(usb.getc());
    }
    if(debug.readable()) {
      usb.putc(debug.getc());
    }
  }
}

Compile the code and load it onto your FRDM-KL25Z, then  connect it to your computer using both USB ports and run the following command.

ls -l /dev/ttyACM*

Your output should look something like this.

crw-rw---- 1 root dialout 166, 0 Aug 19 13:56 /dev/ttyACM0
crw-rw---- 1 root dialout 166, 1 Aug 19 14:19 /dev/ttyACM1

Each of these device files are connected to one of the serial interfaces on the FRDM-KL25Z. To use them you will first have to add your user to the dialout group.

sudo usermod -a -G dialout USERNAME

You can use any terminal emulator of your choice to connect to these serial ports. I will be using minicom for this example.

Open two terminals. On the first run:

minicom -b 9600 -D /dev/ttyACM0

And in the second:

minicom -b 9600 -D /dev/ttyACM1

‘-b 9600’ sets the baud rate. The default value used by mbed is 9600.

If everything is working correctly whatever you write in one terminal should now appear on the other, and vice versa. If it doesn’t create new lines when you press ‘enter’ and it instead jumps to the beginning of the current line, tell minicom to add linefeeds by pressing ‘Ctrl-a a’.