Created
March 18, 2018 13:49
-
-
Save nus/1389e0a8a58e0e21c923bf9de0b1ef24 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.example.urgcapturer | |
import android.content.Context | |
import android.hardware.usb.UsbManager | |
import android.util.Log | |
import com.hoho.android.usbserial.driver.CdcAcmSerialDriver | |
import com.hoho.android.usbserial.driver.ProbeTable | |
import com.hoho.android.usbserial.driver.UsbSerialPort | |
import com.hoho.android.usbserial.driver.UsbSerialProber | |
import com.kristou.urgLibJ.Connection.Connection | |
import java.io.IOException | |
import java.util.* | |
class AndroidSerialConnection(context: Context) : Connection { | |
companion object { | |
val TAG: String = AndroidSerialConnection::class.java.simpleName | |
val TIMEOUT_MILLIS = 1000 | |
} | |
private val _context: Context = context | |
private var _errorMessage: String = "" | |
private var _connected: Boolean = false | |
private var _deviceSpeed = 19200 | |
private var _deviceName: String = "" | |
private lateinit var _serialPort: UsbSerialPort | |
private var _receiveBuffer = Vector<Byte>() | |
override fun what(): String { | |
Log.d(TAG, "what") | |
return _errorMessage | |
} | |
override fun connect(device: String?, baudrate: Int): Boolean { | |
Log.d(TAG, "connect") | |
if (device == null) { | |
_connected = false | |
return false | |
} | |
_deviceName = device | |
_deviceSpeed = baudrate | |
val usbManager = _context.getSystemService(Context.USB_SERVICE) as UsbManager | |
val customTable = ProbeTable() | |
customTable.addProduct(0x15d1, 0x0000, CdcAcmSerialDriver::class.java) // URG-04LX-UG01 | |
val prober = UsbSerialProber(customTable) | |
val drivers = prober.findAllDrivers(usbManager) | |
if (drivers.isEmpty()) { | |
Log.e(TAG, "There are no devices.") | |
_errorMessage = "There are no devices." | |
_connected = false | |
return false | |
} | |
val driver = drivers[0] | |
val connection = usbManager.openDevice(driver.device) | |
if (connection == null) { | |
Log.e(TAG, "There is no connection.") | |
_errorMessage = "There is no connection." | |
_connected = false | |
return false | |
} | |
val port = driver.ports[0] | |
if (port == null) { | |
_connected = false | |
return false | |
} | |
try { | |
port.open(connection) | |
port.setParameters(_deviceSpeed, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE); | |
_serialPort = port | |
_connected = true | |
return true | |
} catch (e: IOException) { | |
e.printStackTrace() | |
port.close() | |
_connected = false | |
return false | |
} | |
} | |
override fun connect(device: String?): Boolean { | |
Log.d(TAG, "connect") | |
return connect(device, _deviceSpeed) | |
} | |
override fun disconnect(): Boolean { | |
Log.d(TAG, "disconnect") | |
var ret = true | |
if (_serialPort == null) { | |
return ret | |
} | |
try { | |
_serialPort.close() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
ret = false | |
} | |
_connected = false | |
return ret | |
} | |
override fun setBaudrate(baud: Int): Boolean { | |
Log.d(TAG, "setBaudrate: " + baud) | |
_deviceSpeed = baud | |
disconnect() | |
return connect(_deviceName, _deviceSpeed) | |
} | |
override fun getBaudrate(): Int { | |
Log.d(TAG, "getBaudrate") | |
return _deviceSpeed | |
} | |
override fun isConnected(): Boolean { | |
return _connected | |
} | |
override fun send(data: String?, count: Int): Boolean { | |
Log.d(TAG, "send1: " + data + "count: " + count) | |
var success = false | |
if (isConnected) { | |
try { | |
_serialPort.write(data?.toByteArray(Charsets.UTF_8), TIMEOUT_MILLIS) | |
success = true | |
} catch (e: IOException) { | |
e.printStackTrace() | |
disconnect() | |
} | |
} | |
return success | |
} | |
override fun send(data: String?): Boolean { | |
Log.d(TAG, "send2: " + data) | |
var success = false | |
if (isConnected) { | |
try { | |
val bytes = data?.toByteArray(Charsets.UTF_8) | |
if (bytes == null) { | |
Log.e(TAG, "Failed to toByteArray.") | |
return false | |
} | |
val count = bytes.count() | |
val len = _serialPort.write(bytes, TIMEOUT_MILLIS) | |
if (len != bytes.size) { | |
Log.e(TAG, "Failed to _serialPort.write.") | |
} | |
success = true | |
} catch (e: IOException) { | |
e.printStackTrace() | |
disconnect() | |
} | |
} | |
return success | |
} | |
override fun receive(count: Int, timeout: Int): String { | |
// Log.d(TAG, "receive: " + count + " timeout: " + timeout) | |
val startTime = System.currentTimeMillis() | |
if (!isConnected) { | |
_errorMessage = "Error, disconnected" | |
// Log.e(TAG, "Error, disconnected") | |
return "" | |
} | |
val buffer = ByteArray(1024) | |
var result = "" | |
var readBytes = 0 | |
try { | |
while (readBytes <= count) { | |
if (_receiveBuffer.size > 0) { | |
result += _receiveBuffer.elementAt(0).toChar() | |
_receiveBuffer.removeElementAt(0) | |
readBytes++ | |
if (readBytes == count) { | |
return result | |
} | |
} | |
val len = _serialPort.read(buffer, timeout) | |
for (i in 0 until len) { | |
_receiveBuffer.add(buffer[i]) | |
} | |
while (_receiveBuffer.size > 0) { | |
result += _receiveBuffer.elementAt(0).toChar() | |
_receiveBuffer.removeElementAt(0) | |
readBytes++ | |
if (readBytes == count) { | |
return result | |
} | |
} | |
val duration = System.currentTimeMillis() - startTime | |
if (duration > timeout) { | |
break | |
} | |
} | |
Log.d(TAG, "result2: " + result) | |
return result | |
} catch (e: IOException) { | |
_errorMessage = "Failed to read." | |
Log.e(TAG, "Failed to read.") | |
return "" | |
} | |
} | |
override fun flush() { | |
Log.d(TAG, "flush") | |
_serialPort.purgeHwBuffers(true, true) | |
} | |
override fun clear() { | |
Log.d(TAG, "clear") | |
_serialPort.purgeHwBuffers(true, false) | |
} | |
override fun type(): Connection.ConnectionType { | |
Log.d(TAG, "type") | |
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
override fun ungetc(c: Char) { | |
Log.d(TAG, "ungetc") | |
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
val dummy = ByteArray(1) | |
_serialPort.read(dummy, 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment