Shopping Cart (0)

Setting up a UDP based network serial port on a Linksys WRT54GS router.

Introduction

This is part 2 of a tutorial I wrote on using network serial ports on a Linksys router. In part 2 of this tutorial I am going to show how to get a UDP based network serial port running on your Linksys WRT54GS dd-wrt powered router.

This is an animation of netcat receiving a Locosys GPS NMEA datastream via UDP.

This is an animation of netcat receiving a Locosys GPS NMEA datastream via UDP.

The nice thing about Linksys WRT54GS routers is that you can get them for about $25 on eBay. You need to check the model number to make sure it has enough ram to run the full version (standard generic) of dd-wrt and have plenty of memory available to create a JFFS storage partition. You can check out the details and specs of your router model on the dd-wrt router database.

You need to start by following the all the steps on my first blog post to get a working TCP based serial link before you try this tutorial. Part 2 of this tutorial also expects you to have created the JFFS partition and installed the custom build of netcat from the first tutorial. Also, you need to confirm that you are able to remotely connect by TCP to the serial port on your Linksys router and read data from the port.

For this tutorial my Linksys WRT54GS router already has the 10 pin 0.1″ serial port header connector soldered onto the circuit board and the router is running dd-wrt with a local IP address of 192.168.1.30

The Linksys WRT54GS router has two serial ports. One of the serial ports is easy to use and the other serial port is tied up with the serial console on the router.

Step 1. Prepare the Files on your desktop system.

Download the ZIP archive file: dd-wrt_serial_UDP.zip

Then expand the ZIP file on your desktop computer.

You will now have a new folder called dd-wrt UDP serial software that contains the following files:

busybox-stty-setconsole.zip

gps2udp.sh

setserial_2.17-1_mipsel.ipk

Next we need to unzip the ZIP archive called busybox-stty-setconsole.zip to get a file named:

busybox

This is a custom build of the busybox program that has the custom stty setconsole command enabled. This program will allow you to set the baud rate for the Linksys WRT54GS serial port.

Step 2. Copy the files to your Linksys router.

Use the SCP protocol and a program like WINSCP to copy the files from your desktop computer onto the /jffs/tmp folder on your Linksys router.

This is the WinSCP Login window.

This is the WinSCP Login window.

To connect to your Linksys router with SCP you need to type in the IP address of your router under Host Name. Set the user name to root and type in your password.

The File Protocol should be set to SCP.

You also need to edit the WinSCP Environment settings.

You also need to edit the WinSCP Environment settings.

In the WinSCP Login window click on the SCP/Shell section. Under the Other options section you need to disable Lookup user groups so you don’t get annoying warning messages every time you use WinSCP to connect to your router.

Click the Login button to connect to your dd-wrt router.

You will see a dd-wrt banner when you connect to the router. Click the Continue button.

You will see a dd-wrt banner when you connect to the router. Click the Continue button.

This is the initial path for WinSCP.

This is the initial path for WinSCP.

You need to navigate in WinSCP to the /jffs/tmp folder to install the UDP support files on the router.

Drag the files you expanded from the ZIP archive in Step 1 of this tutorial to the /jffs/tmp folder in WinSCP.

This is the file copy dialogue in WinSCP.

This is the file copy dialogue in WinSCP.

This is the folder view of the /jffs/tmp directory after I copied the supporting files in place.

This is the folder view of the /jffs/tmp directory after I copied the supporting files in place.

(It is important that you have followed Step 4. Enable JFFS2 Support on your router from on my first tutorial so you have a persistent JFFS file system enabled on your router.)

Step 3. Install the supporting files

Login to your router using SSH.

On Linux use your standard terminal program. On Mac OS X use the Terminal.app program that is located in your /Applications/Utilites folder. On Windows you can download the free Putty SSH program.

I am going to connect to the router using SSH with the default username of “root”.

On Mac and Linux systems you would connect to the router using:

ssh root@192.168.1.30

On Windows the SSH program called PUTTY is used to connect to the router.

Here is a screenshot of the PUTTY Configuration window that is used to login to the dd-wrt router.

Here is a screenshot of the PUTTY Configuration window that is used to login to the dd-wrt router.

Using Putty you can save the connection settings by giving your SSH connection a session name and pressing the Save button.

This is the DD-WRT v24 login banner that is shown when you connect to the router using SSH.

This is the DD-WRT v24 login banner that is shown when you connect to the router using SSH.

Once you are connected to the router you need to navigate to the temp folder on the JFFS partition. This is where your UDP support files are.

cd /jffs/tmp

Next we need to install the setserial program. Start by updating the ipkg package list. The Setserial package is used to configure the serial port’s IRQ address.

Type the following into the dd-wrt SSH command line:

ipkg update

ipkg install setserial_2.17-1_mipsel.ipk

The ipkg installer makes it easy to install dd-wrt software packages.

The ipkg installer makes it easy to install dd-wrt software packages.

