android虚拟机串口使用问题
首先我使用虚拟串口软件在windows上虚拟了两个串口COM5<--->COM6 两个虚拟串口可以互相收发
使用windows的串口5启动虚拟机
emulator @Nexus_5X_API_23 -qemu -serial COM5
使用adb shell 命令进入命令行
chmod 777 /dev/ttyS1
echo "test" >/dev/ttyS1
然后可以在COM6收到test字符
问题有两个:
1. 虽然已经用chmod 修改了ttyS1的权限,但是device.canRead()仍然返回false
2. 在虚拟机上执行这个命令就出现异常了,程序异常退出
su = Runtime.getRuntime().exec("/system/bin/su");
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 777 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}