private void Svr_ClientConnect(object sender, ZYBEventArgs e) { if (e == null) { return; } if (sender == null) { return; } if (e.DTU == null) { return; } lock (sender) { try { ///接收的数据 Dtu_Data data = new Dtu_Data(); data.IP = e.DTU.IP; data.ID = e.DTU.ID; data.IsOnline = e.DTU.IsOnline; data.LoginTime = e.DTU.LoginTime; data.RefreshTime = e.DTU.RefreshTime; data.PhoneNumber = e.DTU.PhoneNumber; data.datas = e.DTU.DataByte; data.DtuID = e.DTU.DtuID; data.Msg = e.Msg; IO_DEVICE currentDevice = this.IODevices.Find(x => x.IO_DEVICE_ADDRESS == data.ID); if (currentDevice != null) { //设备上线 this.OnLine(base.IOServer, this.IOCommunication, currentDevice, null, data.ID); } } catch (Exception emx) { this.DeviceException(("error=10008" + emx.Message)); } } }
private void Svr_ReceiveData(object sender, ZYBEventArgs e) { if (e == null) { return; } if (sender == null) { return; } if (e.DTU == null) { return; } lock (sender) { if (e.DTU != null) { try { //注意此处传过来的ID是16位字符串 string id = e.DTU.ID; // 接收的数据 Dtu_Data data = new Dtu_Data(); data.IP = e.DTU.IP; data.ID = e.DTU.ID; IO_DEVICE currentDevice = this.IODevices.Find(x => x.IO_DEVICE_ADDRESS == data.ID); if (currentDevice == null) { return; } if (e.DTU.DtuID != "") { data.DtuID = e.DTU.DtuID; } else { data.DtuID = svr.InneridToId(int.Parse(e.DTU.ID)); } data.IsOnline = e.DTU.IsOnline; if (e.DTU.LoginTime != null) { data.LoginTime = e.DTU.LoginTime; } if (e.DTU.RefreshTime != null) { data.RefreshTime = e.DTU.RefreshTime; } data.PhoneNumber = e.DTU.PhoneNumber; data.datas = e.DTU.DataByte; data.Msg = e.Msg; //针对接收的数据做两种状态,读命令的应答和写命令的应答 if (data.datas.Length == 1)//判断是不是心跳包 { if (CVT.ByteToHexStr(data.datas) == "fe") { this.DeviceStatus(this.IOServer, this.IOCommunication, currentDevice, null, "fe"); } } else if (data.datas.Length >= 5 && data.datas.Length != 21) //判断是不是数据包 { byte SlaveID = data.datas[0]; //设备号 byte Code = data.datas[1]; //设备号 if (Code == 0x03 || Code == 0x04 || Code == 0x01 || Code == 0x02) //用户读取的数据 { data.DataStatus = DataStatus.ReadData; //将接收的数据转换成字符串并发送 this.ReceiveData(this.IOServer, this.IOCommunication, currentDevice, data.datas, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } else if (Code > 0x80)//出现的错误信息 { data.DataStatus = DataStatus.ReadData; //执行操作 } else if (Code == 0x06 || Code == 0x10)//表示用户写入的数据和 { data.DataStatus = DataStatus.WriterData; //执行操作 } } } catch (Exception emx) { this.DeviceException(("error =10006," + emx.Message)); } } } }