public List <string> GuoYiWaterInject(byte[] data)
        {
            List <string> results = new List <string>();
            var           client  = new ModbusRtuOverTcp();

            var value8 = client.ByteTransform.TransUInt16(data, 8);
            var value6 = client.ByteTransform.TransUInt16(data, 6);

            var cumulativeFlow = (value6 << 16 | value8) / 100.0;

            results.Add($"表头累计:{cumulativeFlow}");

            var value      = client.ByteTransform.TransInt16(data, 2);
            var settedFlow = value / 100.0; //设定流量回读

            results.Add($"流量回读:{settedFlow}");


            value = client.ByteTransform.TransInt16(data, 14);
            var tubePressure = value / 100.0;//管压

            results.Add($"管压:{tubePressure}");
            value = client.ByteTransform.TransInt16(data, 4);
            var instantaneousFlow = value / 100.0; //瞬时流量

            results.Add($"瞬时流量:{instantaneousFlow}");


            value = client.ByteTransform.TransInt16(data, 0);
            var valveStatus = (value >> 4) & 0x01; //阀门状态
            var valveMode   = (value >> 1) & 0x01; //阀门工作模式

            return(results);
        }
Example #2
0
        public static async Task <bool> GetDateTime(ModbusRtuOverTcp client, int modbusAddress, IotDataOilWellMovefluidlevel dytDiagram, ushort startAddress, ushort regCount, int tryReadTimes = 3)
        {
            var read = await client.ReadAsync($"s={modbusAddress};x=4;{startAddress}", regCount);

            if (read.IsSuccess)
            {
                var year  = client.ByteTransform.TransUInt16(read.Content, 0);
                var month = client.ByteTransform.TransUInt16(read.Content, 1);
                var date  = client.ByteTransform.TransUInt16(read.Content, 2);
                var h     = client.ByteTransform.TransUInt16(read.Content, 3);
                var m     = client.ByteTransform.TransUInt16(read.Content, 4);
                var s     = client.ByteTransform.TransUInt16(read.Content, 5);

                dytDiagram.DateTime = new DateTime(2000 + year, month, date, h, m, s);
                return(true);
            }
            else
            {
                tryReadTimes--;
                if (tryReadTimes > 0)
                {
                    return(await GetDateTime(client, modbusAddress, dytDiagram, startAddress, regCount, tryReadTimes));
                }
                return(false);
            }
        }
Example #3
0
        public static void Get_XAGL_WM2000KZG_ControllerStatus_Mock(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                var iotDataOilWellControllerState = new IotDataOilWellControllerState()
                {
                    NetworkNode = ClientInfo.ManyIpAddress,
                    AlarmCode   = 0,
                    AlarmMsg    = "正常"
                };

                iotDataOilWellControllerState.WellId       = par.DeviceId;
                iotDataOilWellControllerState.DeviceTypeId = par.DeviceTypeId;
                iotDataOilWellControllerState.DateTime     = DateTime.Now;
                iotDataOilWellControllerState.Mock         = par.UseMockData;

                redisClient.AddItemToList("YCIOT:IOT_Data_OilWell_ControllerState", iotDataOilWellControllerState.ToJson().IndentJson());
                redisClient.Set($"Group:OilWell:{par.DeviceName}-{par.DeviceId}:ControllerState", iotDataOilWellControllerState);
                redisClient.Set($"Single:OilWell:ControllerState:{par.DeviceName}-{par.DeviceId}", iotDataOilWellControllerState);
            }
            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);
                }
            }
        }
Example #4
0
        public static (ushort, ushort) GetIDData(byte[] t)
        {
            var v1 = new ModbusRtuOverTcp().ByteTransform.TransUInt16(t, 0);
            var v2 = new ModbusRtuOverTcp().ByteTransform.TransUInt16(t, 2);

            return(v1, v2);
        }
Example #5
0
        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);
            }
        }
Example #6
0
        public static async Task <bool> GetCount(ModbusRtuOverTcp client, int modbusAddress, IotDataOilWellIndicatorDiagram indicatorDiagram, 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);
                indicatorDiagram.Count = value;
                return(true);
            }
            else
            {
                tryReadTimes--;

                if (tryReadTimes > 0)
                {
                    return(await GetCount(client, modbusAddress, indicatorDiagram, startAddress, regCount, tryReadTimes));
                }

                return(false);
            }
        }
