Created
March 11, 2014 15:39
-
-
Save AxGord/9488342 to your computer and use it in GitHub Desktop.
TCP-IP <-> Com port bridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | |
*/ | |
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