private native static FileDescriptor open(String path, int baudrate, int flags); public native void close(); 很显然一个是打开串口 一个是 关闭串口 方法 打开串口之前,程序需要获得最高权限,SerialPort.java的构造函数里面需要获得设备的超级root权限,也是通过输入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 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(); } }
static { System.loadLibrary("serial_port"); }
public SerialHelper(String sPort, int iBaudRate) { this.sPort = "/dev/ttyS3"; this.iBaudRate = 9600; this._isOpen = false; this._bLoopData = new byte[]{48}; this.iDelay = 500; this.sPort = sPort; this.iBaudRate = iBaudRate; }
public void open() throws SecurityException, IOException, InvalidParameterException { this.mSerialPort = new SerialPort(new File(this.sPort), this.iBaudRate, 0); this.mOutputStream = this.mSerialPort.getOutputStream(); this.mInputStream = this.mSerialPort.getInputStream(); this.mReadThread = new SerialHelper.ReadThread(); this.mReadThread.start(); this.mSendThread = new SerialHelper.SendThread(); this.mSendThread.setSuspendFlag(); this.mSendThread.start(); this._isOpen = true; } // 关闭线程 释放函数 public void close() { if (this.mReadThread != null) { this.mReadThread.interrupt(); } if (this.mSerialPort != null) { this.mSerialPort.close(); this.mSerialPort = null; } this._isOpen = false; }
private class SendThread extends Thread { public boolean suspendFlag; private SendThread() { this.suspendFlag = true; } public void run() { super.run(); while(!this.isInterrupted()) { synchronized(this) { while(this.suspendFlag) { try { this.wait(); } catch (InterruptedException var5) { var5.printStackTrace(); } } } SerialHelper.this.send(SerialHelper.this.getbLoopData()); try { Thread.sleep((long)SerialHelper.this.iDelay); } catch (InterruptedException var4) { var4.printStackTrace(); } } } public void setSuspendFlag() { this.suspendFlag = true; } public synchronized void setResume() { this.suspendFlag = false; this.notify(); } }
private class ReadThread extends Thread { private ReadThread() { } public void run() { super.run(); while(!this.isInterrupted()) { try { if (SerialHelper.this.mInputStream == null) { return; } byte[] buffer = new byte[512]; int size = SerialHelper.this.mInputStream.read(buffer); if (size > 0) { ComBean ComRecData = new ComBean(SerialHelper.this.sPort, buffer, size); SerialHelper.this.onDataReceived(ComRecData); } } catch (Throwable var4) { Log.e("error", var4.getMessage()); return; } } } }
serialPortFinder = new SerialPortFinder(); serialHelper = new SerialHelper() { @Override protected void onDataReceived(final ComBean comBean) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getBaseContext(), FuncUtil.ByteArrToHex(comBean.bRec), Toast.LENGTH_SHORT).show(); logListAdapter.addData(comBean.sRecTime+": "+FuncUtil.ByteArrToHex(comBean.bRec)); recy.smoothScrollToPosition(logListAdapter.getData().size()); } }); } };
final String[] ports = serialPortFinder.getAllDevicesPath(); final String[] botes = new String[]{"0", "50", "75", "110", "134", "150", "200", "300", "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "460800", "500000", "576000", "921600", "1000000", "1152000", "1500000", "2000000", "2500000", "3000000", "3500000", "4000000"}; SpAdapter spAdapter = new SpAdapter(this); spAdapter.setDatas(ports); spSerial.setAdapter(spAdapter); SpAdapter spAdapter2 = new SpAdapter(this); spAdapter2.setDatas(botes); spBote.setAdapter(spAdapter2)
btOpen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { serialHelper.open(); btOpen.setEnabled(false); } catch (IOException e) { e.printStackTrace(); } } });
if (serialHelper.isOpen()) { serialHelper.sendTxt(edInput.getText().toString()); } else { Toast.makeText(getBaseContext(), "搞毛啊,串口都没打开", Toast.LENGTH_SHORT).show(); }
if (serialHelper.isOpen()) { serialHelper.sendHex(edInput.getText().toString()); } else { Toast.makeText(getBaseContext(), "搞毛啊,串口都没打开", Toast.LENGTH_SHORT).show(); }
@Override protected void onDestroy() { super.onDestroy(); serialHelper.close(); }
标签: #串口调试助手#