private (bool IsError, string ErrorMessage, List <Dose> Datas) GetPLCData()
        {
            var         datas            = new List <Dose>();
            PlcMemory   dm               = PlcMemory.DM;
            short       resultCountCells = 0;
            EtherNetPLC cp1              = new EtherNetPLC();

            try
            {
                cp1.Link(_configApp.IpAddress, _configApp.PortAddress, 300);

                cp1.ReadWord(dm, COUNTER_CELLS_FOR_READING, out resultCountCells);

                if (resultCountCells > 0)
                {
                    short   total = (short)(resultCountCells / PACKEGES); //целое число пакетов (кратное числу возможному для приема пакетов за раз).
                    short   mod   = (short)(resultCountCells % PACKEGES); //остаток пакетов от целого числа
                    short[] data  = new short[600];
                    for (int i = 0; i < total; i++)
                    {
                        cp1.ReadWords(dm, (short)(START_CELL + (i * PACKEGES * COUNT_DATAROWS)), (short)((PACKEGES * COUNT_DATAROWS)), out data);
                        datas.AddRange(TransformArrayToDose(data).ToArray());
                    }

                    if (mod > 0)
                    {
                        cp1.ReadWords(dm, (short)(START_CELL + ((short)total * PACKEGES * COUNT_DATAROWS)), (short)((mod * COUNT_DATAROWS)), out data);


                        datas.AddRange(TransformArrayToDose(data).ToArray());
                    }
                    cp1.WriteWord(dm, COUNTER_CELLS_FOR_READING, 0);
                }

                // cp1.WriteWord(dm, COUNTER_CELLS_FOR_READING, 0);
                return(false, "", datas.ToList());
            }
            catch
            {
                return(true, "Connection to PLC didn't establish", null);
            }
            finally
            {
                cp1.Close();
            }
        }
Beispiel #2
0
        private void btnRead_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Stopwatch sp = new System.Diagnostics.Stopwatch();
            sp.Start();
            txtValues.Text = "";
            short[] rd;
            string  address = txtAddress.Text.Trim();
            string  count   = txtCount.Text.Trim();
            short   re      = ENT.ReadWords(PlcMemory.DM, short.Parse(address), short.Parse(count), out rd);

            if (re != 0)
            {
                //写入数据出错
                txtValues.Text = "数据读取出错!";
            }
            else
            {
                for (int i = 0; i < rd.Length; i++)
                {
                    txtValues.Text += rd[i].ToString() + ",";
                }
            }

            //单个读取速度测试
            //txtValues.Text = "";
            //short rd = 0;
            //short address = short.Parse(txtAddress.Text.Trim());
            //short count = short.Parse(txtCount.Text.Trim());
            //for (int i = 0; i < count; i++)
            //{
            //    short add = (short)(address + i);
            //    short re = ENT.ReadWord(EtherNetPLC.PlcMemory.DM_Word, add, out rd);
            //    if (re != 0)
            //    {
            //        //写入数据出错
            //        txtValues.Text = "数据读取出错,已停止!";
            //        break;
            //    }
            //    else
            //    {
            //        txtValues.Text += rd.ToString() + ",";
            //    }
            //    //txtValues.Text += ",数据读取成功!";
            //}
            sp.Stop();
            txtUseTime.Text = sp.Elapsed.TotalMilliseconds.ToString() + "ms";
        }
        public bool ReadMultiElement(PlcScanItems item, short ncount, ref short[] value)
        {
            PlcMemory memorytype   = item.AddressType;
            short     startaddress = short.Parse(item.Address);
            short     count        = ncount;
            short     result       = 0;

            lock (readLock)
            {
                result = omronethernetplc.ReadWords(memorytype, startaddress, ncount, out value);
                if (result == 0)
                {
                    return(true);
                }
                else
                {
                    bConnectOmronPLC = false;
                    return(false);
                }
            }
        }