Skip to content

Instantly share code, notes, and snippets.

@AxGord
Created March 11, 2014 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AxGord/9488342 to your computer and use it in GitHub Desktop.
Save AxGord/9488342 to your computer and use it in GitHub Desktop.
TCP-IP <-> Com port bridge
package ;
import haxe.io.BytesInput;
import haxe.io.BytesOutput;
import pony.net.SocketClient;
import pony.net.SocketServer;
import pony.nodejs.SerialPort;
using pony.Tools;
/**
* Main
* @author AxGord <axgord@gmail.com>
*/
class Com {
public static var sp:SerialPort;
public static var server:SocketServer;
static function main() {
#if debug
untyped require('source-map-support').install();
#end
var comport = Sys.args()[2];
sp = new SerialPort(comport, {
baudrate: 4800
});
sp.open << open;
sp.data << comData;
server = new SocketServer(8080);
server.data << socketData;
}
static function socketData(b:BytesInput):Void {
var b = b.readAll();
trace('Data from socket: '+b.toHex());
var out = new BytesOutput();
out.write(b);
sp.write(out);
}
static function comData(b:BytesInput):Void {
var out = new BytesOutput();
for (_ in 0...b.length) {
var byte:Int = b.readByte();
trace('Data from com: ' + StringTools.hex(byte));
if (byte == 0x5A) {
sp.write([0xC3].toBytes());
} else {
out.writeByte(byte);
}
}
server.send(out);
}
static function open():Void {
trace('com opened');
/*
sp.write([0x0C, 0x66].toBytes());
sp.write([0x00].toBytes());
sp.write([0x0E, 0x63].toBytes());
sp.write([0x00].toBytes());
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment