/// <summary> /// 读取数据 /// </summary> /// <param name="o"></param> void thSend(object o) { bool whileSwitch = true; #if DEBUG // tools.log.writeLog("tcpListenerControl.readByte 线程:{0},数据等待", System.Threading.Thread.CurrentThread.ManagedThreadId); #endif while (whileSwitch) { try { currentSendBleData = this.DataQueue.Dequeue(); if (currentSendBleData == null) { continue; } #if DEBUG // tools.log.writeLog("tcpDataProcessingControl.tcpDataProcessing 线程:{0},取到了一条数据", System.Threading.Thread.CurrentThread.ManagedThreadId); #endif if (currentSendBleData.command == BLEcommand.t12) { currentSendBleData.toBleStream(this.sendDataGetStream()); } else { sendData(currentSendBleData); } currentSendBleData = null; } catch (NotSupportedException ex1) { whileSwitch = false; tools.log.writeLog("NotSupportedException:{0}", ex1.Message); connectionDisconnection(); } catch (ObjectDisposedException ex2) { whileSwitch = false; tools.log.writeLog("ObjectDisposedException:{0}", ex2.Message); connectionDisconnection(); } catch (IOException ex3) { whileSwitch = false; tools.log.writeLog("IOException:{0}", ex3.Message); connectionDisconnection(); } catch (System.Threading.ThreadAbortException ex) { whileSwitch = false; tools.log.writeLog("ThreadAbortException:{0}", ex.Message); ///线程终止 connectionDisconnection(); } } }
//新消息事件 void newBlemessageEventFun(tcpDataCommunication tcpComm, BLEData ble) { //string jsonText = ((BLE.bleClass.t11)ble).msg; //JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); stringMsg msg = stringMsg.jsonToModel(ble.ToString()); switch (msg.name) { case msgEnum.dengru: //bool bl = Convert.ToBoolean(jo["value"]["return"]); bool bl = Convert.ToBoolean(msg.value["return"]); if (bl) { RetUser curr = JsonConvert.DeserializeObject <RetUser>(msg.value["jsonCurr"]); ConfigInfo ConfigInfo1 = JsonConvert.DeserializeObject <ConfigInfo>(msg.value["ConfigInfo"]); //RetUser curr = user.Login(login.textBox1.Text, login.textBox2.Text); CurrUser.currUser = curr.User; CurrUser.config = ConfigInfo1; CloseFrom2(); ShowFile(curr.User.ID); } else { // zTcpClient1.tcpComm.stop(); MessageBox.Show("账号或密码错误!"); } break; case msgEnum.liaotian: //string groupSendingMsg = jo["value"]["groupSending"].ToString(); string reciveMsg = ""; if (msg.value.Keys.Contains("groupSending")) { reciveMsg = msg.value["groupSending"]; } else if (msg.value.Keys.Contains("singleSending")) { reciveMsg = msg.value["singleSending"]; } showMsg(reciveMsg); break; case msgEnum.returnUserFileList: ResetList(msg); break; case msgEnum.fileUpload: fileUpload(tcpComm, ble); break; default: break; } }
void newBlemessageEventFun(tcpDataCommunication tcpComm, BLEData ble) { string jsonText = ((BLE.bleClass.t11)ble).msg; JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); bool bl = Convert.ToBoolean(jo["value"]["return"]); if (bl) { MessageBox.Show("登陆成功!"); // ff.Controls["button5"].Enabled = false; //button5.Enabled = true; } else { MessageBox.Show("账号或密码错误!"); } }
void fileUpload(tcpDataCommunication tcpComm, BLEData msg) { stringMsg m1 = stringMsg.jsonToModel(msg.ToString()); if (m1.value["saveFileFullPath"].Trim() == "") { showMsg("<下载失败,服务端文件不存在>"); // MessageBox.Show("下载失败,服务端文件不存在", "提示"); return; } string fileName = System.IO.Path.GetFileName(m1.value["saveFileFullPath"]); showMsg(string.Format("<{0} 下载完成>", fileName)); //MessageBox.Show("下载完成", "提示"); }
/// <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); }
public static extern bool PollData(out BLEData data, bool block);
public static extern bool SendData(BLEData data);
/// <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="ble"></param> public void addSendBle(BLEData ble) { this.DataQueue.Enqueue(ble); // waitSendList = this.DataQueue.GetSendFileList(); }
/// <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> /// 处理数据 /// </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; } } }