Exemple #1
0
 public PLCTest()
 {
     plcUtils = PLCSerialPortUtils.GetInstance(this);
     plcUtils.Open();
 }
        public void Start()
        {
            IsStart = true;
            Task.Run(() =>
            {
                while (IsStart)
                {
                    lock (hasReadHelper)
                    {
                        if (HasRead == false)
                        {
                            continue;
                        }
                    }
                    if (priority2Queue.IsEmpty == false)
                    {
                        if (currentQueue == CurrentQueue.None || currentQueue == CurrentQueue.CCD2Queue)
                        {
                            CCDSendData prevCommand;
                            // 尝试取出一个
                            if (priority2Queue.TryPeek(out prevCommand))
                            {
                                currentQueue = CurrentQueue.CCD2Queue;
                                // 判断是否已经发送
                                if (prevCommand.IsSending == true)
                                {
                                    if (prevCommand.HasSend == false)
                                    {
                                        // 出队
                                        while (priority2Queue.TryDequeue(out prevCommand) == false)
                                        {
                                        }
                                        TimeWatcher.SendedCCD2Time = DateTime.Now;
                                        myEventLog.LogInfo($"收到CCD2返回信号");
                                        myEventLog.LogInfo($"开始发送指令信号到收到返回信号时间间隔:" + (TimeWatcher.SendedCCD2Time - TimeWatcher.SendCCD2Time).TotalMilliseconds);
                                        // 已经发送:调用回调函数
                                        prevCommand.OnSend();
                                        prevCommand.HasSend = true;
                                        currentQueue        = CurrentQueue.None;
                                        continue;
                                    }
                                }
                                else
                                {
                                    lock (hasReadHelper)
                                    {
                                        HasRead = false;
                                    }

                                    prevCommand.IsSending    = true;
                                    TimeWatcher.SendCCD2Time = DateTime.Now;
                                    myEventLog.LogInfo($"发送CCD2指令");
                                    myEventLog.LogInfo($"收到信号到开始发送指令信号时间间隔:" + (TimeWatcher.SendCCD2Time - TimeWatcher.Receive84Time).TotalMilliseconds);
                                    myEventLog.LogInfo($"识别出有效信号到开始发送指令信号时间间隔:" + (TimeWatcher.SendCCD2Time - TimeWatcher.ReceiveEffect84Time).TotalMilliseconds);
                                    myEventLog.LogInfo($"正在发送CCD2成功或失败:{prevCommand.Command}");
                                    PLCSerialPortUtils.GetInstance(serialPortInterface).SendData(prevCommand.Command);
                                    continue;
                                }
                            }
                        }
                    }
                    if (priority1Queue.IsEmpty == false)
                    {
                        if (currentQueue == CurrentQueue.None || currentQueue == CurrentQueue.CCD1Queue)
                        {
                            CCDSendData prevCommand;
                            // 尝试取出一个
                            if (priority1Queue.TryPeek(out prevCommand))
                            {
                                currentQueue = CurrentQueue.CCD1Queue;
                                // 判断是否已经发送
                                if (prevCommand.IsSending == true)
                                {
                                    if (prevCommand.HasSend == false)
                                    {
                                        // 出队
                                        while (priority1Queue.TryDequeue(out prevCommand) == false)
                                        {
                                        }
                                        // 已经发送:调用回调函数
                                        prevCommand.OnSend();
                                        prevCommand.HasSend = true;
                                        currentQueue        = CurrentQueue.None;
                                        continue;
                                    }
                                }
                                else
                                {
                                    lock (hasReadHelper)
                                    {
                                        HasRead = false;
                                    }

                                    prevCommand.IsSending = true;
                                    myEventLog.LogInfo($"正在发送CCD1成功或失败:{prevCommand.Command}");
                                    PLCSerialPortUtils.GetInstance(serialPortInterface).SendData(prevCommand.Command);
                                    continue;
                                }
                            }
                        }
                    }
                    if (queue.IsEmpty)
                    {
                        continue;
                    }
                    string command = "";

                    if (currentQueue == CurrentQueue.None || currentQueue == CurrentQueue.OtherQueue)
                    {
                        if (queue.TryDequeue(out command))
                        {
                            lock (hasReadHelper)
                            {
                                HasRead = false;
                            }
                            if (command.Contains("WDD0090100901"))
                            {
                                myEventLog.LogInfo($"正在发送CCD1成功或失败:{command}");
                                //myEventLog.LogInfo($"发送信号时间间隔:CCD结果:{(DateTime.Now - prevSendTime).TotalMilliseconds}");
                            }
                            else
                            if (command.Contains("WDD0090000900"))
                            {
                                myEventLog.LogInfo($"正在发送CCD2成功或失败:{command}");
                                //myEventLog.LogInfo($"发送信号时间间隔:CCD结果:{(DateTime.Now - prevSendTime).TotalMilliseconds}");
                            }
                            else
                            if (command.Contains("00201"))
                            {
                                myEventLog.LogInfo($"正在发送开始打印命令:{command}");
                                //myEventLog.LogInfo($"发送信号时间间隔:发送:{(DateTime.Now - prevSendTime).TotalMilliseconds}");
                            }
                            else
                            if (command.Contains("00291"))
                            {
                                myEventLog.LogInfo($"正在发送停止打印命令:{command}");
                                //myEventLog.LogInfo($"发送信号时间间隔:停止:{(DateTime.Now - prevSendTime).TotalMilliseconds}");
                            }
                            else
                            if (command.Contains("R00050"))
                            {
                                myEventLog.LogInfo($"发送启动传送带命令:{command}");
                                //myEventLog.LogInfo($"发送信号时间间隔:停止:{(DateTime.Now - prevSendTime).TotalMilliseconds}");
                            }
                            if (command.Contains("R00051"))
                            {
                                myEventLog.LogInfo($"发送停止传送带命令:{command}");
                                //myEventLog.LogInfo($"发送信号时间间隔:停止:{(DateTime.Now - prevSendTime).TotalMilliseconds}");
                            }
                            else
                            {
                                //myEventLog.LogInfo($"发送信号时间间隔:读取:{(DateTime.Now - prevSendTime).TotalMilliseconds}");
                            }
                            //prevSendTime = DateTime.Now;
                            PLCSerialPortUtils.GetInstance(serialPortInterface).SendData(command);
                            //Thread.Sleep(30);
                        }
                    }
                }
            });
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="serialPortInterface">PLC</param>
 /// <param name="frequent">发送监听时间间隔(单位毫秒)</param>
 /// <param name="data">发送内容</param>
 public PLCReader(PLCSerialPortInterface serialPortInterface, int frequent, string data)
 {
     plcUtils  = PLCSerialPortUtils.GetInstance(serialPortInterface);
     Frequent  = frequent;
     this.data = data;
 }