Example #7
0
        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);
            }
        }
Example #8
0
        public static void Get_XAGL_WM2000YXGT_IndicatorDiagram_Mock(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                var indicatorDiagram = new IotDataOilWellIndicatorDiagram()
                {
                    AlarmCode = 0,
                    AlarmMsg  = "正常"
                };

                indicatorDiagram.WellId       = par.DeviceId;
                indicatorDiagram.DeviceTypeId = par.DeviceTypeId;
                indicatorDiagram.DateTime     = DateTime.Now;
                indicatorDiagram.Mock         = par.UseMockData;

                redisClient.AddItemToList("YCIOT:IOT_Data_OilWell_IndicatorDiagram", indicatorDiagram.ToJson().IndentJson());
                redisClient.Set($"Group:OilWell:{par.DeviceName}-{par.DeviceId}:IndicatorDiagram", indicatorDiagram);
                redisClient.Set($"Single:OilWell:IndicatorDiagram:{par.DeviceName}-{par.DeviceId}", indicatorDiagram);
            }
            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);
                }
            }
        }
Example #9
0
        public static void Post_XAGL_WM2000KZG_StartWell(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                var modbusAddress = par.ModbusAddress;

                if (!par.UseMockData)
                {
                    var startAddress = (ushort)4131;

                    var address = $"s={par.ModbusAddress};{startAddress}";

                    client.WriteOneRegister(address, (ushort)(1));
                }

                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, "ok");
                }
            }
            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);
                }
            }
        }
Example #10
0
        public static async Task Post_XAGL_WM1000KZG_StopWell(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            //用于通过ServerEvent给调用着返回消息
            if (!par.UserName.IsNullOrEmpty())
            {
                ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, "ok");
            }

            return;

            try
            {
                var modbusAddress = par.ModbusAddress;

                ClientInfo.CurrentModbusPoolAddress = modbusAddress;

                //写入0值到4121(Holding register)寄存器,设置油井的状态为开启。
                // var result2 = await client.WriteAsync("4126", 2);

                var startAddress = (ushort)4126;

                var address = $"s={par.ModbusAddress};{startAddress}";

                var result2 = await client.WriteOneRegisterAsync(address, (ushort)(2));

                if (result2.IsSuccess)
                {
                    //用于通过ServerEvent给调用着返回消息
                    if (!par.UserName.IsNullOrEmpty())
                    {
                        ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, "ok");
                    }
                }
                else
                {
                    //用于通过ServerEvent给调用着返回消息
                    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);

                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, -1, ex.Message);
                }
            }

            //
        }
Example #11
0
        public static async Task Post_BJBC_WII_InjectionAllocation(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            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;

                            //30201 原始地址
                            var startAddress = (ushort)(201 + slotId - 1);

                            var value = (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);

                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, -1, ex.Message);
                }
            }
        }
Example #12
0
        public static async Task Get_LYQH_GJL_Pressure(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                IotDataOilWellPressure oillPress = new IotDataOilWellPressure
                {
                    AlarmCode = 0,
                    AlarmMsg  = "正常"
                };

                var logIotModbusPoll = par.ConvertTo <LogIotModbusPoll>();

                var modbusAddress = par.ModbusAddress;

                ClientInfo.CurrentModbusPoolAddress = modbusAddress;

                oillPress.WellId = par.DeviceId;

                oillPress.DeviceTypeId = par.DeviceTypeId;
                oillPress.Mock         = false;

                var failed = await SetPressure(redisClient, client, modbusAddress, oillPress, logIotModbusPoll, par);

                oillPress.NetworkNode = ClientInfo.ManyIpAddress;

                //用于将读取的结果写入Redis队列
                if (!failed || par.UseMockData)
                {
                    oillPress.Mock = par.UseMockData;
                    redisClient.AddItemToList("YCIOT:IOT_Data_OilWell_Pressure", oillPress.ToJson().IndentJson());
                    redisClient.Set($"Group:OilWell:{par.DeviceName}-{par.DeviceId}:Pressure", oillPress);
                    redisClient.Set($"Single:OilWell:Pressure:{par.DeviceName}-{par.DeviceId}", oillPress);
                }

                //用于通过ServerEvent给调用着返回消息
                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, failed ? -2 : 0, oillPress.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);
                }
            }
        }