At this point we should make the new busybox program and the gps2udp.sh bash script executable using the chmod command with the 775 executable permissions:

chmod 775 busybox

chmod 775 gps2udp.sh

You can check if the files have the correct permissions using the list directory option:

ls -la

The chmod program is used to change the file permissions.

The chmod program is used to change the file permissions.

Create a new bin folder in the /jffs/usr/ directory. Then copy the new busybox program from the current /jffs/tmp folder to the new /jffs/usr/bin folder.

cd /jffs/usr/

mkdir bin

cp /jffs/tmp/busybox /jffs/usr/bin/

 

If you have ser2net running on the Linksys router (based upon the instructions from part 1 of my previous Linksys dd-wrt serial port tutorial) now would be a good time to quit the ser2net process. If ser2net is listed as a startup item in the dd-wrt web UI you should remove it there aswell.

Run the program “top” from the dd-wrt SSH command line to find out the current Process ID (PID) for ser2net.

top

Make a note of the PID number for ser2net. This PID number is unique and changes every time you restart your router.

Now let’s quit top by pressing the Q key on the keyboard. To termiate the ser2net process, type in the command “kill” and the unique PID number on your system that was displayed next to the process name “ser2net” in top. In this example ser2net had a PID of 324.

kill 324

Step 4. Testing the UDP settings

To test that everything is installed correctly lets manually type in the settings once before we define a new startup command.
We will start by defining an IRQ address for the tts/1 serial port. This is the available serial port on a Linksys WRT54GS router that is NOT linked to the router’s serial console.

setserial /dev/tts/1 irq 3

For this example I have a Locosys 3.3 Volt gps connected to the 10 pin header I soldered on my Linksys WRT54GS router in part 1 of the tutorial. The Locosys LS20033 GPS has already been programmed to run at 38400 baud.

I am going to use the custom busybox version (which supports the stty command) that we just installed on the router to set the baud rate of the serial port to 38400 baud. On my Linksys router I had to run the command twice to force the setting.

/jffs/usr/bin/busybox stty -F /dev/tts/1 raw speed 38400

/jffs/usr/bin/busybox stty -F /dev/tts/1 raw speed 38400

Next we will run the program netcat (that we installed in the previous Linksys serial tutorial). Netcat is a handy network utiltiy that has a lot of cool features.

The feature we are going to use today is netcat’s ability to forward data from the /dev/tts/1 serial port to an incomming UDP connection. When we start netcat from the command line we can enable UDP support by adding the -u argument  We had to install the full featured version of netcat to get UDP support. The standard version of netcat that is comes pre-installed with dd-wrt is called “nc” and lacks UDP.

In this example I am going to have netcat reply to any incomming UDP requests using the UDP port number 36700. Netcat will use the Linux < redirection feature to open the tts/1 serial port on the Linksys router and send the serial data out over UDP.

netcat -l -p 36700 -u < /dev/tts/1

Step 5. Setting up a Startup item

Note: You need to have a BASH .sh startup script set to run netcat in an infinite loop. This is important because netcat will quit every time it finishes a remote UDP connection on the Linksys router. The BASH script will restart netcat automatically so your router will be ready for a new connection.

The web based dd-wrt control panel makes it easy to define startup items.

The web based dd-wrt control panel makes it easy to define startup items.

Using a web browser connect to the dd-wrt control panel.  Open the Administration > Commands tab.

In the Command Shell type:

/jffs/tmp/gps2udp.sh

Then click the Save Startup button to create a startup item.

Reboot the router to enable the startup item.

The gps2udp.sh script is now a startup item on the router.

The gps2udp.sh script is now a startup item on the router.

If you have trouble with the startup script you can login to the router using SSH and check TOP to see if the startup script is actually  running.

In the dd-wrt command line type:

top

TOP will list all of the running processes on the router. If the startup item was save correctly when you reboot the router you should see the program gps2up.sh listed in top.

TOP will list all of the running processes on the router. If the startup item was save correctly when you reboot the router you should see the program gps2up.sh listed in top.

For your reference, here is the contents of my bash startup script gps2udp.sh. If you set this script to be a startup item it will automatically set the baud rate for the serial port and launch netcat in an infinite loop. You can change the baud rate of the connection by replacing the number 38400 in the script with a different speed such as 9600 baud or 4800 baud. You can change the UDP server port number by replacing the netcat 36700 port number to another port number.

Contents of the  gps2udp.sh bash script included in the UDP support files ZIP archive:

#!/bin/sh

echo “———————————-”

echo “Starting a GPS session on port 36700”

echo “using /dev/tts/1 at 38400 baud”

echo “———————————-”

setserial /dev/tts/1 irq 3

/jffs/usr/bin/busybox stty -F /dev/tts/1 raw speed 38400

/jffs/usr/bin/busybox stty -F /dev/tts/1 raw speed 38400

while [ “forever” ]

do

netcat -l -p 36700 -u < /dev/tts/1

done

 

