dependencies {
implementation 'com.hd:usbserialport:last-version'
}
注意:usbserialport只实现了简单的读写,更深度化的使用可查看 usb-serial-port-measure
public class AIOApp extends Application {
@Override
public void onCreate() {
super.onCreate();
DeviceMeasureController.init(this,BuildConfig.DEBUG);
}
}
DeviceMeasureController.INSTANCE.scanSerialPort();//扫描串口
DeviceMeasureController.INSTANCE.scanUsbPort();//扫描usb转串口
//测量所有设备
@Override
protected void onResume() {
super.onResume();
DeviceMeasureController.INSTANCE.measure(DeviceMeasureController.INSTANCE.scanUsbPort(),new UsbMeasureParameter(), new UsbMeasureListener() {
@Override
public void measuring(@Nullable Object tag,UsbSerialPort usbSerialPort, byte[] data) {
//处理返回数据
}
@Override
public void write(@Nullable Object tag,UsbSerialPort usbSerialPort) {
//允许持续性写入数据
try {
usbSerialPort.write(new byte[]{(byte) 0xff, (byte) 0xff},1000);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void measureError(@Nullable Object tag,String message) {
}
});
}
//测量单个目标设备
@Override
protected void onResume() {
super.onResume();
//根据实际选择情况
UsbSerialPort port=DeviceMeasureController.INSTANCE.scanUsbPort().get(0).getPorts().get(0);
DeviceMeasureController.INSTANCE.measure(port, new UsbMeasureParameter(115200,8,1,0), new UsbMeasureListener() {
@Override
public void measuring(@Nullable Object tag,UsbSerialPort usbSerialPort, byte[] data) {
//处理返回数据
}
@Override
public void write(@Nullable Object tag,UsbSerialPort usbSerialPort) {
//允许持续性写入数据
try {
usbSerialPort.write(new byte[]{(byte) 0xff, (byte) 0xff},1000);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void measureError(@Nullable Object tag,String message) {
}
});
}
//测量所有设备
@Override
protected void onResume() {
super.onResume();
DeviceMeasureController.INSTANCE.measure(null, new SerialPortMeasureParameter(), new SerialPortMeasureListener() {
@Override
public void measuring(@Nullable Object tag,String path, byte[] data) {
//处理返回数据
}
@Override
public void write(@Nullable Object tag,OutputStream outputStream) {
//允许持续性写入数据
try {
outputStream.write(new byte[]{(byte) 0xff, (byte) 0xff});
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void measureError(@Nullable Object tag,String message) {
}
});
}
//测量单个目标设备
@Override
protected void onResume() {
super.onResume();
String targetDevicePath="/dev/ttyS3";
DeviceMeasureController.INSTANCE.measure(new SerialPortMeasureParameter(targetDevicePath,115200,0), new SerialPortMeasureListener() {
@Override
public void measuring(@Nullable Object tag,String path, byte[] data) {
//处理返回数据
}
@Override
public void write(@Nullable Object tag,OutputStream outputStream) {
//允许持续性写入数据
try {
outputStream.write(new byte[]{(byte) 0xff, (byte) 0xff});
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void measureError(@Nullable Object tag,String message) {
}
});
}
DeviceMeasureController.INSTANCE.write()
DeviceMeasureController.INSTANCE.stop()
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.