模拟器可以使用PC的串口
来源:互联网 发布:剑灵人女捏脸数据下载 编辑:程序博客网 时间:2024/06/05 06:36
1.模拟器可以使用PC的串口
启动模拟器并加载PC串口 命令如下:
运行 emulator @模拟器名称 -qemu -serial COM1
2.查看串口是否被加载
启动后使用 adb shell 命令打开命令行
cd dev 查看会发现ttyS0 ttyS1 ttyS2,其他ttyS2 就是我们加载上来的串口COM1
3.修改权限
chmod 777 ttyS2
现在我们可以开发串口程序了。
4.串口程序实例
下载libserial_port.so ,放入libs/armeabi 目录,可以自己创建此目录
libserial_port.so 下载地址:
http://code.google.com/p/android-serialport-api/
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Log;
public class SerialPort {
private static final String TAG = "SerialPort";
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
public SerialPort(File device, int baudrate) throws SecurityException, IOException {
if (!device.canRead() || !device.canWrite()) {
try {
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + 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();
}
}
mFd = open(device.getAbsolutePath(), baudrate);
if (mFd == null) {
Log.e(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
public InputStream getInputStream() {
return mFileInputStream;
}
public OutputStream getOutputStream() {
return mFileOutputStream;
}
//JNI
private native static FileDescriptor open(String path, int baudrate);
public native void close();
static {
System.loadLibrary("serial_port");
}
}
####################################
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* LINUX下串口打印接口
* @author douk
*
*/
public class PrintClass {
//输入流
private static InputStream in;
//输出流
private static OutputStream out;
private static final String PORT = "/dev/ttyS2";//串口
private SerialPort serialPort;
/**
* 连接串口
*/
private void Connect()
{
try {
serialPort = new SerialPort(new File(PORT), 38400);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭串口
* @throws IOException
*/
public void CloseSerialPort()
{
try {
out.close();
in.close();
serialPort.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
启动模拟器并加载PC串口 命令如下:
运行 emulator @模拟器名称 -qemu -serial COM1
2.查看串口是否被加载
启动后使用 adb shell 命令打开命令行
cd dev 查看会发现ttyS0 ttyS1 ttyS2,其他ttyS2 就是我们加载上来的串口COM1
3.修改权限
chmod 777 ttyS2
现在我们可以开发串口程序了。
4.串口程序实例
下载libserial_port.so ,放入libs/armeabi 目录,可以自己创建此目录
libserial_port.so 下载地址:
http://code.google.com/p/android-serialport-api/
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Log;
public class SerialPort {
private static final String TAG = "SerialPort";
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
public SerialPort(File device, int baudrate) throws SecurityException, IOException {
if (!device.canRead() || !device.canWrite()) {
try {
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + 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();
}
}
mFd = open(device.getAbsolutePath(), baudrate);
if (mFd == null) {
Log.e(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
public InputStream getInputStream() {
return mFileInputStream;
}
public OutputStream getOutputStream() {
return mFileOutputStream;
}
//JNI
private native static FileDescriptor open(String path, int baudrate);
public native void close();
static {
System.loadLibrary("serial_port");
}
}
####################################
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* LINUX下串口打印接口
* @author douk
*
*/
public class PrintClass {
//输入流
private static InputStream in;
//输出流
private static OutputStream out;
private static final String PORT = "/dev/ttyS2";//串口
private SerialPort serialPort;
/**
* 连接串口
*/
private void Connect()
{
try {
serialPort = new SerialPort(new File(PORT), 38400);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭串口
* @throws IOException
*/
public void CloseSerialPort()
{
try {
out.close();
in.close();
serialPort.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 模拟器可以使用PC的串口
- Android开发,模拟器使用PC的串口
- 总结android模拟器使用PC的串口调试程序的完整过程。
- Android模拟器使用PC端USB转串口调试
- android模拟器与pc间的串口通信
- 安卓-模拟器实现PC串口
- evc模拟器中串口的使用
- evc模拟器中串口的使用
- 模拟器上面运行的应用程序能从串口接收PC发的数据吗
- PC的默认串口
- 洋人太疯狂 -- 用 JavaScript 做的可以运行 Linux 的 PC 模拟器
- 串口基础-查找可以使用的串口-C#窗口应用程序
- 使用单片机测试PC或ARM的串口校验方式
- VMware9.0虚拟机Ubuntu使用PC的USB转串口
- 模拟器Active Sync同步使用pc网络
- 使用VSPM连接PC与串口服务器
- Pocket pc模拟器与PC机传输文件的方法
- Pocket PC 2003模拟器的汉化
- 在列表中添加值后,无法检索出来
- 关于cisco日志的配置
- Java基于XML+FreeMarker导出Word
- 从摄像头获得图片并存储感兴趣区变化大的图片
- php按照单词截取子串
- 模拟器可以使用PC的串口
- 计算某个文件夹所占的磁盘空间大小
- 【算法学习】线性时间排序-计数排序、基数排序和桶排序详解与编程实现
- Android ListView理解,BaseAdapter,ListView 的adapter用法和理解
- Struts2请求处理流程
- 浙大计算机研究生复试上机考试-2010年
- 【jmx学习】3、jmx之RMI Adaptor
- Android新开辟一个线程打开进度框
- MySql查询时间段