Step 6. Connecting to the router using UDP

Using UDP with netcat on Mac OS X

Now that you have your Linksys router all set up the netcat program will listen for an incomming UDP connection.

Note: With this set up only one UDP connection is possible at a time. Also the default version of netcat called nc that comes bundled with Mac OS X does not include UDP support!

Since the default version of netcat that comes with Mac OS X doesn’t include UDP support,  an easy way to install a full featured version of netcat is to use the MacPorts open source package installer.

You can download MacPorts from macports.org .

Apple’s free xcode compiler is also required to compile the open source packages downloaded by MacPorts.

Once xcode and MacPorts are installed on your Mac you can then install netcat using the following command in the Terminal.app program:

sudo port install netcat

With the full version of netcat installed on Mac OS X you can now connect with UDP to the serial port on the Linksys router.

In this example I am going to use netcat running on my Mac OS X computer to connect to port 36700 on my Linksys router. If everything is set up correctly I should see a live NMEA serial data stream from my Locosys GPS over my network connection.

nc -u 192.168.1.30 36700

Once nc connects to the Linksys router you need to press either the enter or return key on your keyboard to start viewing the serial data stream.

Here is a sample of the live NMEA datastream in my terminal from window:

$GPGGA,194541.957,,,,,0,0,,,M,,M,,*4F

$GPRMC,194541.957,V,,,,,0.00,0.00,030312,,,N*49

$GPGGA,194542.290,,,,,0,0,,,M,,M,,*4C

$GPRMC,194542.290,V,,,,,0.00,0.00,030312,,,N*4A

$GPGGA,194542.623,,,,,0,0,,,M,,M,,*40

$GPRMC,194542.623,V,,,,,0.00,0.00,030312,,,N*46

$GPGGA,194542.957,,,,,0,0,,,M,,M,,*4C

$GPRMC,194542.957,V,,,,,0.00,0.00,030312,,,N*4A

$GPGGA,194543.290,,,,,0,0,,,M,,M,,*4D

$GPRMC,194543.290,V,,,,,0.00,0.00,030312,,,N*4B

$GPGGA,194543.623,,,,,0,0,,,M,,M,,*41

$GPRMC,194543.623,V,,,,,0.00,0.00,030312,,,N*47

Using UDP with MikroC Pro for PIC32 on Windows

This is the MikroC Pro for PIC32 startup screen.

This is the MikroC Pro for PIC32 startup screen.

To test the UDP connection on Windows I started by using the MikroC Pro for PIC32 built-in UDP Communication Terminal utility. This utility comes with all versions of MikroC. The UDP Terminal in MikroC offers a simple GUI that lets you send and receive UDP data. This utility comes in handy when you need to interact with a networked UDP device like a PIC microcontroller while you are programming a new firmware with MikroC.

You can find the MikroC UDP Communication Terminal in the Tools menu.

The MikroC UDP Terminal is located in the Tools menu.

The MikroC UDP Terminal is located in the Tools menu.

The MikroC UDP Communication Terminal makes it easy to test a UDP connection on Windows.

The MikroC UDP Communication Terminal makes it easy to test a UDP connection on Windows.

In the MikroC UDP Communications Termial type in the IP address for the Linksys router and the UDP port number 36700. Then click the Connect button.

You need to send a single character to netcat on the Linksys router to start receiving the UDP datastream. Type any ascii letter into the send text field and then press the send button. Now the UDP Terminal will start receiving the UDP packets from the router. You can click the Clear button to erase the contents of the received text area.

Using PUTTY and a loopback connection on the Router

Another way I was able to test the UDP connection to the router was by using Putty to create a 2nd SSH connection on the Linksys router. I then ran another copy of netcat on the Linksys router using SSH to connect to the UDP server port:

netcat -u 192.168.1.30 36700

Using PUTTY with SSH to create a 2nd SSH connection to the router.

Using PUTTY with SSH to create a 2nd SSH connection to the router.

3 comments
  1. Thanks for these great Linksys/DD-WRT serial articles! This could save me a few ethernet-to-serial servers. I have a few questions. First, have you tried to use the console port as a 2nd user serial port? I understand it may require a custom dd-wrt firmware. Secondly, have you used the SDL library for serial over ethernet? For my application, I’m actually looking for a linux server program that can accept incoming connections from multiple clients and broadcast live GPS correction data from a serial port to all those clients simultaneously. Any ideas? Thanks again.

  2. Hi Matt.

    Have you tried the GPSD (GPS service daemon)? It can serve a GPS device over a serial port to multiple network connections. http://www.catb.org/gpsd/

    A few years ago I experimented with SDL_net and BSD sockets on Mac OS X along with Termios for the serial port as part of my UAV Mission Control ground control station project. Using SDL_net and termios in my own program was a bit like re-inventing the wheel. http://www.andrewhazelden.com/blog/2011/08/the-story-of-andrews-uav-ground-control-station/

    Regards,
    Andrew

Comments are closed.