多线程通信连接方法 PLCCommunication

//一般通信连接读取数据时都是需要另开线程操作

            CommMethods.plcDevice.Cts = new CancellationTokenSource();
            Task.Run(new Action(() =>
            {
                PLCCommunication(CommMethods.plcDevice);
            }), CommMethods.plcDevice.Cts.Token);

//通信连接步骤,根据连接状态(连接标志位)进行解析或重连,重连根据是否首次连接标志位判定连接情况(先断开,再连接)
// 设备对象和通信对象,设备对象即设备参数化的实体对象,通信对象即实际的通信载体

     /// <summary>
    /// 多线程通信连接方法
    /// </summary>
    /// <param name="Device"></param>
    private void PLCCommunication(ModbusRTUDevice device)
    {
        while (!device.Cts.IsCancellationRequested)
        {
            if (device.IsConnected)
            {
                //读取解析
                foreach (var group in device.GroupList)
                {
                    if (group.IsActive)
                    {
                        var result=   GetGroupValue(group);
                        if (result.IsSuccess) 
                        {
                            // 设置通信状态
                            device.ErrorTimes = 0;
                        }
                        else
                        {
                            device.ErrorTimes ++;
                            if (device.ErrorTimes>= device.AllowErrorTimes)
                            {
                                //设置通信状态
                                //线断不能重连,串口不存在重连
                                if (IsComExits(device.PortName))
                                {
                                    //延时
                                    Thread.Sleep(10);
                                    continue;
                                }
                                else
                                {
                                    device.IsConnected = false;
                                    //设置通信状态
                                    //写入日志
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else //断线重连
            {
                //根据是否首次连接进行判断
                if (!device.IsFirstConnect)
                {
                    Thread.Sleep(device.ReConnectTime);
                    CommMethods.plc?.DisConnect();
                }
                //连接PLC
              var result=  CommMethods.plc.Connect(device.PortName, device.BaudRate, device.DataBits, device.Parity, device.StopBits);
                if (result)
                {
                    device.IsConnected = true;
                    //设置通信状态
                    //写入日志
                }
                else
                {
                    device.IsConnected = false;
                    //设置通信状态
                    //写入日志
                }
                //清除首次连接标志位
                if (device.IsFirstConnect) 
                {
                    device.IsFirstConnect= false;
                }
            }
        }
    }
posted @ 2024-03-20 12:45  $虫虫$  阅读(34)  评论(0)    收藏  举报