如何使用 usb-serial-for-android 从连接到 Android 的 CDC 设备读取数据?
How to read for data from CDC device connected to Android using usb-serial-for-android?
我正在使用这个 library 从 Feather 32u4 设备发送和接收数据。 Feather 在端口打开时发送一些固定数据,然后回显发送给它的内容。
我在理解 read
和 write
方法在库中的工作方式时遇到问题。我很确定我使用 write
是正确的,但我不知道 read
。使用 read
时返回的长度始终为 0,我使用的是正确的波特率。我只是不太确定如何 使用它。我还在尝试找出事件驱动选项。
private void DoTheThing () {
String textMessage = "";
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
// Find available drivers for Feather board.
ProbeTable customTable = new ProbeTable();
customTable.addProduct(0x239a, 0x800c, CdcAcmSerialDriver.class);
UsbSerialProber prober = new UsbSerialProber(customTable);
List<UsbSerialDriver> drivers = prober.findAllDrivers(manager);
if (drivers.isEmpty()) {
textMessage += "Could not find any drivers.\n";
m_textView.setText(textMessage);
return;
}
textMessage += "Driver found\n";
m_textView.setText(textMessage);
UsbDeviceConnection connection = null;
UsbSerialDriver driver = drivers.get(0);
PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(INTENT_ACTION_GRANT_USB), 0);
manager.requestPermission(driver.getDevice(), usbPermissionIntent);
try {
connection = manager.openDevice(driver.getDevice());
} catch (Exception e) {
textMessage += e + "\n";
m_textView.setText(textMessage);
}
if (connection == null) {
// add UsbManager.requestPermission(driver.getDevice(), ..) handling here
textMessage += "Could not open device.\n";
m_textView.setText(textMessage);
return;
}
textMessage += "Device opened\n";
m_textView.setText(textMessage);
UsbSerialPort port = driver.getPorts().get(0);
// Most devices have just one port (port 0)
try {
port.open(connection);
port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
} catch (IOException e) {
textMessage += e + "\n";
m_textView.setText(textMessage);
return;
}
textMessage += "Connection established\n";
m_textView.setText(textMessage);
// Get received data on connection.
int length = -2;
byte[] receivedData = new byte[64];
try {
length = port.read(receivedData, 2000);
} catch (Exception e) {
textMessage += "Oops: " + e + "\n";
m_textView.setText(textMessage);
}
String data = new String(receivedData);
textMessage += "Data recieved: " + length + " " + data + "\n";
m_textView.setText(textMessage);
// Send canned data.
length = -2;
try {
length = port.write("a".getBytes(), 2000);
} catch (Exception e) {
textMessage += "Oops: " + e + "\n";
m_textView.setText(textMessage);
}
textMessage += "Data sent " + length + "\n";
m_textView.setText(textMessage);
// Get received data.
length = -2;
receivedData = new byte[64];
try {
length = port.read(receivedData, 2000);
} catch (Exception e) {
textMessage += "Oops: " + e + "\n";
m_textView.setText(textMessage);
}
data = new String(receivedData);
textMessage += "Data recieved: " + length + " " + data;
m_textView.setText(textMessage);
}
编辑
当我在我的端口上将 DTR 标志设置为 true 时发生严重崩溃。
m_port = driver.getPorts().get(0);
// Most devices have just one port (port 0)
try {
m_port.setDTR(true); // <-- hard crash
m_port.open(connection);
m_port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
} catch (IOException e) {
m_textMessage += e + "\n";
m_textView.setText(m_textMessage);
return;
}
难道m_port
到那个时候还没有初始化吗?
德普
当在 2000 毫秒超时内未收到任何内容时,长度为 0。
对于像 32u4 这样的 CDC 设备,您通常必须按照所述设置 DTR 线 here
我正在使用这个 library 从 Feather 32u4 设备发送和接收数据。 Feather 在端口打开时发送一些固定数据,然后回显发送给它的内容。
我在理解 read
和 write
方法在库中的工作方式时遇到问题。我很确定我使用 write
是正确的,但我不知道 read
。使用 read
时返回的长度始终为 0,我使用的是正确的波特率。我只是不太确定如何 使用它。我还在尝试找出事件驱动选项。
private void DoTheThing () {
String textMessage = "";
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
// Find available drivers for Feather board.
ProbeTable customTable = new ProbeTable();
customTable.addProduct(0x239a, 0x800c, CdcAcmSerialDriver.class);
UsbSerialProber prober = new UsbSerialProber(customTable);
List<UsbSerialDriver> drivers = prober.findAllDrivers(manager);
if (drivers.isEmpty()) {
textMessage += "Could not find any drivers.\n";
m_textView.setText(textMessage);
return;
}
textMessage += "Driver found\n";
m_textView.setText(textMessage);
UsbDeviceConnection connection = null;
UsbSerialDriver driver = drivers.get(0);
PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(INTENT_ACTION_GRANT_USB), 0);
manager.requestPermission(driver.getDevice(), usbPermissionIntent);
try {
connection = manager.openDevice(driver.getDevice());
} catch (Exception e) {
textMessage += e + "\n";
m_textView.setText(textMessage);
}
if (connection == null) {
// add UsbManager.requestPermission(driver.getDevice(), ..) handling here
textMessage += "Could not open device.\n";
m_textView.setText(textMessage);
return;
}
textMessage += "Device opened\n";
m_textView.setText(textMessage);
UsbSerialPort port = driver.getPorts().get(0);
// Most devices have just one port (port 0)
try {
port.open(connection);
port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
} catch (IOException e) {
textMessage += e + "\n";
m_textView.setText(textMessage);
return;
}
textMessage += "Connection established\n";
m_textView.setText(textMessage);
// Get received data on connection.
int length = -2;
byte[] receivedData = new byte[64];
try {
length = port.read(receivedData, 2000);
} catch (Exception e) {
textMessage += "Oops: " + e + "\n";
m_textView.setText(textMessage);
}
String data = new String(receivedData);
textMessage += "Data recieved: " + length + " " + data + "\n";
m_textView.setText(textMessage);
// Send canned data.
length = -2;
try {
length = port.write("a".getBytes(), 2000);
} catch (Exception e) {
textMessage += "Oops: " + e + "\n";
m_textView.setText(textMessage);
}
textMessage += "Data sent " + length + "\n";
m_textView.setText(textMessage);
// Get received data.
length = -2;
receivedData = new byte[64];
try {
length = port.read(receivedData, 2000);
} catch (Exception e) {
textMessage += "Oops: " + e + "\n";
m_textView.setText(textMessage);
}
data = new String(receivedData);
textMessage += "Data recieved: " + length + " " + data;
m_textView.setText(textMessage);
}
编辑
当我在我的端口上将 DTR 标志设置为 true 时发生严重崩溃。
m_port = driver.getPorts().get(0);
// Most devices have just one port (port 0)
try {
m_port.setDTR(true); // <-- hard crash
m_port.open(connection);
m_port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
} catch (IOException e) {
m_textMessage += e + "\n";
m_textView.setText(m_textMessage);
return;
}
难道m_port
到那个时候还没有初始化吗?
德普
当在 2000 毫秒超时内未收到任何内容时,长度为 0。
对于像 32u4 这样的 CDC 设备,您通常必须按照所述设置 DTR 线 here