支持无阻塞,设置超时时间,可多个串口,一问一答同步响应
public class MySerialHelper extends SerialHelper {
private String TAG="MySerialHelper";
@Override
public String getPort() {
return "/dev/ttyS0";
}
@Override
public int getBaudRate() {
return 9600;
}
@Override
public int getTimeout() {
return 5*1000;
}
@Override
protected byte[] readCommand(byte[] command) throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean loop = true;
byte[] temp = new byte[1];
...
private fun initOkSerialPort(){
OkSerialPort.instance.init(MySerialHelper())
}
private fun send(ba: ByteArray){
OkSerialPort.instance.start(ba){
Log.e(TAG,"【----------start------------】")
setRecord(ba,true)
}.receive {
res->
setRecord(res,false)
}.onError {
e->
run {
when (e) {
is IOException -> toast("通信超时")
is ExecutionException -> toast("通信超时")
is TimeoutException -> toast("响应超时")
is InterruptedException -> toast("通信中断")
}
}
}.onComplete {
Log.e(TAG,"【----------onComplete------------】")
}.ok()
}
安装虚拟串口软件
安装串口调试助手
打开genymotion,直接跑代码