/// <summary> /// 消息接收事件处理程序 /// </summary> /// <param name="tcpComm"></param> /// <param name="ble"></param> void newBlemessageEventFun(tcpDataCommunication tcpComm, BLE.BLEData ble) { string s1 = ble.ToString(); stringMsg m2 = stringMsg.jsonToModel(s1); newMessageEvent?.Invoke(tcpComm, m2); }
/// <summary> /// 消息接收事件处理程序 /// </summary> /// <param name="tcpComm"></param> /// <param name="ble"></param> void newBlemessageEventFun(tcpDataCommunication tcpComm, BLE.BLEData ble) { string s1 = ""; if (ble.command == BLEcommand.t11) { s1 = ble.ToString(); } else if (ble.command == BLEcommand.t12) { s1 = ((BLE.bleClass.t12)ble).ReceiveFullMsg; } stringMsg m2 = stringMsg.jsonToModel(s1); newMessageEvent?.Invoke(tcpComm, m2); }
/// <summary> /// 创建实例 /// </summary> /// <param name="comm"></param> /// <returns></returns> public static BLEData CreateBle(BLEcommand comm) { BLEData b = null; switch (comm) { case BLEcommand.t11: b = new bleClass.t11(); break; case BLEcommand.t12: b = new bleClass.t12(); break; default: b = new BLEData(comm); break; } return(b); }
/// <summary> /// 发送数据 /// </summary> /// <param name="data"></param> public void sendData(BLE.BLEData b1) { lock (this) { try { //BLE.bleClass.t11 t11 = new BLE.bleClass.t11(); //t11.msg = this.richTextBox2.Text; sendData(b1.toBleByte()); } catch (ObjectDisposedException ex2) { connectionDisconnection(); } catch (IOException ex3) { connectionDisconnection(); } } }
/// <summary> /// 处理数据 /// </summary> /// <param name="b"></param> public void readData(byte[] tcpByte, int count) { ////写入返回值 int writeRet = -1; for (int i = 0; i < count; i++) { byte b = tcpByte[i]; switch (currentPosition) { case 0: if (b == 0x55) { data.Add(b); currentPosition++; } else { } break; case 1: if (b == 0xAA) { data.Add(b); currentPosition++; } else { errorData(); } break; case 2: BLEcommand b1; if (!Enum.TryParse(b.ToString(), out b1)) { errorData(); break; } data.Add(b); ble = BLEData.CreateBle(b1); currentPosition++; break; case 3: case 4: case 5: case 6: case 7: case 8: case 9: data.Add(b); currentPosition++; break; case 10: data.Add(b); try { dataLength = BLEData.byteToInt64(data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10]); } catch { errorData(); break; } foreach (var d in data) { writeRet = ble.writeByte(d); } currentPosition++; // dataLength = BLE.BLEData.getInt16(data[2], data[3]); break; default: writeRet = ble.writeByte(b); currentPosition++; break; } if (writeRet == 0) { successData(); writeRet = -1; } } }