Channels ▼

Al Williams

Dr. Dobb's Bloggers

Serial Sockets

February 26, 2013

I mentioned last time that serial ports, while on the wane, are still around in one form or another, especially in the embedded world. Although official support in Java for the serial port is anemic, there is good third-party support and so if you want to use Java, you can use the serial port.

Where Java shines, of course, is network programming. Not only that, most languages have good support for networking thanks to the Internet fad (I'm kidding — even I don't think it's a fad). It might be nice if you could treat a serial port like a network socket. Then you could use a wide range of tools to access it. Besides that, if you could run a server that managed the socket, you could place the server and the serial port in one place and then access it from anywhere on the network.

Sounds like a good idea? It is such a good idea that there are several implementations of it and an RFC (RFC 2217). If you use Linux, there is a program called sredird that you can either build or get from most distributions. If you read the RFC, the protocol is essentially Telnet with extensions, so it is easy enough to access the port remotely. Barring that, Kermit has support for working as a client if you like.

There are plenty of other options. For example, ser2net and serialoverip are both on SourceForge. If you want a client in Java, check out jvser.

Another option is to use socat, which is a bit like a Swiss Army knife and will work as both client and server. For example, I have a 3D printer that has a virtual serial port connected to a remote computer. On that computer I run (all one line):

socat tcp-l:54321,fork,reuseaddr open:/dev/ttyACM0,raw,nonblock,waitlock=/tmp/acm0.locak,echo=0,b115200,crnl

This opens a TCP socket on 54321 and connects it to the serial port at /dev/ttyACM0 at 115,200 baud.

Assuming the remote machine is at address remote_computer, on my desktop computer I run:

sudo socat pty,link=/dev/ttyUSB99,echo=0,raw,crnl tcp:remote_computer:54321

This produces a virtual ttyUSB99 port and most software will treat it like a serial port. Of course, you can easily replace that with your own socket manipulation code, if you prefer.

If you are a Windows user, you might be more interested in com0com, which includes com2tcp that performs a similar function. The hub4com even supposedly supports RFC2217, although I haven't tried it.

With the preponderance of cheap Linux boards like the Raspberry Pi, standing up a little serial server to put a serial device on the network isn't a big deal. It is one more reason serial ports aren't dead quite yet.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Comments:



Video