/// <summary> /// 解析接收数据 /// </summary> private void ParseReceive() { while (true) { Thread.Sleep(100); if (this.dataReceiveStr != "") { string tmpReceiveStr = dataReceiveStr; if (tmpReceiveStr.IndexOf("B5") == 0 && tmpReceiveStr.Length % 9 == 0) { this.dataReceiveStr = ""; byte[] bufferSend = Converts.StringToBytes(tmpReceiveStr); ByteConvertString(bufferSend); if (EventDataArrival != null && dataArrival.Count > 0) { this.EventDataArrival(dataArrival); } this.dataArrival.Clear(); } else { this.dataReceiveStr = ""; } } } }
/// <summary> /// 接收数据 /// </summary> private void SocketThread() { while (true) { Thread.Sleep(1); try { byte[] buffer = new byte[1024]; int count = this.socket.Receive(buffer); this.countReceive = this.countReceive + count; if (buffer[0] == 0xB5 && count % 9 == 0) { byte[] data = new byte[count]; Array.Copy(buffer, 0, data, 0, data.Length); this.dataReceiveStr = this.dataReceiveStr + Converts.BytesToString(data); } else { this.dataReceiveStr = ""; } } catch (Exception err) { Console.WriteLine(err.Message); } } }
public bool SendData(string[] strData) { try { string message = "A5" + strData[0] + strData[1] + "5A"; byte[] buffer = Converts.StringToBytes(message); this.socket.Send(buffer); this.countSend = this.countSend + buffer.Length; return(true); } catch { return(false); } }