/// <summary> /// 获取设备的温度补偿参数 para为0表示低温补偿,1表示常温补偿 2表示高温补偿 /// </summary> /// <param name="longAddress"></param> /// <param name="para"></param> /// <returns></returns> public TemperatureCompensation ReadTC(long longAddress, byte para) { TemperatureCompensation ret = null; RequestPacket request = new RequestPacket() { LongOrShort = 1, Address = longAddress, Command = 0xC3, DataContent = new byte[] { 0x4C, para }, }; ResponsePacket response = Request(request); if (response != null && response.DataContent != null && response.DataContent.Length >= 14) { byte[] d = response.DataContent; ret = new TemperatureCompensation(); ret.LowerRangeAD = BitConverter.ToSingle(new byte[] { d[5], d[4], d[3], d[2] }, 0); ret.UpperRangeAD = BitConverter.ToSingle(new byte[] { d[9], d[8], d[7], d[6] }, 0); ret.TemperatureAD = BitConverter.ToSingle(new byte[] { d[13], d[12], d[11], d[10] }, 0); } return(ret); }
/// <summary> /// 写温度补偿 /// </summary> public bool WriteTC(long longAddress, byte para, TemperatureCompensation tc) { RequestPacket request = new RequestPacket() { LongOrShort = 1, Address = longAddress, Command = 0xC3, }; List <byte> temp = new List <byte>(); temp.Add(0x22); temp.Add(para); temp.AddRange(BitConverter.GetBytes(tc.LowerRangeAD).Reverse()); temp.AddRange(BitConverter.GetBytes(tc.UpperRangeAD).Reverse()); temp.AddRange(BitConverter.GetBytes(tc.TemperatureAD).Reverse()); request.DataContent = temp.ToArray(); ResponsePacket response = Request(request); return(response != null); }
/// <summary> /// 写标签信息 /// </summary> public bool WriteTag(long longAddress, DeviceTagInfo tag) { RequestPacket request = new RequestPacket() { LongOrShort = 1, Address = longAddress, Command = 18, }; List <byte> d = new List <byte>(); d.AddRange(PackAsciiHelper.GetBytes(tag.Tag.PadRight(8, ' ').Substring(0, 8), 6)); d.AddRange(PackAsciiHelper.GetBytes(tag.Description.PadRight(16, ' ').Substring(0, 16), 12)); d.Add((byte)tag.Day); d.Add((byte)tag.Month); d.Add((byte)(tag.Year - 1990)); request.DataContent = d.ToArray(); ResponsePacket response = Request(request); return(response != null); }
/// <summary> /// 读取主变量传感器信息 /// </summary> public SensorInfo ReadPVSensor(long longAddress) { SensorInfo ret = null; RequestPacket request = new RequestPacket() { LongOrShort = 1, Address = longAddress, Command = 14 }; ResponsePacket response = Request(request); if (response != null && response.DataContent != null && response.DataContent.Length >= 16) { byte[] d = response.DataContent; ret = new SensorInfo(); ret.SensorSN = SEBinaryConverter.BytesToInt(new byte[] { d[2], d[1], d[0] }); ret.UnitCode = (UnitCode)d[3]; ret.UpperLimit = BitConverter.ToSingle(new byte[] { d[7], d[6], d[5], d[4] }, 0); ret.LowerLimit = BitConverter.ToSingle(new byte[] { d[11], d[10], d[9], d[8] }, 0); ret.MinimumSpan = BitConverter.ToSingle(new byte[] { d[15], d[14], d[13], d[12] }, 0); } return(ret); }
/// <summary> /// 读取设备标签信息 /// </summary> public DeviceTagInfo ReadTag(long longAddress) { DeviceTagInfo ret = null; RequestPacket request = new RequestPacket() { LongOrShort = 1, Address = longAddress, Command = 13 }; ResponsePacket response = Request(request); if (response != null && response.DataContent != null && response.DataContent.Length >= 21) { byte[] d = response.DataContent; ret = new DeviceTagInfo(); ret.Tag = PackAsciiHelper.GetString(new byte[] { d[0], d[1], d[2], d[3], d[4], d[5] }); //字节0-5 ret.Description = PackAsciiHelper.GetString(new byte[] { d[6], d[7], d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15], d[16], d[17] }); //字节6-17 ret.Year = 1990 + d[20]; ret.Month = d[19]; ret.Day = d[18]; } return(ret); }
/// <summary> /// 请求数据 /// </summary> /// <param name="packet"></param> /// <returns></returns> public ResponsePacket Request(RequestPacket packet, int timeout = 2000) { ResponsePacket ret = null; lock (_CommandLocker) { byte[] cmd = packet.ToBytes(); if (cmd != null && cmd.Length > 0) { _DeviceResponsed.Reset(); _RequestPakcet = packet; _ResponsePacket = null; _Buffer.Clear(); SendData(cmd); if (_DeviceResponsed.WaitOne(timeout)) { if (_ResponsePacket.ResponseOk) { ret = _ResponsePacket; _LastError = string.Empty; } else { _LastError = string.Format("通讯状态:{0} 设备状态:{1}", _ResponsePacket.CommunicationError_Str(), _ResponsePacket.DeviceStatus_Str()); } } else { _LastError = "设备没有回复"; if (Debug) { LJH.GeneralLibrary.LOG.FileLog.Log("串口通讯", "解析包失败"); } } } } return(ret); }
/// <summary> /// 读取模拟输出信息 /// </summary> public OutputInfo ReadOutput(long longAddress) { OutputInfo ret = null; RequestPacket request = new RequestPacket() { LongOrShort = 1, Address = longAddress, Command = 15 }; ResponsePacket response = Request(request); if (response != null && response.DataContent != null && response.DataContent.Length >= 17) { byte[] d = response.DataContent; ret = new OutputInfo(); ret.AlarmSelectCode = d[0]; ret.TransferFunctionCode = d[1]; ret.PVUnitCode = (UnitCode)d[2]; ret.UpperRangeValue = BitConverter.ToSingle(new byte[] { d[6], d[5], d[4], d[3] }, 0); ret.LowerRangeValue = BitConverter.ToSingle(new byte[] { d[10], d[9], d[8], d[7] }, 0); ret.DampingValue = BitConverter.ToSingle(new byte[] { d[14], d[13], d[12], d[11] }, 0); ret.WriteProtectCode = d[15]; ret.PrivateLabelDistributorCode = d[16]; } return(ret); }