Example #13
0
        public static async Task Get_XAGL_WM3000KZG_ControllerParameter(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                var modbusAddress = par.ModbusAddress;

                ClientInfo.CurrentModbusPoolAddress = modbusAddress;

                lock (ClientInfo.locker)
                {
                    ClientInfo.RequestTime     = DateTime.Now;
                    ClientInfo.ExpectedType    = 0x03;
                    ClientInfo.ExpectedDataLen = 10;
                }

                var read = await client.ReadAsync($"s={par.ModbusAddress};x=3;7596", 5);

                if (read.IsSuccess && read.Content.Length == 2 * 5)
                {
                    // 电机额定功率  7596  变比 0.1
                    var value       = client.ByteTransform.TransInt16(read.Content, 0);
                    var eDingGongLv = Math.Round(value * 0.1, 2);

                    // 电机额定电压  7597  变比 1
                    value = client.ByteTransform.TransInt16(read.Content, 2);
                    var eDingDianYa = value;

                    // 电机额定电流  7598  变比 0.01
                    value = client.ByteTransform.TransInt16(read.Content, 4);
                    var eDingDianliu = Math.Round(value * 0.01, 2);

                    // 电机额定频率  7599  变比 0.01
                    value = client.ByteTransform.TransInt16(read.Content, 6);
                    var eDingPingLv = Math.Round(value * 0.01, 2);

                    // 电机额定转速  7600  变比 1
                    value = client.ByteTransform.TransInt16(read.Content, 8);
                    var eDingZhuanSu = value;
                }
            }
            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);
                }
            }
        }
Example #14
0
        public static async Task Get_LYQH_KZG_StartWellTime(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                var modbusAddress = par.ModbusAddress;

                // 读取0x0081  开井时间

                var jo1 = (JObject)JsonConvert.DeserializeObject(par.CommandParameter);

                var slotId = Convert.ToInt32(jo1["1"].ToString());

                var startAddress = (ushort)((slotId * 0x100) + 0x0081);

                lock (ClientInfo.locker)
                {
                    ClientInfo.RequestTime     = DateTime.Now;
                    ClientInfo.ExpectedType    = 0x03;
                    ClientInfo.ExpectedDataLen = 4 * 2;
                }
                var read = await client.ReadAsync($"s={par.ModbusAddress};3;{startAddress}", 4);

                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);

                    var DateTime = new DateTime(2000 + year, month, date, h, m, s);
                }

                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, read.IsSuccess ? 0 : -2, "ok");
                }
            }
            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);
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // 连接
            if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address))
            {
                MessageBox.Show(DemoUtils.IpAddressInputWrong);
                return;
            }


            if (!int.TryParse(textBox2.Text, out int port))
            {
                MessageBox.Show(DemoUtils.PortInputWrong);
                return;
            }


            if (!byte.TryParse(textBox15.Text, out byte station))
            {
                MessageBox.Show("Station input is wrong!");
                return;
            }

            busTcpClient?.ConnectClose( );
            busTcpClient = new ModbusRtuOverTcp(textBox1.Text, port, station);
            busTcpClient.AddressStartWithZero = checkBox1.Checked;


            ComboBox1_SelectedIndexChanged(null, new EventArgs( ));    // 设置数据服务
            busTcpClient.IsStringReverse = checkBox3.Checked;

            try
            {
                OperateResult connect = busTcpClient.ConnectServer( );
                if (connect.IsSuccess)
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess);
                    button2.Enabled = true;
                    button1.Enabled = false;
                    panel2.Enabled  = true;

                    userControlCurve1.ReadWriteNet = busTcpClient;
                }
                else
                {
                    MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed + connect.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #16
0
        public static (ushort, ushort, ushort, ushort) GetMACData(byte[] t)
        {
            //byte[] t = { 0x35, 0x96, 0x50, 0xFE, 0xFF, 0x14, 0x2E, 0x84 };

            var t1 = HiLowTranse(t);

            var v1 = new ModbusRtuOverTcp().ByteTransform.TransUInt16(t1, 0);
            var v2 = new ModbusRtuOverTcp().ByteTransform.TransUInt16(t1, 2);
            var v3 = new ModbusRtuOverTcp().ByteTransform.TransUInt16(t1, 4);
            var v4 = new ModbusRtuOverTcp().ByteTransform.TransUInt16(t1, 6);

            return(v1, v2, v3, v4);
        }
Example #17
0
        public static async Task <bool> GetAll(ModbusRtuOverTcp client, int modbusAddress, IotDataOilWellMovefluidlevel dytDiagram, 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);

                dytDiagram.DateTime = new DateTime(2000 + year, month, date, h, m, s);

                //采样周期
                var value = client.ByteTransform.TransInt16(read.Content, 6);

                dytDiagram.Period = (int)(value * 0.01);

                //模式
                value = client.ByteTransform.TransInt16(read.Content, 8);

                dytDiagram.Model = value;

                //点数
                value = client.ByteTransform.TransInt16(read.Content, 10);

                dytDiagram.Count = value;
                return(true);
            }
            else
            {
                tryReadTimes--;

                if (tryReadTimes > 0)
                {
                    return(await GetAll(client, modbusAddress, dytDiagram, startAddress, regCount, tryReadTimes));
                }

                return(false);
            }
        }
