去年我写了一个名为Disco Desktop的应用程序,它使用TinkerProxy允许AIR通过USB串口与Arduino通信。我的TinkerProxy
和TinkerProxyEvent
类如下所示。
该应用程序捆绑了serproxy,并使用本地进程API调用它,因此有两个不同的安装程序 - Mac OS X和Windows。TinkerProxy.as
扩展了Socket,在运行时根据用户输入编写serproxy配置文件,并基于此配置启动serproxy作为后台进程。终端/cmd窗口从未可见。
注意:请调用open()
而不是connect()
。安装程序中包含Arduino的简单设备原理图和草图。
TinkerProxy
:
package com.mattie.net
private function init():void
else if (Capabilities.os.toLowerCase().indexOf("mac") > -1)
else
}
public function open(
serialPort:String,
baudRate:uint,
networkAddress:String = "127.0.0.1",
networkPort:uint = 5331,
loadDelay:uint = 1000,
initializeDelay:uint = 2000,
comDatabits:uint = 8,
comStopbits:uint = 1,
proxyTimeout:uint = 63115200
)
this.serialPort = serialPort;
this.baudRate = baudRate;
this.networkAddress = networkAddress;
this.networkPort = networkPort;
this.loadDelay = loadDelay;
this.initializeDelay = initializeDelay;
this.comDatabits = comDatabits;
this.comStopbits = comStopbits;
this.proxyTimeout = proxyTimeout;
writeConfigStream = new FileStream();
writeConfigStream.addEventListener(Event.CLOSE, launchTinkerProxyApplication);
writeConfigStream.addEventListener(IOErrorEvent.IO_ERROR, IOErrorEventHandler);
writeConfigStream.openAsync(tinkerProxyConfigurationFile, FileMode.WRITE);
writeConfigStream.writeUTFBytes("serial_device1=" + serialPort + File.lineEnding);
writeConfigStream.writeUTFBytes("comm_ports=1" + File.lineEnding);
writeConfigStream.writeUTFBytes("net_port1=" + networkPort + File.lineEnding);
writeConfigStream.writeUTFBytes("newlines_to_nils=false" + File.lineEnding);
writeConfigStream.writeUTFBytes("comm_baud=" + baudRate + File.lineEnding);
writeConfigStream.writeUTFBytes("comm_databits=" + comDatabits + File.lineEnding);
writeConfigStream.writeUTFBytes("comm_stopbits=" + comStopbits+ File.lineEnding);
writeConfigStream.writeUTFBytes("comm_parity=none" + File.lineEnding);
writeConfigStream.writeUTFBytes("timeout=" + proxyTimeout + File.lineEnding);
writeConfigStream.close();
}
}
private function launchTinkerProxyApplication(evt:Event):void
var tinkerProxyProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
tinkerProxyProcessStartupInfo.executable = tinkerProxyApplication;
var processArguments:Vector.<String> = new Vector.<String>();
processArguments[0] = tinkerProxyConfigurationFile.nativePath;
tinkerProxyProcessStartupInfo.arguments = processArguments;
tinkerProxyProcess = new NativeProcess();
tinkerProxyProcess.start(tinkerProxyProcessStartupInfo);
loadDelayTimer = new Timer(loadDelay, 1);
loadDelayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, connectTinkerProxy);
loadDelayTimer.start();
}
private function connectTinkerProxy(evt:TimerEvent):void
catch(error:IOError)
catch(error:SecurityError)
}
private function initializeDelayTimerHandler(evt:Event):void
private function tinkerProxyConnectionComplete(evt:TimerEvent):void
override public function connect(host:String, port:int):void
override public function close():void
if (loadDelayTimer.running)
if (initializeDelayTimer.running)
}
super.close();
tinkerProxyProcess.exit(true);
tinkerProxyProcess = null;
dispatchEvent(new TinkerProxyEvent(TinkerProxyEvent.DISCONNECT));
connectedProperty = false;
removeEventListener(Event.CLOSE, connectionErrorEventHandler);
removeEventListener(IOErrorEvent.IO_ERROR, connectionErrorEventHandler);
removeEventListener(SecurityErrorEvent.SECURITY_ERROR, connectionErrorEventHandler);
}
private function connectionErrorEventHandler(evt:*):void
removeEventListener(Event.CLOSE, connectionErrorEventHandler);
removeEventListener(IOErrorEvent.IO_ERROR, connectionErrorEventHandler);
removeEventListener(SecurityErrorEvent.SECURITY_ERROR, connectionErrorEventHandler);
tinkerProxyProcess.exit(true);
tinkerProxyProcess = null;
}
private function IOErrorEventHandler(evt:IOErrorEvent):void
public function get systemIsWindows():Boolean
public function get opening():Boolean
override public function get connected():Boolean
}
}
TinkerProxyEvent
:
package com.mattie.events
{
import flash.events.Event;
public class TinkerProxyEvent extends Event
{
public static const LOADING:String = "Loading";
public static const INITIALIZING:String = "Initializing";
public static const CONNECT:String = "Connect";
public static const DISCONNECT:String = "Disconnect";
public static const ERROR:String = "Error";
public function TinkerProxyEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
}
}
}