Beispiel #1
0
        private void btnCon_Click(object sender, EventArgs e)
        {
            string s = btnCon.Text.Trim();

            if (s == "连接")
            {
                string plcip   = txtPlcip.Text.Trim();
                string plcport = txtPlcport.Text.Trim();
                ENT = new EtherNetPLC();
                short re = ENT.Link(plcip, short.Parse(plcport), 500);
                if (re == 0)
                {
                    btnCon.Text    = "断开";
                    txtPCNode.Text = ENT.PCNode.ToString();
                }
                else
                {
                    MessageBox.Show("连接出错!");
                }
            }
            else
            {
                short re = ENT.Close();
                if (re == 0)
                {
                    btnCon.Text    = "连接";
                    txtPCNode.Text = "pcNode";
                }
                else
                {
                    MessageBox.Show("断开出错!");
                }
            }
        }
        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 #3
0
 /// <summary>
 /// 测试Fins通讯是否正常
 /// </summary>
 /// <returns></returns>
 public bool TestFinsConnection()
 {
     try
     {
         mOmronFins = new EtherNetPLC();
         short conn = mOmronFins.Link(mPLCIP, mPLCPort, 1500);
         if (conn == 0)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception)
     {
         return(false);
     }
 }
Beispiel #4
0
 /// <summary>
 /// 初始化Omron Fins通讯
 /// </summary>
 /// <returns></returns>
 public bool InitializeOmronFins()
 {
     try
     {
         // mOmronFins.Close();
         mOmronFins = new EtherNetPLC();
         short conn = mOmronFins.Link(mPLCIP, mPLCPort, 1500);
         if (conn == 0)
         {
             mFinsConnStatus = true;
         }
         else
         {
             mFinsConnStatus = false;
         }
         return(mFinsConnStatus);
     }
     catch (Exception)
     {
         // MessageBox.Show("PLC连接失败");
     }
     return(false);
 }
 public OmronFinsAPI()
 {
     omronethernetplc = new EtherNetPLC(System.Net.Sockets.ProtocolType.Udp);
 }