Example #18
0
        public static void Post_XAAS_WII_StopWell(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                var modbusAddress = par.ModbusAddress;

                ClientInfo.CurrentModbusPoolAddress = modbusAddress;

                if (!par.UseMockData)
                {
                    //写入82值到40491(Holding register)寄存器,设置油井的状态为停止。

                    var startAddress = (ushort)40491;

                    var address = $"s={par.ModbusAddress};{startAddress}";

                    client.WriteOneRegister(address, (ushort)(82));

                    //用于通过ServerEvent给调用着返回消息
                    if (!par.UserName.IsNullOrEmpty())
                    {
                        ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, "ok");
                    }
                }
                else
                {
                }
            }
            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);
                }
            }
        }
Example #19
0
        public static async Task <bool> GetMode(ModbusRtuOverTcp client, int modbusAddress, IotDataOilWellMovefluidlevel dytDiagram, ushort startAddress, ushort regCount, int tryReadTimes)
        {
            var read = await client.ReadAsync($"s={modbusAddress};x=4;{startAddress}", regCount);

            if (read.IsSuccess)
            {
                var value = client.ByteTransform.TransInt16(read.Content, 0);
                dytDiagram.Model = value;
                return(true);
            }
            else
            {
                tryReadTimes--;
                if (tryReadTimes > 0)
                {
                    return(await GetMode(client, modbusAddress, dytDiagram, startAddress, regCount, tryReadTimes));
                }
                return(false);
            }
        }
Example #20
0
        public static async Task <bool> SetdytDiagram(RedisClient redisClient, ModbusRtuOverTcp client, int modbusAddress, IotDataOilWellMovefluidlevel dytDiagram, LogIotModbusPoll logIotModbusPoll, ControlRequest par, int slotId, int tryReadTimes = 3)
        {
            var regAddress = (ushort)((slotId * 0x3000) + 1536);  //读取位移数据
            var isSuccess  = await GetAll(client, modbusAddress, dytDiagram, regAddress, 6, tryReadTimes);

            if (!isSuccess)
            {
                FailLog.Write(redisClient, par, dytDiagram, logIotModbusPoll, "Get_LYQH_DYM_Movefluidlevel", "读取动液面异常!");
                return(false);
            }

            ushort step = 100;

            //读取序列数据
            if (dytDiagram.Count <= 6000 && dytDiagram.Count >= step)
            {
                regAddress = (ushort)((slotId * 0x3000) + 1636);  //读取序列数据
                for (var i = 0; i < dytDiagram.Count; i += step)
                {
                    var itemCount = (i + step > dytDiagram.Count) ? (ushort)(dytDiagram.Count - i) : step;

                    Logger.Info($"{i}:{itemCount}:{(ushort)(regAddress + i)}");

                    //读取序列
                    isSuccess = await GetDData(client, modbusAddress, dytDiagram, (ushort)(regAddress + i), itemCount, tryReadTimes);

                    if (!isSuccess)
                    {
                        var message = "从 " + (regAddress + i).ToString() + " 个开始,读取 " + itemCount.ToString() + " 个动液面位移数据异常!";
                        FailLog.Write(redisClient, par, dytDiagram, logIotModbusPoll, "Get_LYQH_DYM_Movefluidlevel", message);
                        return(false);
                    }
                    Thread.Sleep(100);
                }
            }
            else
            {
                return(false);
            }
            return(true);
        }
