我正在尝试通过串口将一串数据从 Android 应用程序发送到 Zigbee。
该应用程序将类似于 Play 商店中的串行 USB 终端应用程序。这个链接是https://play.google.com/store/apps/details?id=de.kai_morich.serial_usb_terminal&hl=en_IN
这是 USB Serail 应用程序与我的应用程序的任务比较
USB串口应用
- 能够检测 USB 并连接
- 发送 XCTU 应用程序生成的十六进制数据
- 获取发送数据的响应
我的申请
- 能够检测 USB 并连接
- 发送 XCTU 应用程序生成的十六进制数据
- 未收到回复
我无法发送十六进制数据,其代码是 获取USB设备并连接的代码
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
if (availableDrivers.isEmpty()) {
responseText.setText("No USB detected");
return;
}
UsbSerialPort port = null;
responseText.append("Device Count:"+availableDrivers.size());
// Open a connection to the first available driver.
UsbSerialDriver driver = availableDrivers.get(0);
UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
if (connection == null) {
// add UsbManager.requestPermission(driver.getDevice(), ..) handling here
return;
}
try {
port = driver.getPorts().get(0); // Most devices have just one port (port 0)
port.open(connection);
port.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
} catch (IOException ioe){
showToast("IO Exception at open");
try{
port.close();
} catch (IOException ioe2){
showToast("IO Exception at open disconnection");
}
}
// if device connected and open show write and read feature
if(port != null){
responseText.append("\n" + "Port"+port.toString());
showSerialConnected();
}
发送请求到串口
public void writeToDevice(){
serialInputOutputManager = new SerialInputOutputManager(port, mListener);
Executors.newSingleThreadExecutor().submit(serialInputOutputManager);
String data = requestEdit.getText().toString();
try{
port.write(data.getBytes(), 5000);
} catch(IOException ioe) {
responseText.append("\n IO Exception at write");
}
}
要发送的数据 7E 00 2B 10 01 00 13 A2 00 41 A7 A7 ED FF FE 00 00 24 7C 53 56 7C 31 31 7C 31 30 38 7C 31 33 30 33 32 30 31 30 30 33 30 36 7C 3 3 39 7C 23 2E
响应监听器
ExecutorService mExecutor;
SerialInputOutputManager serialInputOutputManager;
SerialInputOutputManager.Listener mListener;
mExecutor = Executors.newSingleThreadExecutor();
mListener = new SerialInputOutputManager.Listener() {
@Override
public void onRunError(Exception e) {
Log.d(TAG, "Runner stopped.");
}
@Override
public void onNewData(final byte[] data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String message = " bytes: \n"+ HexDump.dumpHexString(data);
responseText.append(message);
}
});
}
};
提前致谢
最佳答案
要隔离问题,您可以尝试 fork 并改编我的开源变体 https://github.com/kai-morich/SimpleUsbTerminal USB 串行应用程序
来找出您的实现的不同之处
关于android - 将数据发送到串行 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60680595/