/// <summary> /// 协调器发送数据 /// </summary> /// <returns></returns> public bool SendData(byte[] buffer) { try { serialPort.Write(buffer, 0, buffer.Length); sendData = sendData + Converts.BytesToString(buffer); return(true); } catch { return(false); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="type">类型</param> /// <param name="addr">地址</param> /// <param name="data">数据</param> /// <returns></returns> public bool SendData(byte type, byte addr, byte[] data) { try { byte[] buffer = new byte[] { 0xA5, type, addr, data[0], data[1], data[2], data[3], data[4], 0x5A }; serialPort.Write(buffer, 0, buffer.Length); sendData = sendData + Converts.BytesToString(buffer); return(true); } catch { return(false); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="type">类型</param> /// <param name="addr">地址</param> /// <param name="data">数据</param> /// <returns>状态</returns> public bool SendData(byte type, byte addr, byte[] data) { try { byte[] buffer = new byte[] { 0xA5, type, addr, data[0], data[1], data[2], data[3], data[4], 0x5A }; socket.Send(buffer); sendData = sendData + Converts.BytesToString(buffer); return(true); } catch { return(false); } }
/// <summary> /// 解析接收数据 /// </summary> private void ParseReceiveData() { //while (true) //{ Thread.Sleep(1); if (isGateWay) { receiveData = GateWay.receiveData; GateWay.receiveData = ""; } else { receiveData = Coordinator.receiveData; Coordinator.receiveData = ""; } while (this.receiveData != "") { Thread.Sleep(1); Byte[] data = new Byte[9]; try { data = Converts.StringToBytes(receiveData.Substring(0, 18)); receiveData = receiveData.Remove(0, 18); } catch { } if (data[0] == 0xB5 && data.Length == 9) { SensorBase sameSensor = sensorList.FirstOrDefault(p => p.Type.Equals(data[1]) && p.Addr.Equals(data[2])); if (sameSensor != null) { sameSensor.Data = new byte[5] { data[3], data[4], data[5], data[6], data[7] }; } else { sensorList.Add(GetType(data)); } } } //} }
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { int count = this.serialPort.BytesToRead; //数据的长度 Byte[] buffer = new Byte[count]; this.serialPort.Read(buffer, 0, buffer.Length); //串口接收数据 if (buffer[0] == 0xB5 && count % 9 == 0) //判断数据下标为0的数据是否为0xB5及长度为9的倍数 { Byte[] data = new Byte[count]; Array.Copy(buffer, 0, data, 0, data.Length); //复制数据 receiveData = receiveData + Converts.BytesToString(data); //将数据转化为String类型放置接收缓存区; this.rawData = this.rawData + receiveData; Thread.Sleep(10); parseData.StartParseData(); } } catch (Exception) { } }
/// <summary> /// 接收数据 /// </summary> private void ReceiveData() { while (true) { Thread.Sleep(1); //线程停止1sms try { Byte[] buffer = new Byte[1024]; Int32 count = this.socket.Receive(buffer); //Socket接收数据 if (buffer[0] == 0xB5 && count % 9 == 0) //判断数据下标为0的数据是否为0xB5及长度为9的倍数 { Byte[] data = new Byte[count]; Array.Copy(buffer, 0, data, 0, data.Length); //复制数据 receiveData = receiveData + Converts.BytesToString(data); //将数据转化为String类型放置接收缓存区 this.rawData = this.rawData + receiveData; } } catch (Exception) { } } }