Arduino and JavaProxy

via Dave Brink (dcb)

Overview

Building on the standalone java program: Arduino + Java , I wanted to take a stab at a proxy. Really I wanted to do python <-> arduino, but I lost the scent about 5 hyperlinks into researching pyserial (and countless other issues), and sockets is really more appropriate going forward in the general sense. So here is a Proxy.java class you can run and connect to. The default port is 8225, but you can change it in the arduino properties. Perhaps something like this could be integrated with the ide. Here is a thread with a python client example and arduino script. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230026454/0#0

Here's the code. Create and run the Proxy.java file like in the Arduino + Java Page:

/* simple Java serial port <-> tcp proxy */

import java.net.ServerSocket;

import java.net.Socket;

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;

import java.io.InputStream;

import java.io.OutputStream;

import processing.app.Preferences;

public class Proxy {

	static InputStream input;

	static OutputStream output;

	public static void main(String[] args) throws Exception {

		Preferences.init();

		String pp = Preferences.get("serial.proxyport");
		if (pp == null)
			pp = "8225";
		int pport = Integer.parseInt(pp);

		CommPortIdentifier portId = CommPortIdentifier
			.getPortIdentifier(Preferences.get("serial.port"));

		System.out.println("Serial Proxy Starting");
		System.out.println("Serial port: " + portId.getName()
				+ " TCP Port: " + pp);

		SerialPort port = (SerialPort) portId.open("serial madness", 4000);
		input = port.getInputStream();
		output = port.getOutputStream();
		port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"),
				SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
				SerialPort.PARITY_NONE);

		ServerSocket ss = new ServerSocket(pport);

		while (true) {
			try {
				Socket s = ss.accept();
				while (s.isConnected()) {
					while (input.available() > 0) {
						s.getOutputStream().write(input.read());
					}
					while (s.getInputStream().available() > 0) {
						output.write(s.getInputStream().read());
					}
					try {Thread.sleep(100);} catch (Exception e) {}
				}
			} catch (Throwable t) {}//just keep running
		}
	}

}

Share