| | |
| | | |
| | | import android.util.Log; |
| | | |
| | | import com.blankj.utilcode.util.ConvertUtils; |
| | | import com.blankj.utilcode.util.LogUtils; |
| | | import com.blankj.utilcode.util.ThreadUtils; |
| | | 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; |
| | |
| | | |
| | | /** |
| | | * 打开串口 |
| | | * |
| | | * @return serialPort串口对象 |
| | | */ |
| | | public SerialPort openSerialPort(String path, int baudrate){ |
| | | public SerialPort openSerialPort(final String path, final int baudrate) { |
| | | try { |
| | | serialPort = new SerialPort(new File(path), baudrate, 0); |
| | | this.serialPortStatus = true; |
| | |
| | | if (onDataReceiveListener!=null) { |
| | | onDataReceiveListener.onDataReceive(buffer, size); |
| | | } |
| | | |
| | | |
| | | // LogUtils.i(TAG, "锁串口返回:" + ConvertUtils.bytes2HexString(buffer)); |
| | | } |
| | | } catch (IOException e) { |
| | | } 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; |
| | | } |
| | | |
| | |
| | | outputStream.write(sendData); |
| | | outputStream.flush(); |
| | | } |
| | | } catch (IOException e) { |
| | | } 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; |
| | | } |