Example #21
0
        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);
        }
Example #22
0
        public static async Task <bool> SetPressure(RedisClient redisClient, ModbusRtuOverTcp client, int modbusAddress, IotDataOilWellPressure pressure, LogIotModbusPoll logIotModbusPoll, ControlRequest par, int tryReadTimes = 3)
        {
            //获取采集时间

            var failed = await GetDateTime(client, modbusAddress, pressure, (ushort)1, 3, tryReadTimes);

            if (failed)
            {
                FailLog.Write(redisClient, par, pressure, logIotModbusPoll, "Get_LYQH_GJL_Pressure", "读取油井压力数据异常!");
                return(false);
            }

            failed = await GetOilPress(client, modbusAddress, pressure, (ushort)0, 1, tryReadTimes);

            if (failed)
            {
                FailLog.Write(redisClient, par, pressure, logIotModbusPoll, "Get_LYQH_GJL_Pressure", "读取油井压力数据异常!");
                return(false);
            }
            return(true);
        }
Example #23
0
        /// <summary>
        /// 读取配水间干压mock
        /// </summary>
        /// <param name="client"></param>
        /// <param name="redisClient"></param>
        /// <param name="messageString"></param>
        public static void Get_ZJJH_WTPM_TrunkPressure_Mock(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par          = messageString.FromJson <ControlRequest>();
            var waterStation = new IotDataWaterStation();

            waterStation.DeviceTypeId = par.DeviceTypeId;

            try
            {
                waterStation.AlarmCode = 0;
                waterStation.AlarmMsg  = "mock数据";
                waterStation.StationId = par.DeviceId; //注水间ID
                waterStation.Mock      = par.UseMockData;


                waterStation.Mock     = par.UseMockData;
                waterStation.DateTime = DateTime.Now;
                //用于将读取的结果写入Redis队列
                redisClient.AddItemToList("YCIOT:IOT_Data_WaterStation", waterStation.ToJson().IndentJson());
                redisClient.Set($"Group:WaterStation:{par.DeviceName}-{par.DeviceId}", waterStation);

                //用于通过ServerEvent给调用着返回消息
                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, waterStation.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);
                }
            }
        }
Example #24
0
        public static void Get_XAGL_PumpPressure_Mock(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par          = messageString.FromJson <ControlRequest>();
            var pumpPressure = new IotDataPumpPressure();

            pumpPressure.DeviceTypeId = par.DeviceTypeId;

            try
            {
                pumpPressure.AlarmCode = 0;
                pumpPressure.AlarmMsg  = "mock数据";
                pumpPressure.Mock      = par.UseMockData;

                pumpPressure.PumpId   = par.DeviceId; //泵编号
                pumpPressure.Mock     = par.UseMockData;
                pumpPressure.DateTime = DateTime.Now;
                //用于将读取的结果写入Redis队列
                redisClient.AddItemToList("YCIOT:IOT_Data_PumpPressure", pumpPressure.ToJson().IndentJson());
                redisClient.Set($"Group:PumpPressure:{par.DeviceName}-{par.DeviceId}", pumpPressure);


                //用于通过ServerEvent给调用着返回消息
                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, pumpPressure.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);
                }
            }
        }
Example #25
0
        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);
        }
