public static void Cacular(byte[] data) { //DtuId:212 //WellId:3 //配注仪状态: 0 Console.WriteLine($"配注仪状态 : {data[4]}"); //设定流量回读: 0.17 var t = BCDUtils.BCDToUshort((ushort)(data[5] << 8 | data[6])); Console.WriteLine($"设定流量回读 : {t / 100.0}"); //瞬时流量: 0.15 t = BCDUtils.BCDToUshort((ushort)(data[7] << 8 | data[8])); Console.WriteLine($"瞬时流量 : { t / 100.0}"); //累计流量: 607.26 var t1 = BCDUtils.BCDToUshort((ushort)(data[9] << 8 | data[10])); var t2 = BCDUtils.BCDToUshort((ushort)(data[11] << 8 | data[12])); Console.WriteLine($"累计流量 : {(t1 * 10000 + t2) / 100.0}"); //扩展: 0.0 //水井压力: 7.35 t = BCDUtils.BCDToUshort((ushort)(data[17] << 8 | data[18])); Console.WriteLine($"水井压力 : {t / 100.0}"); }
public static async Task Post_ZJJH_WII_InjectionAllocation(ModbusRtuOverTcp client, RedisClient redisClient, string messageString) { var par = messageString.FromJson <ControlRequest>(); var waterWell = new IotDataWaterWell(); var logIotModbusPoll = par.ConvertTo <LogIotModbusPoll>(); var modbusAddress = par.ModbusAddress; ClientInfo.CurrentModbusPoolAddress = modbusAddress; try { if (!par.CommandParameter.IsNullOrEmpty()) { var parDict = par.CommandParameter.FromJson <Dictionary <long, double> >(); if (parDict != null) { foreach (var item in parDict) { var slotId = item.Key; var startAddress = (ushort)(5 + slotId - 1); var value = BCDUtils.UshortToBCD((ushort)(item.Value * 100)); var read = await client.WriteOneRegisterAsync($"s={par.ModbusAddress};{startAddress}", value); if (read.IsSuccess) { if (!par.UserName.IsNullOrEmpty()) { ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, "ok"); } } else { if (!par.UserName.IsNullOrEmpty()) { ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, -1, "error"); } } } } } } catch (Exception ex) { Logger.Error(ex.Message); Logger.Error(ex.StackTrace); Logger.Error(ex.Source); } }
public void Execute() { var val1 = int.MaxValue; var val2 = long.MaxValue; var bcdVal1 = BCDUtils.ToBCD(val1, 5); var bcdVal2 = BCDUtils.ToBCD(val2, 10); Output.WriteLine("integer value = {0}", val1); Output.WriteLine("BCD value = {0}", BitConverter.ToString(bcdVal1)); Output.WriteLine("long value = {0}", val2); Output.WriteLine("BCD value = {0}", BitConverter.ToString(bcdVal2)); var val3 = BCDUtils.ToInt(bcdVal1); var val4 = BCDUtils.ToLong(bcdVal2); Output.WriteLine("val1 == val3 = {0}", val1 == val3); Output.WriteLine("val2 == val4 = {0}", val2 == val4); }
public List <string> AnSenWaterInject(byte[] data) { List <string> results = new List <string>(); if (data.Length < 8) { return(results); } var client = new ModbusRtuOverTcp(); //配注仪状态: 0 var r0 = data[0]; var status = (BCDUtils.BCDToUshort(r0) >> 4) & 0x01; //阀门状态 var mode = (BCDUtils.BCDToUshort(r0) >> 1) & 0x01; //阀门工作模式 //设定流量回读: 0.17 var t = BCDUtils.BCDToUshort((ushort)(data[2] << 8 | data[3])); var r1 = t / 100.0; //瞬时流量: 0.15 t = BCDUtils.BCDToUshort((ushort)(data[4] << 8 | data[5])); var r2 = t / 100.0; //累计流量: 607.26 var t1 = BCDUtils.BCDToUshort((ushort)(data[6] << 8 | data[7])); var t2 = BCDUtils.BCDToUshort((ushort)(data[8] << 8 | data[9])); var r3 = (t1 * 10000 + t2) / 100.0; //扩展: 0.0 //水井压力: 7.35 t = BCDUtils.BCDToUshort((ushort)(data[14] << 8 | data[15])); var r4 = t / 100.0; var actual = $"流量回读: {r1} ,管压: {r4} ,瞬时流量: {r2} ,表头累计: {r3}"; results.Add(actual); return(results); }
public static IotDataWaterWell GetCacularResult(byte[] data) { //0.5配注 //0.38瞬时 //6682.76 当日累计 //11.53 管压 IotDataWaterWell waterWell = new IotDataWaterWell(); //配注仪状态: 0 var r0 = data[4]; //设定流量回读: 0.17 var t = BCDUtils.BCDToUshort((ushort)(data[5] << 8 | data[6])); var r1 = t / 100.0; //瞬时流量: 0.15 t = BCDUtils.BCDToUshort((ushort)(data[7] << 8 | data[8])); var r2 = t / 100.0; //累计流量: 6682.89 var t1 = BCDUtils.BCDToUshort((ushort)(data[9] << 8 | data[10])); var t2 = BCDUtils.BCDToUshort((ushort)(data[11] << 8 | data[12])); var r3 = t1 * 100 + t2 / 100.0; //扩展: 0.0 //水井压力: 10.83 t = BCDUtils.BCDToUshort((ushort)(data[17] << 8 | data[18])); var r4 = t / 100.0; waterWell.SettedFlow = r1; //设定流量回读 waterWell.TubePressure = r4; //管压 waterWell.InstantaneousFlow = r2; //瞬时流量 waterWell.CumulativeFlow = r3; //表头累计 Console.WriteLine("waterWell:" + waterWell.ToJson()); waterWell.PrintDump(); return(waterWell); }
private static async Task DataReceiver(Metadata md) { var header = "[" + md.TcpClient.Client.RemoteEndPoint.ToString() + "]"; Console.WriteLine(header + " data receiver started"); try { while (true) { if (!IsClientConnected(md.TcpClient)) { Console.WriteLine(header + " client no longer connected"); break; } if (Token.IsCancellationRequested) { Console.WriteLine(header + " cancellation requested"); break; } var data = await DataReadAsync(md.TcpClient, Token); if (data == null || data.Length < 1) { await Task.Delay(30); continue; } Console.WriteLine(data + ": " + BytesToHexString(data)); var length = data.Length; if (data.Length == 3) { var deviceId = data[2]; switch (data[1]) { case 0xC7: Console.WriteLine($"收到设备[0x{deviceId}]注册包"); break; case 0xC8: Console.WriteLine($"收到设备[0x{deviceId}]心跳包"); break; } } if (length == 25) { var crc32 = data.Take(length - 2).ToArray().CRC16(); if (data[length - 2] == crc32[0] && data[length - 1] == crc32[1]) { //DtuId:212 //WellId:3 //配注仪状态: 0 Console.WriteLine($"配注仪状态 : {data[4]}"); //设定流量回读: 0.17 var t = BCDUtils.BCDToUshort((ushort)(data[5] << 8 | data[6])); Console.WriteLine($"设定流量回读 : {t / 100.0}"); //瞬时流量: 0.15 t = BCDUtils.BCDToUshort((ushort)(data[7] << 8 | data[8])); Console.WriteLine($"瞬时流量 : { t / 100.0}"); //累计流量: 607.26 var t1 = BCDUtils.BCDToUshort((ushort)(data[9] << 8 | data[10])); var t2 = BCDUtils.BCDToUshort((ushort)(data[11] << 8 | data[12])); Console.WriteLine($"累计流量 : {(t1 *10000 + t2) / 100.0}"); //扩展: 0.0 //水井压力: 7.35 t = BCDUtils.BCDToUshort((ushort)(data[17] << 8 | data[18])); Console.WriteLine($"水井压力 : {t / 100.0}"); } } } } catch (Exception e) { Console.WriteLine( Environment.NewLine + header + " DataReceiver Exception: " + Environment.NewLine + e.ToString() + Environment.NewLine); } Console.WriteLine(header + " data receiver terminating"); Clients.TryRemove(md.TcpClient.Client.RemoteEndPoint.ToString(), out _); md.Dispose(); }