Android 读写串口

1.  使用svn从  http://android-serialport-api.googlecode.com/svn/trunk/android-serialport-api check_out工程。

 2.  将android-serialport-api 工程的libserial_port.so拷贝到自己的工程目录。

 3.  在工程中创建 android_serialport_api 包,并将android-serialport-api 工程的SerialPort.java 和 SerialPortFinder.java 拷贝到android_serialport_api 包。


 4.  在Application中添加如下代码:
      public SerialPort getSerialPort() throws SecurityException, IOException, InvalidParameterException {

if (mSerialPort == null) {

/* Read serial port parameters */

String path = "/dev/ttyS0";

int baudrate = 115200;

/* Check parameters */

if ( (path.length() == 0) || (baudrate == -1)) {

throw new InvalidParameterException();

}

/* Open the serial port */

mSerialPort = new SerialPort(new File(path), baudrate, 0);

}

return mSerialPort;

}

public void closeSerialPort() {

if (mSerialPort != null) {

mSerialPort.close();

mSerialPort = null;

}

}

 5.  将 SerialPort.java 类中的 su = Runtime.getRuntime().exec("/system/bin/su");  改成 su = Runtime.getRuntime().exec("/system/xbin/su");

 6.  写入串口数据测试
      XXApplication app = (XXApplication)this.getApplication().

      OutputStream mOutputStream = app.getSerialPort().getOutputStream(); 
      mOutputStream.write(bttotal); 

猜你喜欢

转载自blog.csdn.net/shenxingshu/article/details/89337581