package android_serialport_api; import android.util.Log; import com.blankj.utilcode.util.LogUtils; import com.lotaai.canguiayw.CThreadPoolExecutor; import com.lotaai.canguiayw.application.App; import com.lotaai.canguiayw.common.SettingConfig; import com.pranavpandey.android.dynamic.toasts.DynamicToast; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class SerialPortUtils { private static final String TAG = "SerialPortUtils"; public boolean serialPortStatus = false; //是否打开串口标志 public String data_; public boolean threadStatus; //线程状态,为了安全终止线程 public SerialPort serialPort = null; public InputStream inputStream = null; public OutputStream outputStream = null; public static String[] getAllDevices() { SerialPortFinder serialPortFinder = new SerialPortFinder(); String[] allDevices = serialPortFinder.getAllDevices(); for (int i = 0; i < allDevices.length; i++) { allDevices[i] = "/dev/" + allDevices[i]; Log.d(TAG, "No." + (i + 1) + " = " + allDevices[i]); } return allDevices; } /** * 打开串口 * * @return serialPort串口对象 */ public SerialPort openSerialPort(final String path, final int baudrate) { try { serialPort = new SerialPort(new File(path), baudrate, 0); this.serialPortStatus = true; threadStatus = false; //线程状态 //获取打开的串口中的输入输出流,以便于串口数据的收发 inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); Runnable syncRunnable = new Runnable() { @Override public void run() { //判断进程是否在运行,更安全的结束进程 while (!threadStatus) { byte[] buffer = new byte[64]; int size; //读取数据的大小 try { size = inputStream.read(buffer); if (size > 0) { if (onDataReceiveListener != null) { onDataReceiveListener.onDataReceive(buffer, size); } // LogUtils.i(TAG, "锁串口返回:" + ConvertUtils.bytes2HexString(buffer)); } } catch (final IOException e) { LogUtils.e(TAG, "锁串口返回run: 数据读取异常:" + e.toString()); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { DynamicToast.makeSuccess(App.app, "锁串口返回run: 数据读取异常:" + e.toString(), 3).show(); } }); } } } }; SettingConfig.getInstance().getExecutor().execute(syncRunnable); } catch (IOException e) { LogUtils.e(TAG, "openSerialPort: 打开串口异常:" + e.toString()); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { } }); return serialPort; } LogUtils.i(TAG, "openSerialPort: 打开串口:" + path + ", 波特率:" + baudrate); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { DynamicToast.makeSuccess(App.app, "打开串口:" + path + ", 波特率:" + baudrate, 3).show(); } }); return serialPort; } /** * 关闭串口 */ public void closeSerialPort() { try { inputStream.close(); outputStream.close(); this.serialPortStatus = false; this.threadStatus = true; //线程状态 serialPort.close(); } catch (IOException e) { Log.e(TAG, "closeSerialPort: 关闭串口异常:" + e.toString()); return; } Log.d(TAG, "closeSerialPort: 关闭串口成功"); } /** * 发送串口指令(字符串) * @param data String数据指令 */ public void sendSerialPort(String data) { byte[] sendData = data.getBytes(); //string转byte[] sendSerialPort(sendData); } public void sendSerialPort(byte[] sendData) { try { if (sendData == null) { return; } this.data_ = new String(sendData); if (sendData.length > 0) { StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < sendData.length; i++) { stringBuffer.append(String.format("0x%02x", sendData[i]) + " "); } // LogUtils.i(TAG, "发送串口数据:" + stringBuffer.toString()); outputStream.write(sendData); outputStream.flush(); } } catch (final IOException e) { LogUtils.e(TAG, "sendSerialPort: 串口数据发送失败:" + e.toString()); CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { DynamicToast.makeSuccess(App.app, "串口数据发送失败:" + e.toString(), 3).show(); } }); } } //这是写了一监听器来监听接收数据 public OnDataReceiveListener onDataReceiveListener = null; public interface OnDataReceiveListener { void onDataReceive(byte[] buffer, int size); } public void setOnDataReceiveListener(OnDataReceiveListener dataReceiveListener) { onDataReceiveListener = dataReceiveListener; } }