mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-07 16:06:10 +00:00
readme with sync + async read
This commit is contained in:
parent
6d3ed12ca8
commit
a954db1b94
17
README.md
17
README.md
@ -1,3 +1,4 @@
|
|||||||
|
[](https://github.com/mik3y/usb-serial-for-android/actions)
|
||||||
[](https://jitpack.io/#mik3y/usb-serial-for-android)
|
[](https://jitpack.io/#mik3y/usb-serial-for-android)
|
||||||
[](https://www.codacy.com/manual/kai-morich/usb-serial-for-android-mik3y?utm_source=github.com&utm_medium=referral&utm_content=mik3y/usb-serial-for-android&utm_campaign=Badge_Grade)
|
[](https://www.codacy.com/manual/kai-morich/usb-serial-for-android-mik3y?utm_source=github.com&utm_medium=referral&utm_content=mik3y/usb-serial-for-android&utm_campaign=Badge_Grade)
|
||||||
[](https://codecov.io/gh/mik3y/usb-serial-for-android)
|
[](https://codecov.io/gh/mik3y/usb-serial-for-android)
|
||||||
@ -52,6 +53,7 @@ to your project's `res/xml/` directory and configure in your `AndroidManifest.xm
|
|||||||
|
|
||||||
**3.** Use it! Example code snippet:
|
**3.** Use it! Example code snippet:
|
||||||
|
|
||||||
|
open device:
|
||||||
```java
|
```java
|
||||||
// Find all available drivers from attached devices.
|
// Find all available drivers from attached devices.
|
||||||
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||||
@ -71,18 +73,25 @@ to your project's `res/xml/` directory and configure in your `AndroidManifest.xm
|
|||||||
UsbSerialPort port = driver.getPorts().get(0); // Most devices have just one port (port 0)
|
UsbSerialPort port = driver.getPorts().get(0); // Most devices have just one port (port 0)
|
||||||
port.open(connection);
|
port.open(connection);
|
||||||
port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
|
port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
|
||||||
|
```
|
||||||
|
then use direct read/write
|
||||||
|
```java
|
||||||
|
port.write(request, WRITE_WAIT_MILLIS);
|
||||||
|
len = port.read(response, READ_WAIT_MILLIS);
|
||||||
|
```
|
||||||
|
or direct write + event driven read:
|
||||||
|
```java
|
||||||
usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
|
usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
|
||||||
Executors.newSingleThreadExecutor().submit(usbIoManager);
|
Executors.newSingleThreadExecutor().submit(usbIoManager);
|
||||||
```
|
...
|
||||||
```java
|
|
||||||
port.write("hello".getBytes(), WRITE_WAIT_MILLIS);
|
port.write("hello".getBytes(), WRITE_WAIT_MILLIS);
|
||||||
```
|
|
||||||
```java
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewData(byte[] data) {
|
public void onNewData(byte[] data) {
|
||||||
runOnUiThread(() -> { textView.append(new String(data)); });
|
runOnUiThread(() -> { textView.append(new String(data)); });
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
and finally:
|
||||||
```java
|
```java
|
||||||
port.close();
|
port.close();
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user