public static async Task <bool> GetGJL(ModbusRtuOverTcp client, int modbusAddress, IotDataOilOmeter tankmeasure, ushort startAddress, ushort regCount, int tryReadTimes) { lock (ClientInfo.locker) { ClientInfo.RequestTime = DateTime.Now; ClientInfo.ExpectedType = 0x04; ClientInfo.ExpectedDataLen = regCount * 2; } var read = await client.ReadAsync($"s={modbusAddress};x=4;{startAddress}", regCount); if (read.IsSuccess) { var value = client.ByteTransform.TransInt16(read.Content, 0); //毫米 tankmeasure.CurrentElevation = value; return(true); } else { tryReadTimes--; if (tryReadTimes > 0) { return(await GetGJL(client, modbusAddress, tankmeasure, startAddress, regCount, tryReadTimes)); } return(false); } }
public static async Task <bool> SetGJL(RedisClient redisClient, ModbusRtuOverTcp client, int modbusAddress, ushort startAddress, IotDataOilOmeter tankmeasure, LogIotModbusPoll logIotModbusPoll, ControlRequest par, int tryReadTimes = 3) { //获取采集时间 var address = startAddress + 1; var isSuccess = await GetDateTime(client, modbusAddress, tankmeasure, (ushort)address, 3, tryReadTimes); if (!isSuccess) { FailLog.Write(redisClient, par, tankmeasure, logIotModbusPoll, "Get_LYQH_GJL_Tankmeasure", "读取液位罐计量时间数据异常!"); return(false); } isSuccess = await GetGJL(client, modbusAddress, tankmeasure, startAddress, 1, tryReadTimes); if (!isSuccess) { FailLog.Write(redisClient, par, tankmeasure, logIotModbusPoll, "Get_LYQH_GJL_Tankmeasure", "读取液位罐计量数据异常!"); return(false); } return(true); }
public static async Task Get_LYQH_GJL_Tankmeasure(ModbusRtuOverTcp client, RedisClient redisClient, string messageString) { var par = messageString.FromJson <ControlRequest>(); try { var tankmeasure = new IotDataOilOmeter() { AlarmCode = 0, AlarmMsg = "正常" }; var logIotModbusPoll = par.ConvertTo <LogIotModbusPoll>(); var modbusAddress = par.ModbusAddress; ClientInfo.CurrentModbusPoolAddress = modbusAddress; tankmeasure.DateTime = DateTime.Now; tankmeasure.OilOmeterId = par.DeviceId; tankmeasure.DeviceTypeId = par.DeviceTypeId; tankmeasure.Mock = false; //从罐Id中获取大罐编号(0-3),此编号是协议中定的 var gId = int.Parse(par.DeviceId.ToString().Last().ToString()); var jo1 = (JObject)JsonConvert.DeserializeObject(par.CommandParameter); var slotId = Convert.ToInt32(jo1["1"].ToString()); var startAddress = (ushort)(slotId * 0x600 + gId * 16); var isSuccess = await SetGJL(redisClient, client, modbusAddress, startAddress, tankmeasure, logIotModbusPoll, par); tankmeasure.NetworkNode = ClientInfo.ManyIpAddress; //用于将读取的结果写入Redis队列 if (isSuccess || par.UseMockData) { tankmeasure.Mock = par.UseMockData; redisClient.AddItemToList("YCIOT:IOT_Data_WellField_Tankmeasure", tankmeasure.ToJson().IndentJson()); redisClient.Set($"Group:OilWell:{par.DeviceName}-{par.DeviceId}:Tankmeasure", tankmeasure); redisClient.Set($"Single:OilWell:Tankmeasure:{par.DeviceName}-{par.DeviceId}", tankmeasure); } //用于通过ServerEvent给调用着返回消息 if (!par.UserName.IsNullOrEmpty()) { ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, isSuccess ? 0 : -2, tankmeasure.ToJson().IndentJson()); } } catch (Exception ex) { Logger.Error(ex.Message); Logger.Error(ex.StackTrace); Logger.Error(ex.Source); if (!par.UserName.IsNullOrEmpty()) { ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, -1, ex.Message); } } }
public static async Task <bool> GetDateTime(ModbusRtuOverTcp client, int modbusAddress, IotDataOilOmeter tankmeasure, ushort startAddress, ushort regCount, int tryReadTimes) { lock (ClientInfo.locker) { ClientInfo.RequestTime = DateTime.Now; ClientInfo.ExpectedType = 0x04; ClientInfo.ExpectedDataLen = regCount * 2; } var read = await client.ReadAsync($"s={modbusAddress};x=4;{startAddress}", regCount); if (read.IsSuccess) { var year = client.ByteTransform.TransByte(read.Content, 0); var month = client.ByteTransform.TransByte(read.Content, 1); var date = client.ByteTransform.TransByte(read.Content, 2); var h = client.ByteTransform.TransByte(read.Content, 3); var m = client.ByteTransform.TransByte(read.Content, 4); var s = client.ByteTransform.TransByte(read.Content, 5); if (year != 0 && month != 0 && date != 0) { tankmeasure.DateTime = new DateTime(2000 + year, month, date, h, m, s); } return(true); } else { tryReadTimes--; if (tryReadTimes > 0) { return(await GetDateTime(client, modbusAddress, tankmeasure, startAddress, regCount, tryReadTimes)); } return(false); } }