模拟器可以使用PC的串口 您所在的位置:网站首页 雷电安卓模拟器可以运行exe文件吗 模拟器可以使用PC的串口

模拟器可以使用PC的串口

2024-07-16 07:52| 来源: 网络整理| 查看: 265

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();   }  }

}



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有