forked from mik3y/usb-serial-for-android
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from adamantivm/streams
Implements Input/OutputStream helpers
- Loading branch information
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
UsbSerialLibrary/src/main/java/com/hoho/android/usbserial/stream/UsbSerialInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.hoho.android.usbserial.stream; | ||
|
||
import com.hoho.android.usbserial.driver.UsbSerialDriver; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* @author lucas@creativa77.com.ar (Lucas Chiesa) | ||
*/ | ||
class UsbSerialInputStream extends InputStream{ | ||
|
||
private final UsbSerialDriver device; | ||
private final UsbSerialStreamFactory factory; | ||
|
||
UsbSerialInputStream(UsbSerialDriver device, UsbSerialStreamFactory factory) { | ||
this.device = device; | ||
this.factory = factory; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
device.close(); | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int read(byte[] buffer) throws IOException { | ||
return read(buffer, 0, buffer.length); | ||
} | ||
|
||
@Override | ||
public int read(byte[] buffer, int offset, int count) throws IOException { | ||
byte[] local_buffer = new byte[count]; | ||
if (offset < 0 || count < 0 || offset + count > buffer.length) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
int byteCount; | ||
byteCount = device.read(local_buffer, count); | ||
System.arraycopy(local_buffer, 0, buffer, offset, byteCount); | ||
return byteCount; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
UsbSerialLibrary/src/main/java/com/hoho/android/usbserial/stream/UsbSerialOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.hoho.android.usbserial.stream; | ||
|
||
import com.hoho.android.usbserial.driver.UsbSerialDriver; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* @author lucas@creativa77.com.ar (Lucas Chiesa) | ||
*/ | ||
class UsbSerialOutputStream extends OutputStream { | ||
|
||
private final UsbSerialDriver driver; | ||
private final UsbSerialStreamFactory factory; | ||
|
||
UsbSerialOutputStream(UsbSerialDriver driver, UsbSerialStreamFactory factory) { | ||
this.driver = driver; | ||
this.factory = factory; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
driver.close(); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
} | ||
|
||
@Override | ||
public void write(byte[] buffer, int offset, int count) throws java.io.IOException { | ||
if (offset < 0 || count < 0 || offset + count > buffer.length) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
byte[] local = new byte[count]; | ||
System.arraycopy(buffer, offset, local, 0, count); | ||
this.driver.write(local, 1000); | ||
} | ||
|
||
@Override | ||
public void write(int oneByte) throws IOException { | ||
write(new byte[] { (byte) oneByte }, 0, 1); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
UsbSerialLibrary/src/main/java/com/hoho/android/usbserial/stream/UsbSerialStreamFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.hoho.android.usbserial.stream; | ||
|
||
import com.hoho.android.usbserial.driver.UsbSerialDriver; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* This helper class encapsulates a given <code>UsbSerialDriver</code> with implementations of | ||
* <code>java.io.InputStream</code> and <code>java.io.OutputStream</code>. | ||
* | ||
* @author jcerruti@creativa77.com.ar (Julian Cerruti) | ||
*/ | ||
public class UsbSerialStreamFactory { | ||
private final UsbSerialDriver driver; | ||
|
||
private final int baudRate; | ||
private final int dataBits; | ||
private final int stopBits; | ||
private final int parity; | ||
|
||
public UsbSerialStreamFactory(UsbSerialDriver driver, int baudRate, int dataBits, int stopBits, int parity) throws IOException { | ||
this.driver = driver; | ||
this.baudRate = baudRate; | ||
this.dataBits = dataBits; | ||
this.stopBits = stopBits; | ||
this.parity = parity; | ||
|
||
driver.open(); | ||
driver.setParameters(baudRate, dataBits, stopBits, parity); | ||
} | ||
|
||
/** | ||
* Uses the following default values to open the underlying serial connection: | ||
* - baud rante: 115200 | ||
* - data bits: 8 | ||
* - stop bits: 1 | ||
* - parity: none | ||
*/ | ||
public UsbSerialStreamFactory(UsbSerialDriver driver) throws IOException { | ||
this(driver, 115200, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE); | ||
} | ||
|
||
/** | ||
* Closes the underlying driver's serial connection | ||
*/ | ||
public void close() throws IOException { | ||
driver.close(); | ||
} | ||
|
||
public InputStream getInputStream() { | ||
return new UsbSerialInputStream(this.driver, this); | ||
} | ||
public OutputStream getOutputStream() { | ||
return new UsbSerialOutputStream(this.driver, this); | ||
} | ||
|
||
public int getStopBits() { | ||
return stopBits; | ||
} | ||
|
||
public int getParity() { | ||
return parity; | ||
} | ||
|
||
public int getDataBits() { | ||
return dataBits; | ||
} | ||
|
||
public int getBaudRate() { | ||
return baudRate; | ||
} | ||
} |