Example #26
0
        public static async Task POST_LYQH_KZG_controllerstate(ModbusRtuOverTcp client, RedisClient redisClient, string messageString, int state = 1)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                var modbusAddress = par.ModbusAddress;

                // 0 - 关井,1 - 开井
                //写入1值到0x0080(Holding register)寄存器,设置油井的状态为开启或者关闭

                var jo1 = (JObject)JsonConvert.DeserializeObject(par.CommandParameter);

                var slotId = Convert.ToInt32(jo1["1"].ToString());

                var startAddress = (ushort)((slotId * 0x100) + 0x0080);

                var read = await client.WriteAsync($"s={par.ModbusAddress};{startAddress}", new byte[] { 00, (byte)state, 20, 07, 23, 11, 34, 00 });

                if (read.IsSuccess)
                {
                }
                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, read.IsSuccess ? 0 : -2, "ok");
                }
            }
            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);
                }
            }
        }
Example #27
0
        /// <summary>
        /// 设置冲次
        /// </summary>
        /// <param name="client"></param>
        /// <param name="redisClient"></param>
        /// <param name="messageString"></param>
        /// <returns></returns>
        public static async Task Post_XAGL_WM2000KZG_StrokeFrequency(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par = messageString.FromJson <ControlRequest>();

            try
            {
                var modbusAddress = par.ModbusAddress;

                ClientInfo.CurrentModbusPoolAddress = modbusAddress;

                var stroke = (int)(float.Parse(par.CommandParameter) * 10);

                //4132(Holding register)寄存器为47,代表当前设定冲次4.7次。

                var startAddress = (ushort)4132;

                var address = $"s={par.ModbusAddress};{startAddress}";

                await client.WriteOneRegisterAsync(address, (ushort)(stroke));


                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, "ok");
                }
            }
            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);
                }
            }
        }
Example #28
0
        public static void Get_XAGY_WII_WaterInjectingInstrument_Mock(ModbusRtuOverTcp client, RedisClient redisClient, string messageString)
        {
            var par       = messageString.FromJson <ControlRequest>();
            var waterWell = new IotDataWaterWell
            {
                AlarmCode = 0,
                AlarmMsg  = ""
            };

            try
            {
                waterWell.AlarmCode = 0;
                waterWell.AlarmMsg  = "Mock数据";

                waterWell.Mock     = par.UseMockData;
                waterWell.WellId   = par.DeviceId;
                waterWell.DateTime = DateTime.Now;
                redisClient.AddItemToList("YCIOT:IOT_Data_WaterWell", waterWell.ToJson().IndentJson());
                redisClient.Set($"Group:WaterWell:{par.DeviceName}-{par.DeviceId}", waterWell);

                //用于通过ServerEvent给调用着返回消息
                if (!par.UserName.IsNullOrEmpty())
                {
                    ServerEventHelper.SendSseMessage(par.UserName, par.SessionId, 0, waterWell.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);
                }
            }
        }
Example #29
0
        public static async Task <bool> GetCData(ModbusRtuOverTcp client, int modbusAddress, IotDataOilWellCurrentDiagram currentDiagram, 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)
            {
                for (var j = 0; j < regCount; j++)
                {
                    var value = client.ByteTransform.TransInt16(read.Content, j * 2);
                    if (value != 0)
                    {
                        //变比0.001,没有按协议文档,按实际的情况定
                        var L = value * 0.001;
                        currentDiagram.C.Add(Math.Round(L, 2));
                    }
                    else
                    {
                        currentDiagram.C.Add(value);
                    }
                }
                return(true);
            }
            else
            {
                tryReadTimes--;
                if (tryReadTimes > 0)
                {
                    return(await GetCData(client, modbusAddress, currentDiagram, startAddress, regCount, tryReadTimes));
                }
                return(false);
            }
        }
Example #30
0
        public static async Task <bool> GetDData(ModbusRtuOverTcp client, int modbusAddress, IotDataOilWellMovefluidlevel dytDiagram, 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)
            {
                for (var j = 0; j < regCount; j++)
                {
                    //无符号数0-65535
                    var value = client.ByteTransform.TransUInt16(read.Content, j * 2);
                    if (value != 0)
                    {
                        var L = value;
                        dytDiagram.Y.Add(value);
                    }
                    else
                    {
                        dytDiagram.Y.Add(value);
                    }
                }
                return(true);
            }
            else
            {
                tryReadTimes--;
                if (tryReadTimes > 0)
                {
                    return(await GetDData(client, modbusAddress, dytDiagram, startAddress, regCount, tryReadTimes));
                }
                return(false);
            }
        }