Repository: jzt-Tesla/GoogleSerialPort Branch: master Commit: f75331c09d21 Files: 27 Total size: 28.6 KB Directory structure: gitextract_dqphpr97/ ├── .gitignore ├── LICENSE ├── MySerialPort/ │ ├── .gitignore │ ├── app/ │ │ ├── .gitignore │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src/ │ │ ├── androidTest/ │ │ │ └── java/ │ │ │ └── cn/ │ │ │ └── humiao/ │ │ │ └── myserialport/ │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main/ │ │ │ ├── AndroidManifest.xml │ │ │ ├── java/ │ │ │ │ ├── android_serialport_api/ │ │ │ │ │ ├── SerialPort.java │ │ │ │ │ └── SerialPortFinder.java │ │ │ │ └── cn/ │ │ │ │ └── humiao/ │ │ │ │ └── myserialport/ │ │ │ │ ├── Cmd.java │ │ │ │ ├── DataUtils.java │ │ │ │ ├── MainActivity.java │ │ │ │ └── SerialPortUtil.java │ │ │ └── res/ │ │ │ ├── layout/ │ │ │ │ └── activity_main.xml │ │ │ └── values/ │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test/ │ │ └── java/ │ │ └── cn/ │ │ └── humiao/ │ │ └── myserialport/ │ │ └── ExampleUnitTest.java │ ├── build.gradle │ ├── gradle/ │ │ └── wrapper/ │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── gradle.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *.iml .gradle /local.properties /.idea/workspace.xml /.idea/libraries .DS_Store /build MySerialPort/.idea/ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2018 白如白牙 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: MySerialPort/.gitignore ================================================ *.iml .gradle /local.properties /.idea/workspace.xml /.idea/libraries .DS_Store /build /captures .externalNativeBuild ================================================ FILE: MySerialPort/app/.gitignore ================================================ /build ================================================ FILE: MySerialPort/app/build.gradle ================================================ apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "cn.humiao.myserialport" minSdkVersion 14 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } sourceSets { main { jniLibs.srcDirs = ['libs'] } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'org.greenrobot:eventbus:3.0.0' } ================================================ FILE: MySerialPort/app/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in D:\AndroidSdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile ================================================ FILE: MySerialPort/app/src/androidTest/java/cn/humiao/myserialport/ExampleInstrumentedTest.java ================================================ package cn.humiao.myserialport; import android.content.Context; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.*; /** * Instrumentation test, which will execute on an Android device. * * @see Testing documentation */ @RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { @Test public void useAppContext() throws Exception { // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("cn.humiao.myserialport", appContext.getPackageName()); } } ================================================ FILE: MySerialPort/app/src/main/AndroidManifest.xml ================================================ ================================================ FILE: MySerialPort/app/src/main/java/android_serialport_api/SerialPort.java ================================================ package android_serialport_api; import android.util.Log; 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; /** * Google官方代码 * 此类的作用为,JNI的调用,用来加载.so文件的 * 获取串口输入输出流 */ public class SerialPort { private static final String TAG = "SerialPort"; /* * Do not remove or rename the field mFd: it is used by native method * close(); */ private FileDescriptor mFd; private FileInputStream mFileInputStream; private FileOutputStream mFileOutputStream; public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException { /* Check access permission */ 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(); } } System.out.println(device.getAbsolutePath() + "=============================="); mFd = open(device.getAbsolutePath(), baudrate, flags); if (mFd == null) { Log.e(TAG, "native open returns null"); throw new IOException(); } mFileInputStream = new FileInputStream(mFd); mFileOutputStream = new FileOutputStream(mFd); } // Getters and setters public InputStream getInputStream() { return mFileInputStream; } public OutputStream getOutputStream() { return mFileOutputStream; } // JNI private native static FileDescriptor open(String path, int baudrate, int flags); public native void close(); static { System.out.println("=============================="); System.loadLibrary("serial_port"); System.out.println("********************************"); } } ================================================ FILE: MySerialPort/app/src/main/java/android_serialport_api/SerialPortFinder.java ================================================ package android_serialport_api; import android.util.Log; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.util.Iterator; import java.util.Vector; /** * Google官方代码 * 此类的作用为,寻找得到有效的串口的物理地址。 * 如果你本身就知道串口的地址如:ttyS1、ttyS2,那么这个类就可以不用了。 * */ public class SerialPortFinder { public class Driver { public Driver(String name, String root) { mDriverName = name; mDeviceRoot = root; } private String mDriverName; private String mDeviceRoot; Vector mDevices = null; public Vector getDevices() { if (mDevices == null) { mDevices = new Vector(); File dev = new File("/dev"); File[] files = dev.listFiles(); int i; for (i=0; i mDrivers = null; Vector getDrivers() throws IOException { if (mDrivers == null) { mDrivers = new Vector(); LineNumberReader r = new LineNumberReader(new FileReader("/proc/tty/drivers")); String l; while((l = r.readLine()) != null) { // Issue 3: // Since driver name may contain spaces, we do not extract driver name with split() String drivername = l.substring(0, 0x15).trim(); String[] w = l.split(" +"); if ((w.length >= 5) && (w[w.length-1].equals("serial"))) { Log.d(TAG, "Found new driver " + drivername + " on " + w[w.length-4]); mDrivers.add(new Driver(drivername, w[w.length-4])); } } r.close(); } return mDrivers; } public String[] getAllDevices() { Vector devices = new Vector(); // Parse each driver Iterator itdriv; try { itdriv = getDrivers().iterator(); while(itdriv.hasNext()) { Driver driver = itdriv.next(); Iterator itdev = driver.getDevices().iterator(); while(itdev.hasNext()) { String device = itdev.next().getName(); String value = String.format("%s (%s)", device, driver.getName()); devices.add(value); } } } catch (IOException e) { e.printStackTrace(); } return devices.toArray(new String[devices.size()]); } public String[] getAllDevicesPath() { Vector devices = new Vector(); // Parse each driver Iterator itdriv; try { itdriv = getDrivers().iterator(); while(itdriv.hasNext()) { Driver driver = itdriv.next(); Iterator itdev = driver.getDevices().iterator(); while(itdev.hasNext()) { String device = itdev.next().getAbsolutePath(); devices.add(device); } } } catch (IOException e) { e.printStackTrace(); } return devices.toArray(new String[devices.size()]); } } ================================================ FILE: MySerialPort/app/src/main/java/cn/humiao/myserialport/Cmd.java ================================================ package cn.humiao.myserialport; /** * @author by AllenJ on 2018/5/3. */ public interface Cmd { String OPEN_DOOR = "010100000000FFFF"; } ================================================ FILE: MySerialPort/app/src/main/java/cn/humiao/myserialport/DataUtils.java ================================================ package cn.humiao.myserialport; import java.util.ArrayList; import java.util.List; /** * 串口数据转换工具类 * Created by Administrator on 2016/6/2. */ public class DataUtils { //------------------------------------------------------- // 判断奇数或偶数,位运算,最后一位是1则为奇数,为0是偶数 public static int isOdd(int num) { return num & 1; } //------------------------------------------------------- //Hex字符串转int public static int HexToInt(String inHex) { return Integer.parseInt(inHex, 16); } public static String IntToHex(int intHex){ return Integer.toHexString(intHex); } //------------------------------------------------------- //Hex字符串转byte public static byte HexToByte(String inHex) { return (byte) Integer.parseInt(inHex, 16); } //------------------------------------------------------- //1字节转2个Hex字符 public static String Byte2Hex(Byte inByte) { return String.format("%02x", new Object[]{inByte}).toUpperCase(); } //------------------------------------------------------- //字节数组转转hex字符串 public static String ByteArrToHex(byte[] inBytArr) { StringBuilder strBuilder = new StringBuilder(); for (byte valueOf : inBytArr) { strBuilder.append(Byte2Hex(Byte.valueOf(valueOf))); strBuilder.append(" "); } return strBuilder.toString(); } //------------------------------------------------------- //字节数组转转hex字符串,可选长度 public static String ByteArrToHex(byte[] inBytArr, int offset, int byteCount) { StringBuilder strBuilder = new StringBuilder(); int j = byteCount; for (int i = offset; i < j; i++) { strBuilder.append(Byte2Hex(Byte.valueOf(inBytArr[i]))); } return strBuilder.toString(); } //------------------------------------------------------- //转hex字符串转字节数组 public static byte[] HexToByteArr(String inHex) { byte[] result; int hexlen = inHex.length(); if (isOdd(hexlen) == 1) { hexlen++; result = new byte[(hexlen / 2)]; inHex = "0" + inHex; } else { result = new byte[(hexlen / 2)]; } int j = 0; for (int i = 0; i < hexlen; i += 2) { result[j] = HexToByte(inHex.substring(i, i + 2)); j++; } return result; } /** * 按照指定长度切割字符串 * * @param inputString 需要切割的源字符串 * @param length 指定的长度 * @return */ public static List getDivLines(String inputString, int length) { List divList = new ArrayList<>(); int remainder = (inputString.length()) % length; // 一共要分割成几段 int number = (int) Math.floor((inputString.length()) / length); for (int index = 0; index < number; index++) { String childStr = inputString.substring(index * length, (index + 1) * length); divList.add(childStr); } if (remainder > 0) { String cStr = inputString.substring(number * length, inputString.length()); divList.add(cStr); } return divList; } /** * 计算长度,两个字节长度 * * @param val value * @return 结果 */ public static String twoByte(String val) { if (val.length() > 4) { val = val.substring(0, 4); } else { int l = 4 - val.length(); for (int i = 0; i < l; i++) { val = "0" + val; } } return val; } /** * 校验和 * * @param cmd 指令 * @return 结果 */ public static String sum(String cmd) { List cmdList = DataUtils.getDivLines(cmd, 2); int sumInt = 0; for (String c : cmdList) { sumInt += DataUtils.HexToInt(c); } String sum = DataUtils.IntToHex(sumInt); sum = DataUtils.twoByte(sum); cmd += sum; return cmd.toUpperCase(); } } ================================================ FILE: MySerialPort/app/src/main/java/cn/humiao/myserialport/MainActivity.java ================================================ package cn.humiao.myserialport; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; public class MainActivity extends AppCompatActivity { private String TAG = "MainActivity"; private Button button; private TextView tv; private SerialPortUtil serialPortUtil; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); serialPortUtil = new SerialPortUtil(); serialPortUtil.openSerialPort(); //注册EventBus EventBus.getDefault().register(this); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { serialPortUtil.sendSerialPort(Cmd.OPEN_DOOR); } }); } /** * 用EventBus进行线程间通信,也可以使用Handler * @param string */ @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMainThread(String string){ Log.d(TAG,"获取到了从传感器发送到Android主板的串口数据"); tv.setText(string); } } ================================================ FILE: MySerialPort/app/src/main/java/cn/humiao/myserialport/SerialPortUtil.java ================================================ package cn.humiao.myserialport; import android.util.Log; import org.greenrobot.eventbus.EventBus; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android_serialport_api.SerialPort; /** * @author by AllenJ on 2018/4/20. * * 通过串口用于接收或发送数据 */ public class SerialPortUtil { private SerialPort serialPort = null; private InputStream inputStream = null; private OutputStream outputStream = null; private ReceiveThread mReceiveThread = null; private boolean isStart = false; /** * 打开串口,接收数据 * 通过串口,接收单片机发送来的数据 */ public void openSerialPort() { try { serialPort = new SerialPort(new File("/dev/ttyS0"), 9600, 0); //调用对象SerialPort方法,获取串口中"读和写"的数据流 inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); isStart = true; } catch (IOException e) { e.printStackTrace(); } getSerialPort(); } /** * 关闭串口 * 关闭串口中的输入输出流 */ public void closeSerialPort() { Log.i("test", "关闭串口"); try { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } isStart = false; } catch (IOException e) { e.printStackTrace(); } } /** * 发送数据 * 通过串口,发送数据到单片机 * * @param data 要发送的数据 */ public void sendSerialPort(String data) { try { byte[] sendData = DataUtils.HexToByteArr(data); outputStream.write(sendData); outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } } private void getSerialPort() { if (mReceiveThread == null) { mReceiveThread = new ReceiveThread(); } mReceiveThread.start(); } /** * 接收串口数据的线程 */ private class ReceiveThread extends Thread { @Override public void run() { super.run(); //条件判断,只要条件为true,则一直执行这个线程 while (isStart) { if (inputStream == null) { return; } byte[] readData = new byte[1024]; try { int size = inputStream.read(readData); if (size > 0) { String readString = DataUtils.ByteArrToHex(readData, 0, size); EventBus.getDefault().post(readString); } } catch (IOException e) { e.printStackTrace(); } } } } } ================================================ FILE: MySerialPort/app/src/main/res/layout/activity_main.xml ================================================