I've been playing around with a Raspberry Pi for a while, and wanted to get something working over a serial connection. So after experimenting for a bit this is what I came up with.

Hardware

  • A Computer with Linux and a Raspberry Pi with Debian "squeeze"
  • USB to Serial connector with TXD and RXD connected to the correct UART pins (fourth and fifth from the top left)

use ls /dev/tty* on your computer and on the pi before and after connecting to find out which terminals they're connected to. Test the connection by logging into the pi with screen: sudo screen /dev/ttyUSB0 115200 (replace ttyUSB0 with the terminal your pi is on)

Next we need to tell linux to not use the connection as a terminal so that our application can use it. To do this we need to make two changes:

  1. Remove the serial port from getty initialization in /etc/inittab:
    • My line 55 looked like this: 2:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
    • I commented this out and created an entry for tty2 just like the others
  2. Remove the serial port from the boot command line in /boot/cmdline.txt:
    • I had an entry like this: console=ttyAMA0,115200
    • I made a backup of the file and removed that entry

Reboot the pi and you should be good to go

Software

node.js

Follow the instructions here. This will take a long time.

For node v0.8.0 configure with ./configure --dest-cpu=armv6 (and the line numbers will be slightly different). Update: this doesn't actually work for node v0.8.0 right now

Serial port module

npm install serialport2

Chat client

Here is the extremely simple serial chat client I used, mostly from the example on the serialport2 github page:

var SerialPort = require('serialport2').SerialPort;  
var port = new SerialPort();  
var stdin = process.stdin;

port.on('data', function(data) {  
  console.log(data.toString());
  port.write(data);
});

port.on('error', function(err) {  
  console.log(err);
});

port.open('/dev/ttyUSB0', {  
  baudRate: 115200,
  dataBits: 8,
  parity: 'none',
  stopBits: 1
}, function(err) {
  console.log("Port opened.");
  while(1) {
    port.write("00000000");
  }
});

stdin.on('data', function(chunk) {  
  var line = chunk;
  port.write(line);
});

stdin.resume();  

Run that on the computer and on the pi and you should be able to talk between them over the serial connection.

Tags: raspberry pi, linux