//用户发送下置命令 public override bool SendCommand(IO_SERVER server, IO_COMMUNICATION communication, IO_DEVICE device, IO_PARA para, string value) { try { Dtu_Data data = new Dtu_Data(); data.DeviceID = device.IO_DEVICE_ID; //获取value 的字节数组 DeviceDrive driver = DeviceDrives.Find(x => x.DeviceDriverID == device.DEVICE_DRIVER_ID); if (driver != null) { data.datas = driver.GetSendValueBytes(server, this.IOCommunication, device, para, value); this.DeviceException("error=10011," + device.IO_DEVICE_ADDRESS + "设备驱动不存在"); } data.ParaID = para.IO_ID; data.DtuID = device.IO_DEVICE_ADDRESS; data.DataStatus = DataStatus.WriterData; string error = ""; bool result = false; Thread.Sleep(100);//停止10秒,保证之前发送命令已经发送出去 if (!svr.Send(data.DtuID, data.datas, out error, false)) { this.DeviceException("error=10010" + error); result = false; } else { result = true; } //信息发送完成的事件 DataSended(server, this.IOCommunication, device, para, value, result); return(true); ///接收的数据 } catch (Exception emx) { return(false); } finally { } }
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)); } } } }