I got tired of the problems with the various serial server solutions for Flash, so I wrote this Processing sketch to do the job. It’s functional, but I still need to tweak it a bit. But it will allow you to connect a Flash movie to a serial port by making an XML socket to localhost in order to access the serial port. Sample test ActionScript code is at the bottom.
Thanks to Shawn van Every for the latest updates, and Actionscript 3 code.
/*
Serial Server
language: processing
This program makes a connection between a serial port
and a network socket.
created 26 Mar. 2007
updated 9 Mar. 2010
by Tom Igoe
*/
import processing.serial.*;
import processing.net.*;
int socketNumber = 9001; // the port the server listens on
Server myServer; // the server
Client thisClient; // the reference to the client that logs
char terminationString = '\0'; // the string that the client sends
// to cause the server to disconnect
// into the server
Serial myPort; // the serial port you're using
String portnum; // name of the serial port
String outString = ""; // the string being sent out the serial port
String inString = ""; // the string coming in from the serial port
String socketString = ""; // string of bytes in from the socket
int receivedLines = 0; // how many lines have been received in the serial port
int bufferedLines = 5; // number of incoming lines to keep
// Flash asks for a security policy before
// allowing the data to flow freely back and forth:
String policyFile = "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + socketNumber + "\" /></cross-domain-policy>";
// Counter for number of request from Flash
int numberOfRequests = 0;
void setup() {
size(400, 300); // window size
// create a font with the second font available to the system:
PFont myFont = createFont(PFont.list()[2], 14);
textFont(myFont);
// list all the serial ports:
println(Serial.list());
// based on the list of serial ports printed from the
//previous command, change the 0 to your port's number:
portnum = Serial.list()[0];
// initialize the serial port:
myPort = new Serial(this, portnum, 9600);
// buffer until a newLine:
myPort.bufferUntil('n');
// start the server:
myServer = new Server(this, socketNumber); // Starts a server
}
void draw() {
// clear the screen:
background(0);
// print the name of the serial port:
text("Serial port: " + portnum, 10, 20);
// Print out what you get:
text("From serial port:n" + inString, 10, 80);
text("From socket:n" + socketString, 200, 80);
// if the client is not null, and says something, display what it said:
if (thisClient !=null) {
// print out the current client:
text("Active client: " + thisClient.ip(), 10, 60);
// read what the client said:
String whatClientSaid = thisClient.readString();
if (whatClientSaid != null) {
// save what it said to print to the screen:
socketString = whatClientSaid;
// send what it said out the serial port:
myPort.write(socketString);
}
}
}
// this method runs when bytes show up in the serial port:
void serialEvent(Serial myPort) {
// read the String from the serial port:
String whatSerialSaid = myPort.readStringUntil('n');
if (whatSerialSaid != null) {
// save what it said to print to the screen:
inString = whatSerialSaid;
// if there is a netClient, send the serial stuff to them:
if (thisClient != null) {
// If first data from flash, send security policy
if (numberOfRequests == 0) {
thisClient.write(policyFile);
numberOfRequests++;
}
else {
// otherwise
// send the actual text string:
thisClient.write(inString);
} // send the actual text string:
thisClient.write(inString);
// add the end zero byte:
thisClient.write(terminationString);
}
}
}
void serverEvent(Server myServer, Client someClient) {
if (thisClient == null) {
// don't accept the client if we already have one:
thisClient = someClient;
}
}
Test ActionScript Code: Thanks to Shawn Van Every and Dan O’Sullivan for this code.
var socket:XMLSocket;
createSocket();
function createSocket() {
socket = new XMLSocket();
// define the functions that get called when these events happen:
socket.addEventListener(Event.CLOSE, closed);
socket.addEventListener(Event.CONNECT, success);
socket.addEventListener(DataEvent.DATA, newData);
socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
//127.0.0.1 is the same as "localhost"
// i.e. an alias to your local machine
socket.connect("127.0.0.1",9001);
}
function ioErrorHandler(e:IOError)
{
trace(e);
}
function securityErrorHandler(e:SecurityErrorEvent)
{
trace(e);
}
function success(e:Event) {
trace("socket opened");
socket.send("Fn");// tell proxy it is talking to flash
}
function closed(e:Event) {
trace("socket closed");
socket.send("Qn");// tell Proxy Flash is closing socket
}
function newData(e:DataEvent) {
trace(e);
var inString:String = e.data;
trace(inString);// trace the packet of data to the Flash output screen
}