public void sendArduinoCmd(String cmd) { | |
// Get UsbManager from Android. | |
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); | |
// Find the first available driver. | |
UsbSerialDriver driver = UsbSerialProber.acquire(manager); | |
try { | |
if (driver != null) { | |
driver.open(); | |
try { | |
driver.setBaudRate(9600); | |
byte buffer[] = new byte[2]; | |
byte[] cmd_array = cmd.getBytes(); | |
//buffer[0] = 'r'; | |
buffer[0] = cmd_array[0]; | |
Log.d("_dbg", "sending arduino command: " + buffer[0]); | |
buffer[1] = '\n'; | |
int numBytesWrite = driver.write(buffer, 1000); | |
Toast.makeText(getApplicationContext(), | |
"Read " + numBytesWrite + " bytes.", Toast.LENGTH_LONG).show(); | |
} catch (IOException e) { | |
// Deal with error. | |
Toast.makeText(getApplicationContext(), | |
"IOException occurred", Toast.LENGTH_LONG).show(); | |
} finally { | |
driver.close(); | |
} | |
} else { | |
Log.d("_dbg", "arduino driver null"); | |
} | |
} catch (IOException e) { | |
// Deal with error. | |
Toast.makeText(getApplicationContext(), | |
"IOException occurred 2", Toast.LENGTH_LONG).show(); | |
} | |
} |
送信者: xp_prg 送信: 2014年12月24日(水曜日) 11:21 件名: [Py4A] Adding usb host abilities to qpython on android to talk to arduino |