hqm
Posts: 1
Joined: Sun Feb 24, 2013 10:42 pm

TCP to serial port program

Sun Feb 24, 2013 10:53 pm

I would like to make a simple program which opens a TCP connection to a server, and then proxies data which comes over the TCP connection to the serial port, and vice versa (data coming in the serial port is sent out to the TCP stream).

I need to use the 'second serial port' (Raspberry Pi PL011 UART ) I think, because I need to use 5 data bits (I'm interfacing to an old Teletype, that does not use ASCII, it uses ITA2 a.k.a Baudot) at 45 baud.

Does anyone have any sample Java code for opening this serial port, and configuring it's baudrate and data bits and other params?

I know Java network programming pretty well, so can deal with the TCP stream side of things, but do not know any of the details of opening hardware serial port on the Raspberry Pi.

thanks

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: TCP to serial port program

Mon Feb 25, 2013 4:01 am

Hi hqm,
Try searching for examples using the RXTX library.
-Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
crackers
Posts: 55
Joined: Fri Dec 28, 2012 7:45 am

Re: TCP to serial port program

Mon Feb 25, 2013 5:56 am

The application you're describing already exists: ser2net I'm using it to connect my Pi via java.net.Socket to the serial/USB port on another machine.

As for direct serial access in Java, the guys behind RXTX have been doing a spectacular job for many years (used it myself). But I got tired of having to use a native library, so I use the above.

zui
Posts: 2
Joined: Wed Feb 13, 2013 9:53 pm

Re: TCP to serial port program

Mon Mar 11, 2013 11:36 am

Hi,

I used "/dev/ttyUSB0" as COM1 with the Digitus USB-To-Serial Adapter.

Code: Select all

System.setProperty("gnu.io.rxtx.SerialPorts", deviceId);
            Enumeration portList = CommPortIdentifier.getPortIdentifiers();
            while (portList.hasMoreElements()) {
                CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
                log.info("port id: " + portId.getName());
                try {
                    serialPort = (SerialPort) portId.open("net.wbz.moba.controlcenter", 2000);
                    try {
                        outputStream = serialPort.getOutputStream();
                        inputStream = serialPort.getInputStream();
                    } catch (IOException e) {
                    }

                    serialPort.setSerialPortParams(19200,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    log.info("connected to COM1");
                } catch (PortInUseException e) {
                    log.error("COM1 in use", e);
                } catch (UnsupportedCommOperationException e) {
                    log.error("can't connect COM1", e);
                }
System configuration:
http://www.raspberrypi.org/phpBB3/viewt ... 06#p287206

cootcraig
Posts: 15
Joined: Sun Jan 13, 2013 4:56 pm

Re: TCP to serial port program

Sun Apr 21, 2013 3:10 pm

I've found socat an easy way to talk to serial ports from Java.

http://www.dest-unreach.org/socat/

bullen
Posts: 283
Joined: Sun Apr 28, 2013 2:52 pm

Re: TCP to serial port program

Tue Apr 30, 2013 10:00 pm

crackers wrote:The application you're describing already exists: ser2net I'm using it to connect my Pi via java.net.Socket to the serial/USB port on another machine.

As for direct serial access in Java, the guys behind RXTX have been doing a spectacular job for many years (used it myself). But I got tired of having to use a native library, so I use the above.
How do you catch the TCP data on the other end?

I mean you setup this to send bytes to the serial interface (tcp -> serial)... but how do you get data out on the serial -> tcp end?
https://github.com/tinspin/rupy - A tiny Java async HTTP application server.

User avatar
crackers
Posts: 55
Joined: Fri Dec 28, 2012 7:45 am

Re: TCP to serial port program

Wed May 01, 2013 4:20 am

It's a bi-directional socket, so I just use InputStream and OutputStream from Socket. That's about as dirt-simple as it can get. You could get all fancy with nio, but consider that your code is much more likely to be faster than the hardware.

This is what my /etc/ser2net.conf looks like:

Code: Select all

4000:raw:0:/dev/ttyUSB0:19200 8DATABITS NONE 1STOPBIT

Return to “Java”