/// <summary> /// 返回1代表继续接收,返回0代表接收结束 /// </summary> /// <param name="b"></param> /// <returns></returns> public override int writeByte(byte b) { ////写入返回值 int writeRet = -1; currentPosition++; switch (currentPosition) { case 0: case 1: case 2: case 3: /////标识消息总长度8位 case 4: case 5: case 6: case 7: case 8: case 9: headByte.Add(b); break; case 10: headByte.Add(b); try { allDataLength = BLEData.byteToInt64(headByte[3], headByte[4], headByte[5], headByte[6], headByte[7], headByte[8], headByte[9], headByte[10]); } catch { errorData(); return(0); } // dataLength = BLE.BLEData.getInt16(data[2], data[3]); break; case 11: ////这4位代表消息内容的长度 case 12: case 13: msgByteLengthByte.Add(b); break; case 14: msgByteLengthByte.Add(b); msgByteLength = byteToInt32(msgByteLengthByte.ToArray()); break; case 15: ////这8位代表文件的长度 case 16: case 17: case 18: case 19: case 20: case 21: fileDataLengthByte.Add(b); break; case 22: fileDataLengthByte.Add(b); fileDataLength = byteToInt64(fileDataLengthByte.ToArray()); break; default: int beforIndex = 22; if (currentPosition < beforIndex + msgByteLength) { ////当前位置小于消息长度,为消息内容 msgByte.Add(b); } else if (currentPosition == beforIndex + msgByteLength) { ////当前位置等于消息长度,为消息结尾 msgByte.Add(b); string msgJson = getString(msgByte.ToArray()); // stringMsg sm = stringMsg.jsonToModel(pathJson); this.ReceiveFullMsg = msgJson; try { stringMsg FullMsg = stringMsg.jsonToModel(msgJson); string rfmPath = FullMsg.value["saveFileFullPath"]; if (rfmPath.Trim() == "") { errorData(); return(0); } string dir = System.IO.Path.GetDirectoryName(rfmPath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } fileWrite = System.IO.File.Create(rfmPath); //ReceiveFullMsg } #region 创建文件异常处理 catch (UnauthorizedAccessException ex1) { errorData(); return(0); } catch (ArgumentException ex1) { errorData(); return(0); } catch (PathTooLongException ex1) { errorData(); return(0); } catch (DirectoryNotFoundException ex1) { errorData(); return(0); } catch (IOException ex1) { errorData(); return(0); } catch (NotSupportedException ex1) { errorData(); return(0); } #endregion } else if (currentPosition < beforIndex + msgByteLength + fileDataLength) { ////当前位置小于文件长度,为文件内容 fileWrite.WriteByte(b); fileWrite.Flush(); } else if (currentPosition >= beforIndex + msgByteLength + fileDataLength) { ////消息结束 fileWrite.WriteByte(b); fileWrite.Flush(); fileWrite.Close(); successData(); return(0); } break; } return(1); }
/// <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++; isReading?.Invoke(this, true); } 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; } if (!b.ToString().Equals("11") && !b.ToString().Equals("12")) { 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 (Exception ex) { // tools.log.writeLog("readData:第{0}次,错误:{1}", i.ToString(), ex.Message); 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; } } }
/// <summary> /// 返回1代表继续接收,返回0代表接收结束 /// </summary> /// <param name="b"></param> /// <returns></returns> public override int writeByte(byte b) { ////写入返回值 int writeRet = -1; currentPosition++; switch (currentPosition) { case 0: case 1: case 2: case 3: /////标识消息总长度 case 4: case 5: case 6: case 7: case 8: case 9: headByte.Add(b); break; case 10: headByte.Add(b); try { allDataLength = BLEData.byteToInt64(headByte[3], headByte[4], headByte[5], headByte[6], headByte[7], headByte[8], headByte[9], headByte[10]); } catch { errorData(); return(0); } // dataLength = BLE.BLEData.getInt16(data[2], data[3]); break; case 11: ////这4位代表消息内容的长度 case 12: case 13: msgByteLengthByte.Add(b); break; case 14: msgByteLengthByte.Add(b); msgByteLength = byteToInt32(msgByteLengthByte.ToArray()); break; default: msgDataByte.Add(b); if (currentPosition + 1 >= allDataLength + BLE.BLEData.headLength) { successData(); return(0); } break; } return(1); }
/// <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; } } }