31 December 2010

A simple forwarder for AIS and NMEA 0183 data over TCP/IP

I've recently moved the NMEA 0183 HS serial port from my Windows PC input to the central Linux micro computer, and written a small script that retransmits the serial data out over TCP/IP.

This has a number of advantages in my particular install:
  • It allows me to log the AIS data stream.
  • It allows me to view the AIS on both Expedition and Nobeltec software at the same time.
  • It allows me to simultaneously access the AIS stream from my iPad. I've tested it with Digital Yacht's iAIS app, but it should work with other apps running on the iPad.
So instead of being able to use the AIS data only on a single Windows application I can now use it on all navigation hardware access that I have on the boat. Quite an achievement for moving one plug and writing a little bit of PHP code.

The code really is very simple - it reads an entire line from the serial, detects any new clients and then forwards it to all TCP clients that have connected to it. Then loop. As a bonus it will cache the last received 200 AIS messages and send those immediately to any client that connects. This in the hope that this contains a number of VDM type 5 and 24 messages so that the client can show static AIS data immediately.

You can download the nmea_to_tcp script here. I'm releasing this under the GPL v3.

To use this code you must have a computer that has a PHP5 CGI interpreter. On Microsoft Windows you may want to remove the first line of the nmea_to_tcp script, as this instructs a Linux system to use the PHP interpreter. You may need to install the DIO direct I/O package.

Connect you AIS receiver to a serial port, and then start the script with the name of that serial port as it's first argument, for instance nmea_to_tcp /dev/ttyUSB0 on Linux; it is probably something like nmea_to_tcp //./com1 on Microsoft Windows.

That's all. You should now be able to connect to the system at TCP port 2000.

Nobeltec and TCP streams

By the way, to get Nobeltec to use the TCP stream -- you can't rely on the built-in Glass bridge functionality. You have to convert the TCP stream back to serial data, for instance using the open source com0com and com2tcp software.


  1. If anyone gets this working on Windows 7 let me know what you did. when I run it I just get an error that it can not connect to the port. I am running an actisene gateway and an maretron gateway on separate com ports. I can see both devices in the Actisense NMEA reader, so I know they're working.

    Hmm... mabe its because my NMEA is N2K.. not sure.

  2. I'm not sure about the Maretron gateway, but the Actisense gateway certainly emulates a serial port. So you're doing something wrong. I don't know what yet, will have to set up a Windows VM to replicate.

    Note that you are right in thinking that this does NOTHING for a N2K environment, this is strictly NMEA0183HS. For now you can use my packetlogger program to translate NMEA2000 into JSON packets that you could then pipe into this program, but that's a BYO process.

    In future I might build a translator from NMEA2000-in-JSON to NMEA0183 :-) and then we can string all these programs together to build a NMEA2000 + NMEA0183 TCP/IP gateway.

  3. Hi, i'm a novice regarding pc comms.
    Can I use software Serial Stream UDP Multicast (pcmiro.com)to remote AIS tracking? I want to put up a remote AIS receiver site.

  4. @MasterGyro -- yes you can. I suggest you also read this page: Cover your Area on the Live map at marinetraffic.com.