arduino, terminal and two serial ports

This is an attempt to document the setup of using the hardware serial port on the arduino and a softserialport, using the newsoftserial library, and then send messages between the serial ports using the Terminal in OSX. Not particularly useful in itself, but very helpful for debugging and monitoring.

First, the hardware setup, which is extremely simple for this setup. I am using a usbserial adapter connected to the softserial ports on the arduino. I defined the softserial pins to be 2,3 (rx,tx), so the usbserial´s tx is connected to pin 2, and the rx to pin 3, also the ground from the usbserial is connected to the arduino ground. That´s all there is to it really (except plugging both the arduino and usbserial to the computer´s usb ports obviously).

The arduino code is just a slight modification of the softserial example, which echoes whatever comes in from the hardware serialport and prints it out on the softserial port and the opposite way around.

#include <NewSoftSerial.h>

NewSoftSerial mySerial(2, 3);  //(rx,tx)

void setup()
{
Serial.begin(9600);
Serial.println(“Hello hardware serial”);

// set the data rate for the NewSoftSerial port
mySerial.begin(9600);
mySerial.println(“Hello software serial”);
}

void loop()                     // run over and over again
{

if (mySerial.available()) {
Serial.print((char)mySerial.read());
}
if (Serial.available()) {
mySerial.print((char)Serial.read());
}
// delay(1000);
}

I use OSX built-in Terminal to communicate with the two serial ports, so open Terminal, and make sure you have two terminal windows (apple-N to create a new one).

You can query the terminal for available serial ports by typing /dev/tty.*, this will give you a list of ports. You can then copy the name of the serial port you are looking for to open a serial connection. The syntax is screen serialportname baudrate. The text in bold is what I typed in.

localhost:~ hcg$ ls /dev/tty.*

/dev/tty.Bluetooth-Modem    /dev/tty.hc-phone-WirelessiAP
/dev/tty.Bluetooth-PDA-Sync    /dev/tty.usbserial-A4001mRQ
/dev/tty.SparkFun-BT-COM0    /dev/tty.usbserial-A6008doa
/dev/tty.W810i-SerialPort-1

localhost:~ hcg$ screen /dev/tty.usbserial-A6008doa 9600

in the other terminal window you want to open the other usbserialdevice, here named /dev/tty.usbserial-A4001mRQ.

When you have entered the screen command in both terminal windows you should get two empty windows, and you should get the text hardware serial and software serial (you might have to reset the arduino to see this).

now you can start typing in one window and the text will appear in the other window (it is invisible in the window you actually type it into).

One trick on my non-US keyboard layout is that I haven´t been able to find the right keyboard shortcuts to close the screen session properly. So what I do is to enable a US keyboard layout, and then ctrl-A followed by ctrl-@ will give me the option to close the session. This is on a norwegian keyboard, so not sure if this will be the same for other international variations.

 

Leave a 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

%d bloggers like this: