Exemple #1
0
 public ThreadingTest(int length, string Addr, string IP, comm.DataTyte dataTyte, string ThreadCount = null)
 {
     this._DataLength  = length;
     this.Addr         = Addr;
     this.IP           = IP;
     _hda              = new List <HistoryData>();
     this._ThreadCount = ThreadCount;
     this._dataTyte    = dataTyte;
     AsyncStart();
 }
Exemple #2
0
        private void CreateStart(int length, string Addr, string IP, comm.DataTyte dataTyte, string ThreadCount = null)
        {
            if (!radioButton1.Checked)
            {
                PLCTextBox = new LIBnodavePLC();
            }
            else
            {
                PLCTextBox = new SenmensPLC();
            }
            ThreadingTest threadingTest = new ThreadingTest(length, Addr, IP, dataTyte, PLCTextBox, ThreadCount);

            threadingTest.MessageEventHandler += UpdateUI;
            threadingTest.AsyncStart();
            threadingTests.Add(threadingTest);
        }
Exemple #3
0
 //private IPLC PLC;
 #region 单DB块构造函数
 /// <summary>
 /// 单块线程,一个线程一个DB块
 /// </summary>
 /// <param name="length">数据长度</param>
 /// <param name="Addr">地址块</param>
 /// <param name="IP">PLC的IP地址</param>
 /// <param name="dataTyte">数据类型</param>
 /// <param name="ThreadCount">线程数,不设置为空</param>
 public ThreadingTest(int length, string Addr, string IP, comm.DataTyte dataTyte, IPLC PLCTextBox, string ThreadCount = null)
 {
     isFirst         = true;
     timer1.Elapsed += timer1_Elapsed;
     timer1.Interval = 3000;
     logNet          = new LogNetSingle("Treadinglog.txt");
     _PLCDriver      = PLCTextBox;
     //PLC = new SenmensPLC();
     this._DataLength  = length;
     this._Addr        = Addr;
     this._IP          = IP;
     _hda              = new List <HistoryData>();
     this._ThreadCount = ThreadCount;
     this._dataTyte    = dataTyte;
     Name              = "单线程读取,地址:" + _Addr + "PLC IP:" + IP + "\r\n";
     timer1.Start();
 }
Exemple #4
0
        /// <summary>
        /// 同一PLC不同DB块同时读取数据
        /// </summary>
        /// <param name="pLCAddrLists"></param>
        /// <returns><eturns>
        public int AsyncMultiDBStart()
        {
            _hda = new List <HistoryData>();
            List <PLCAddrList> pLCAddrLists = GetPLCAddrLists(_Addr);
            CancellationToken  token        = tokenSource.Token;

            _dataTyte     = comm.DataTyte.STRING;
            _PLCDriver.IP = this._IP;
            _PLCDriver.Connect();
            if (IsConnect)
            {
                isFirst = false;
                Result  = "与PLC连接成功:" + _IP + "地址为" + _Addr + "---------" + DateTime.Now + "\r\n";
            }
            else
            {
                Result = "与PLC连接失败:" + _IP + "地址为" + _Addr + "---------" + DateTime.Now + "\r\n";
            }


            Task task = new Task(() =>
            {
                while (true)
                {
                    try
                    {
                        string send   = null;
                        string recive = null;
                        bool check    = true;
                        double during = 0;
                        int length    = 0;
                        string DBAddr = null;
                        foreach (var item in pLCAddrLists)
                        {
                            if (token.IsCancellationRequested)
                            {
                                _hda.Clear();
                                _PLCDriver.Dispose();
                                throw new OperationCanceledException(token);
                            }
                            send = GetRandomString(item.DataLenth * 2);

                            _PLCDriver.Address = item.Addr;

                            int ret = _PLCDriver.WriteBytes(SoftBasic.HexStringToBytes(send));
                            if (ret != 0)
                            {
                                string result = item.Addr + "写数据失败:" + _PLCDriver.ErrorCode + "---------" + DateTime.Now + "\r\n";
                                if (MessageEventHandler != null)
                                {
                                    MessageEventHandler(result, null);
                                }
                            }
                            ret = _PLCDriver.ReadBytesResult(item.DataLenth);
                            if (ret != 0)
                            {
                                string result = _IP + item.Addr + "读数据失败:" + _PLCDriver.ErrorCode + "---------" + DateTime.Now + "\r\n";
                                if (MessageEventHandler != null)
                                {
                                    MessageEventHandler(result, null);
                                }
                            }
                            byte[] ReciveData = _PLCDriver.ReadBuff as byte[];
                            recive            = SoftBasic.ByteToHexString(ReciveData);

                            during += (_PLCDriver.Readtime - _PLCDriver.Sendtime).TotalMilliseconds;
                            check  &= send.Equals(recive);
                            DBAddr += item.Addr;
                            length += item.DataLenth;
                        }
                        _hda.Add(new HistoryData(during, length, check, DateTime.Now, DBAddr));   // UpdateUIDelegate(LogMessage);
                        if (ListData.Count > 10)
                        {
                            switch (DataTyte)
                            {
                            case comm.DataTyte.BOOL:

                                break;

                            case comm.DataTyte.WORD:

                                SaveData <HistoryData>("HistoryData_Word" + ThreadCount, ListData);
                                break;

                            case comm.DataTyte.INT16:

                                SaveData <HistoryData>("HistoryData_Int16" + ThreadCount, ListData);
                                break;

                            case comm.DataTyte.INT32:
                                SaveData <HistoryData>("HistoryData_Int32" + ThreadCount, ListData);
                                break;

                            case comm.DataTyte.REAL:
                                SaveData <HistoryData>("HistoryData_real" + ThreadCount, ListData);
                                break;

                            case comm.DataTyte.STRING:
                                SaveData <HistoryData>("HistoryData_str" + ThreadCount, ListData);
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (MessageEventHandler != null)
                        {
                            MessageEventHandler(_PLCDriver.ErrorCode + ex.Message.ToString(), null);
                        }
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }
                    }
                }
            }, token);

            task.Start();
            return(0);
        }