我这个用C#写的windows服务 quartz定时执行,就这SPM33执行一段时间不执行了,其他的任务还在执行,这还导致服务器CPU上升,这是啥原因

qq_37978691 2024-02-01 15:02:25

 

 

 

 

 

 

  
        [Obsolete]
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                system_dateTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                //初始化设备列表
                InitSubDeviceList();
                //存储日志
                List<RunLogEntity> _runLogList = new List<RunLogEntity>();
                //按照 采集站IP、采集站Port 进行分组,读取4G模块数据
                var _group = treeSubDeviceList.GroupBy(a => new { a.StationIP, a.StationPort });
                foreach (var _ in _group)
                {
                    StationGroupDto groupDto = new StationGroupDto()
                    {
                        StationIP = _.Key.StationIP,
                        StationPort = _.Key.StationPort,
                        DeviceList = _.ToList()
                    };
                    ReadSPM33Data_Gai(groupDto, out List<RunLogEntity> runLogList);
                    _runLogList.AddRange(runLogList);
                }
                //运行日志入库
                if (_runLogList.Count() > 0)
                {
                    runLogService.InsertRunLogList(_runLogList);
                }
            }
            catch (Exception ex)
            {
                //新增运行日志异常信息
                RunLogEntity runLogEntity = new RunLogEntity()
                {
                    ID = Guid.NewGuid(),
                    LogTime = DateTime.Now,
                    LogCategory = (int)LogCategoryEnum.数据采集日志,
                    LogContent = "派诺SPM33计量仪表启动失败," + ex.Message,
                    RunState = (int)ProgramStateEnum.采数程序异常
                };
                runLogService.InsertRunLog(runLogEntity);
            }
        }
[Obsolete]


          [Obsolete]
        public void ReadSPM33Data_Gai(StationGroupDto groupDto, out List<RunLogEntity> runLogList_)
        {
            List<RunLogEntity> runLogList = new List<RunLogEntity>();
            List<PointData> points = new List<PointData>();
            try
            {
                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect(groupDto.StationIP, groupDto.StationPort);
                ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(tcpClient);
                //设置读取超时时间
                master.Transport.ReadTimeout = 2000;
                master.Transport.Retries = 1;
                master.Transport.WaitToRetryMilliseconds = 150;
                if (tcpClient.Connected)
                {
                    foreach (var deviceInfo in groupDto.DeviceList)
                    {
                        if (deviceInfo.PointList.Count() > 0)
                        {
                            try
                            {
                                var data_List = master.ReadHoldingRegisters(Convert.ToByte(deviceInfo.SlaveID), Convert.ToUInt16(0), Convert.ToUInt16(40));
                                foreach (var item in deviceInfo.PointList)
                                {
                                    double Value = 0;
                                    if (item.DataType == (int)DataTypeEnum.二进制16位)
                                    {
                                        int index = Convert.ToInt32(item.Address);
                                        Value = handleDataType(item.DataBit, data_List[index]);
                                    }
                                    else
                                    {
                                        if (item.IsHighLowBit)
                                        {
                                            int index = Convert.ToInt32(item.Address);
                                            Value = CommonHelper.DealWidthLowHighHex_unSigned(data_List[index + 1], data_List[index]) * item.Multiple * item.Ratio;
                                        }
                                        else
                                        {
                                            int index = Convert.ToInt32(item.Address);
                                            Value = data_List[index] * item.Multiple * item.Ratio;
                                        }
                                    }
                                    var point = PointData
                                                 .Measurement(deviceInfo.ParentCode)
                                                 .Tag("PointTag", item.PointTag.ToString())
                                                 .Tag("PointID", item.ID.ToString())
                                                 .Field("Value", Math.Round(Value, 4))
                                                 .Timestamp(TimeZoneInfo.ConvertTimeToUtc(system_dateTime), WritePrecision.Ns);
                                    points.Add(point);

                                    //验证数据是否报警
                                    alarmRecordService.VerifyAlarm(deviceInfo, item, thresholdList, Value);
                                }
                                //通讯正常
                                var entity = breakEventService.WriteCommunicateEvent(deviceInfo, null, 1, deviceInfo.PointList.Count().ToString());
                                runLogList.Add(entity);
                            }
                            catch (Exception ex)
                            {
                                // 通讯异常
                                var entity = breakEventService.WriteCommunicateEvent(deviceInfo, null, 2, ex.Message);
                                runLogList.Add(entity);
                            }
                            //线程休眠
                            Thread.Sleep(1);
                        }
                    }
                }
                else
                {
                    // tcp/ip 连接失败
                    var entity = breakEventService.WriteCommunicateEvent(null, groupDto, 3, "请检查网络通讯环境!");
                    runLogList.Add(entity);
                }
                //释放寄存器资源
                master.Dispose();
                //释放tcp握手资源
                tcpClient.Close();
                //线程休眠
                Thread.Sleep(1);
                //运行日志推出
                runLogList_ = runLogList;
                //往influxdb写入数据
                if (points.Count > 0)
                {
                    helper.WriteData(points);
                }
            }
            catch (Exception ex)
            {
                //新增运行日志异常信息
                RunLogEntity runLogEntity = new RunLogEntity()
                {
                    ID = Guid.NewGuid(),
                    LogTime = DateTime.Now,
                    LogCategory = (int)LogCategoryEnum.数据采集日志,
                    LogContent = "派诺SPM33计量仪表设备数据采集失败," + ex.Message,
                    RunState = (int)ProgramStateEnum.采数程序异常
                };
                runLogService.InsertRunLog(runLogEntity);
                //运行日志推出
                runLogList_ = new List<RunLogEntity>();
            }

        }

 

...全文
成就一亿技术人!
拼手气红包 5.00元
555 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

优质好文,博主的文章细节很到位,兼顾实用性和可操作性,感谢博主的分享,文章思路清晰,图文并茂,详略得当,三连支持,期待博主持续输出好文!

110,203

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