package android.serialport.api.sample; import java.util.List; import java.io.IOException; import java.util.ArrayList; import android.os.Message; import android.widget.Toast; import sendData.Commands; import sendData.SendDatas; import java.security.InvalidParameterException; public class ReceiveAndSend { public static List SendDatasList = new ArrayList(); public static void Start() { getSerialPort(); new SendThread().start(); new SendDatasThread().start(); new ReadThread().start(); } /** * Open the serial port, input and output streams */ public static void getSerialPort() { Global.mApplication = new Application(); try { Global.mSerialPort = Global.mApplication.getSerialPort(); Global.mOutputStream = Global.mSerialPort.getOutputStream(); Global.mInputStream = Global.mSerialPort.getInputStream(); if (MainActivity.CuttentActivity.mFragment_BUS != null) { Global.BUS bus = new Global.BUS(); Message msg = new Message(); msg.obj = bus; bus.index = 11; bus.value = 1; bus.value2 = 1; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } } catch (SecurityException e) { if (MainActivity.CuttentActivity.mFragment_BUS != null) { Global.BUS bus = new Global.BUS(); Message msg = new Message(); msg.obj = bus; bus.index = 11; bus.value = 0; bus.value2 = 0; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); msg = new Message(); bus.index = -1; bus.str = "You do not have read/write permission to the serial port."; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } } catch (IOException e) { if (MainActivity.CuttentActivity.mFragment_BUS != null) { Global.BUS bus = new Global.BUS(); Message msg = new Message(); msg.obj = bus; bus.index = 11; bus.value = 0; bus.value2 = 0; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); msg = new Message(); bus.index = -1; bus.str = "The serial port can not be opened for an unknown reason."; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } } catch (InvalidParameterException e) { if (MainActivity.CuttentActivity.mFragment_BUS != null) { Global.BUS bus = new Global.BUS(); Message msg = new Message(); msg.obj = bus; bus.index = 11; bus.value = 0; bus.value2 = 0; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); msg = new Message(); bus.index = -1; bus.str = "error_configuration"; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } } } /** * Read data thread * * @author 2017年12月19日 */ private static class ReadThread extends Thread { @Override public void run() { super.run(); while (!isInterrupted()) { int size = 0; try { byte[] buffer = new byte[64]; if (Global.mInputStream == null) { return; } size = Global.mInputStream.read(buffer); // Log.i("asdfg", Integer.toHexString(((buffer[7] & 0xFF) * // 256) + (buffer[8] & 0xFF)) + ""); for (int i = 0; i < size - 1; i++) { if (i != 0) { if ((buffer[i] & 0xFF) == 0xAA && (buffer[i + 1] & 0xFF) == 0xAA) { // byte[] _bf = new byte[i]; System.arraycopy(buffer, 0, _bf, 0, i); if (_bf.length >= 13) { int command = (buffer[7] & 0xFF) * 256 + (buffer[8] & 0xFF); if (command == Commands.ReadTimeFeedback.getCommand() || command == Commands.DryContactInitiativeToSend.getCommand() || command == Commands.READ_TEMP_WITH_DRY1_ACK.getCommand() || command == Commands.InductorState.getCommand() || command == Commands.SensorBack.getCommand() || command == Commands.ClickState.getCommand() || command == Commands.ReadID_ACK.getCommand() || command == Commands.WriteID_ACK.getCommand() || command == Commands.DataRequest.getCommand() || command == Commands.readRemark_ACK.getCommand()) { onDataReceived(buffer, size); } } byte[] _bf2 = new byte[size - i]; System.arraycopy(buffer, i, _bf2, 0, size - i); if (_bf2.length >= 13) { int command = (buffer[7] & 0xFF) * 256 + (buffer[8] & 0xFF); if (command == Commands.ReadTimeFeedback.getCommand() || command == Commands.DryContactInitiativeToSend.getCommand() || command == Commands.READ_TEMP_WITH_DRY1_ACK.getCommand() || command == Commands.InductorState.getCommand() || command == Commands.SensorBack.getCommand() || command == Commands.ClickState.getCommand() || command == Commands.ReadID_ACK.getCommand() || command == Commands.WriteID_ACK.getCommand() || command == Commands.DataRequest.getCommand() || command == Commands.readRemark_ACK.getCommand()) { onDataReceived(buffer, size); } } } } else { if (size >= 13) { int command = (buffer[7] & 0xFF) * 256 + (buffer[8] & 0xFF); if (command == Commands.ReadTimeFeedback.getCommand() || command == Commands.DryContactInitiativeToSend.getCommand() || command == Commands.READ_TEMP_WITH_DRY1_ACK.getCommand() || command == Commands.InductorState.getCommand() || command == Commands.SensorBack.getCommand() || command == Commands.ClickState.getCommand() || command == Commands.ReadID_ACK.getCommand() || command == Commands.WriteID_ACK.getCommand() || command == Commands.DataRequest.getCommand() || command == Commands.readRemark_ACK.getCommand()) { onDataReceived(buffer, size); } } } } } catch (Exception e) { e.printStackTrace(); return; } } } } private static void onDataReceived(final byte[] buffer, final int size) { if (MainActivity.CuttentActivity.mFragment_BUS == null) return; Global.BUS bus = new Global.BUS(); Message msg = new Message(); try { int command = (buffer[7] & 0xFF) * 256 + (buffer[8] & 0xFF); if (command == Commands.ReadTimeFeedback.getCommand()) { // } else if (command == Commands.DryContactInitiativeToSend.getCommand()) { try { Global.mOutputStream .write(SendDatas.AddSendData(Commands.DryContactInitiativeToSendFeedback.getCommand(), Global.SubNet_Id, Global.Device_Id, new byte[] { (byte) 0xf8 })); } catch (Exception e) { e.printStackTrace(); } bus.index = 1; bus.contact1 = buffer[11] & 0xFF; bus.contact2 = buffer[12] & 0xFF; bus.contact3 = buffer[13] & 0xFF; bus.contact4 = buffer[14] & 0xFF; bus.contact5 = buffer[15] & 0xFF; bus.contact6 = buffer[16] & 0xFF; msg.obj = bus; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } else if (command == Commands.READ_TEMP_WITH_DRY1_ACK.getCommand()) {// 读取温度 int value = (int) (buffer[11] & 0xFF); if ((buffer[13] & 0xFF) == 0) { // 单位摄氏度 bus.index = 7; bus.value = 0; bus.value2 = value; msg.obj = bus; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } else { // 华氏度 bus.index = 7; bus.value = 1; bus.value2 = value; msg.obj = bus; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } } else if (command == Commands.SensorBack.getCommand()) { // 传感器灵敏度 bus.index = 8; bus.value = (int) (buffer[12] & 0xFF); bus.value2 = (int) (buffer[12] & 0xFF); msg.obj = bus; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } else if (command == Commands.InductorState.getCommand()) { bus.index = 9; bus.value = 0; bus.value2 = 0; msg.obj = bus; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); } else if (command == Commands.ClickState.getCommand()) { int key = buffer[12] & 0xFF; switch (key) { case 0:// 弹起 bus.index = 109; bus.value = 0; bus.value2 = 0; msg.obj = bus; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); break; case 1:// 按下 bus.index = 10; bus.value = 1; bus.value2 = 0; msg.obj = bus; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); break; case 2:// 长按 bus.index = 10; bus.value = 2; bus.value2 = 0; msg.obj = bus; MainActivity.CuttentActivity.mFragment_BUS.mHandler.sendMessage(msg); break; } } else if (command == Commands.SET_TEMP_COMPENSATE_VALUE_ACK.getCommand()) { if ((buffer[11] & 0xFF) == 0xF5) { Toast.makeText(MainActivity.CuttentActivity, "set compensate value failed", Toast.LENGTH_SHORT) .show(); } } else if (command == Commands.WriteID_ACK.getCommand()) { // 写入ID返回 if ((buffer[11] & 0xFF) == 0xF5) { NavigationDrawerFragment.mNavigationDrawerFragment.mHandler.sendEmptyMessage(0); } else if ((buffer[11] & 0xFF) == 0xF8) { NavigationDrawerFragment.mNavigationDrawerFragment.mHandler.sendEmptyMessage(1); } } else if (command == Commands.ReadID_ACK.getCommand()) { // 读取返回 bus.value = buffer[11] & 0xFF; bus.value2 = buffer[12] & 0xFF; msg.obj = bus; NavigationDrawerFragment.mNavigationDrawerFragment.mHandler_Data.sendMessage(msg); } else if (command == Commands.DataRequest.getCommand()) { // 读取备注返回 / 设备升级 0x15FE buffer[11] buffer[12] 都为0的话,就是备注 // Log.i("asdfg", "设备升级 0x15FE"); int index = (buffer[11] & 0xFF) * 256 + (buffer[12] & 0xFF);// 请求的是第几条包 Fragment_Update.mFragment_Update.mHandler.sendEmptyMessage(index); } } catch (Exception e) { e.getMessage(); } } /** * 2015.12.10 读取温度 */ private static class SendThread extends Thread { @Override public void run() { super.run(); while (true) { try { byte[] sendBytes = SendDatas.AddSendData(Commands.READ_TEMP_WITH_DRY1.getCommand(), Global.SubNet_Id, Global.Device_Id, new byte[] {}); Global.mOutputStream.flush(); Global.mOutputStream.write(sendBytes); sleep(1000); } catch (Exception e) { // continue; } } } } private static class SendDatasThread extends Thread { @Override public void run() { super.run(); while (true) { try { Thread.sleep(100); } catch (Exception e) { e.getMessage(); } for (int i = 0; i < SendDatasList.size(); i++) { try { Thread.sleep(100); } catch (Exception e) { } try { byte[] sendBytes = SendDatasList.get(i); try { Global.mOutputStream.flush(); Global.mOutputStream.write(sendBytes); } catch (Exception e) { e.printStackTrace(); } SendDatasList.remove(i); i--; } catch (Exception ex) { ex.getMessage(); } } } } } }