Ejemplo n.º 1
0
        /// <summary>
        /// 异步写
        /// </summary>
        /// <param name="writeItemNames"></param>
        /// <param name="writeItemValues"></param>
        public void AsyncWrite(string[] writeItemNames, string[] writeItemValues)
        {
            OPCItem[] bItem = new OPCItem[writeItemNames.Length];
            for (int i = 0; i < writeItemNames.Length; i++)
            {
                for (int j = 0; j < itemNames.Count; j++)
                {
                    if (itemNames[j] == writeItemNames[i])
                    {
                        bItem[i] = opcItems.GetOPCItem(itemHandleServer[j]);
                        break;
                    }
                }
            }
            int[] temp = new int[writeItemNames.Length + 1];
            temp[0] = 0;
            for (int i = 1; i < writeItemNames.Length + 1; i++)
            {
                temp[i] = bItem[i - 1].ServerHandle;
            }
            Array serverHandles = (Array)temp;

            object[] valueTemp = new object[writeItemNames.Length + 1];
            valueTemp[0] = "";
            for (int i = 1; i < writeItemNames.Length + 1; i++)
            {
                valueTemp[i] = writeItemValues[i - 1];
            }
            Array values = (Array)valueTemp;
            Array Errors;
            int   cancelID;

            opcGroup.AsyncWrite(writeItemNames.Length, ref serverHandles, ref values, out Errors, 2009, out cancelID);
            GC.Collect();
        }
Ejemplo n.º 2
0
 //定时器,发送数据到OPC;R 20170314 发送数据到和PLC直接建立通讯的OPC服务器,相当于写数据到PLC
 private void TimerOpc_Tick(object sender, EventArgs e)
 {
     try
     {
         #region 发送到opc
         if (!opcConnected)
         {
             return;
         }
         for (int i = 8; i <= 15; i++)
         {
             //发送
             int[] temp          = { 0, kepItem[i].ServerHandle };//R 20170318注意格式
             Array serverHandles = temp;
             //object[] valueTemp = { "", KepDataWrite[i - 8] };
             IEnumerable wrtArr = null;
             wrtArr = Setting.AreaFlag ? KepDataWrite[i - 8] : ((i == 12 || i == 13) ? (IEnumerable)ABDataWrite[i - 12] : KepDataWrite[i - 8]);
             object[] valueTemp = { "", wrtArr };
             Array    values    = valueTemp;
             Array    errors;
             int      cancelId;
             kepGroup.AsyncWrite(1, ref serverHandles, ref values, out errors, i, out cancelId);
             //GC.Collect();//
         }
         #endregion
     }
     catch (Exception err)
     {
         //C.LogOpc.Info("OPC数据发送错误:" + err.Message);
     }
 }
Ejemplo n.º 3
0
        //发送异步写数据指令
        private void button3_Click(object sender, EventArgs e)
        {
            Array AsyncValue_Wt;
            Array SerHandles;

            object[] tmpWtData   = new object[5];//写入的数据必须是object型的,否则会报错
            int[]    tmpSerHdles = new int[5];
            //将输入数据赋给数组,然后再转成Array型送给AsyncValue_Wt
            tmpWtData[1]  = (object)this.textBox1.Text.Trim();
            tmpWtData[2]  = (object)this.textBox2.Text.Trim();
            tmpWtData[3]  = (object)this.textBox2.Text.Trim();
            tmpWtData[4]  = (object)this.textBox1.Text.Trim();
            AsyncValue_Wt = (Array)tmpWtData;
            //将输入数据送给的Item对应服务器句柄赋给数组,然后再转成Array型送给SerHandles
            tmpSerHdles[1] = Convert.ToInt32(lserverhandles.GetValue(1));
            tmpSerHdles[2] = Convert.ToInt32(lserverhandles.GetValue(2));
            tmpSerHdles[3] = Convert.ToInt32(lserverhandles.GetValue(5));
            tmpSerHdles[4] = Convert.ToInt32(lserverhandles.GetValue(4));
            SerHandles     = (Array)tmpSerHdles;
            objGroup.AsyncWrite(4, ref SerHandles, ref AsyncValue_Wt, out lErrors_Wt, lTransID_Wt, out lCancelID_Wt);


            //用objChangeItem[1].Write用这句可也以写入数据,但并不触发写入事件。
            objChangeItem[1] = objItems.GetOPCItem(Convert.ToInt32(lserverhandles.GetValue(1)));
            //objChangeItem[1].Write(102);
        }
Ejemplo n.º 4
0
        public void AsyncWrite(string[] sItemsID, object[] ivalue, string sGroupName, int iTransactionID, out Array Result)
        {
            try
            {
                int      iNumItem       = sItemsID.Length;
                int[]    iServerHandler = GetServerHandles(sItemsID);
                object[] oValue         = new object[ivalue.Length + 1];
                for (int i = 0; i < ivalue.Length; i++)
                {
                    oValue[i + 1] = ivalue[i];
                }
                Array aServerHandler = (Array)iServerHandler;
                Array aValue         = (Array)oValue;
                Array aError;

                int iCanceID;
                KepGroup = KepGroups.GetOPCGroup(sGroupName);
                KepGroup.AsyncWrite(iNumItem, ref aServerHandler, ref aValue, out aError, iTransactionID, out iCanceID);
                Result = aError;
            }
            catch
            {
                // Console.WriteLine("************** " + "AsyncWrite Error" + " **************");
                Result = null;
            }
        }
 /// <summary>
 /// 【按钮】写入
 /// </summary>
 private void btnWrite_Click(object sender, EventArgs e)
 {
     OPCItem bItem = KepItems.GetOPCItem(itmHandleServer);
     int[] temp = new int[2] { 0, bItem.ServerHandle };
     Array serverHandles=(Array)temp;
     object[] valueTemp = new object[2] {"",txtWriteTagValue.Text };
     Array values=(Array)valueTemp;
     Array Errors;
     int cancelID;
     KepGroup.AsyncWrite(1,ref serverHandles,ref values,out Errors, 2009,out cancelID);
     //KepItem.Write(txtWriteTagValue.Text);//这句也可以写入,但并不触发写入事件
     GC.Collect();
 }
Ejemplo n.º 6
0
        public void WriteTagValue(
            int tagServerHandle,
            string tagValue,
            out int errCode,
            out string errText)
        {
            errCode = 0;
            errText = "写入完成";

            OPCItem item = kepItems.GetOPCItem(tagServerHandle);

            if (item != null)
            {
                try
                {
                    //item.Write(tagValue);

                    int[] temp = new int[2] {
                        0, item.ServerHandle
                    };
                    Array    serverHandles = (Array)temp;
                    object[] valueTemp     = new object[2] {
                        "", tagValue
                    };
                    Array values = (Array)valueTemp;
                    Array error;
                    int   cancelID;
                    kepGroup.AsyncWrite(
                        1,
                        ref serverHandles,
                        ref values,
                        out error,
                        transactionID++,
                        out cancelID);
                }
                catch (Exception error)
                {
                    errCode = -1;
                    errText = string.Format("写入时发生错误:{0}", error.Message);
                }
            }
            else
            {
                errCode = -2;
                errText = "没有找到需要写入的 Tag";
            }
        }
Ejemplo n.º 7
0
        public void Write(string m1, string m2)                                            // 写函数
        {
            _itmHandleClient = 1234;
            _kepItem         = _kepItems.AddItem(m1, _itmHandleClient);
            _itmHandleServer = _kepItem.ServerHandle;
            OPCItem bItem = _kepItems.GetOPCItem(_itmHandleServer);

            int[] temp          = new int[] { 0, bItem.ServerHandle };
            Array serverHandles = temp;

            object[] valueTemp = new object[] { "", m2 };
            Array    values    = valueTemp;
            Array    errors;
            int      cancelId;

            _kepGroup.AsyncWrite(1, ref serverHandles, ref values, out errors, 2009, out cancelId);
            GC.Collect();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 右键写入value值。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 写ItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OPCItem bItem = KepItems.GetOPCItem(itmHandlerServer);

            int[] temp = new int[2] {
                0, bItem.ServerHandle
            };
            Array serverHandles = (Array)temp;

            object[] valueTemp = new object[2] {
                "", toolStripTextBox1.Text
            };
            Array values = (Array)valueTemp;
            Array Errors;
            int   CancelID;

            KepGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2017, out CancelID);
            GC.Collect();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Array AsyncValue_Wt;
            Array SerHandles;

            object[] tmpWtData   = new object[3];          //写入的数据必须是object型的,否则会报错
            int[]    tmpSerHdles = new int[3];
            //将输入数据赋给数组,然后再转成Array型送给AsyncValue_Wt
            tmpWtData[1]  = (object)"192.168.70.251";
            tmpWtData[2]  = (object)1;
            AsyncValue_Wt = (Array)tmpWtData;
            //将输入数据送给的Item对应服务器句柄赋给数组,然后再转成Array型送给SerHandles
            tmpSerHdles[1] = Convert.ToInt32(lserverhandles.GetValue(1));
            tmpSerHdles[2] = Convert.ToInt32(lserverhandles.GetValue(2));
            SerHandles     = (Array)tmpSerHdles;
            group.AsyncWrite(2, ref SerHandles, ref AsyncValue_Wt, out lErrors_Wt, lTransID_Wt, out lCancelID_Wt);
            //item = group.OPCItems.AddItem("汽车机械采样机.#1采样机._System._Simulated", 3);
            //item.Write((object)true);
        }
Ejemplo n.º 10
0
        public void SetTagValue(Tag bi)
        {
            int ServerHandle = bi.ExtraAs <DaExtra>().ItmHandleServer;

            //S7:[S7 connection_1]DB53,CHAR30
            int[] temp = new int[2] {
                0, ServerHandle
            };
            Array serverHandles = (Array)temp;

            object[] valueTemp = new object[2] {
                "", bi.Value
            };
            Array values = (Array)valueTemp;
            Array Errors;
            int   cancelID;

            OPCGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
            //KepItem.Write(txtWriteTagValue.Text);//这句也可以写入,但并不触发写入事件
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 写入
        /// </summary>
        public void WriteItem(string itemname, string value)
        {
            OPCItem bItem = PxItems.GetOPCItem(OPCItemDic[itemname].ServerHandle);

            int[] temp = new int[2] {
                0, bItem.ServerHandle
            };
            Array serverHandles = (Array)temp;

            object[] valueTemp = new object[2] {
                "", value
            };
            Array values = (Array)valueTemp;
            Array Errors;
            int   cancelID;

            PxGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
            //KepItem.Write(txtWriteTagValue.Text);//这句也可以写入,但并不触发写入事件
            GC.Collect();
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="tagName"></param>
        /// <param name="_value"></param>
        public static void WriteValue(string tagName, object _value)
        {
            GetTagValue(tagName);
            OPCItem bItem = KepItems.GetOPCItem(itmHandleServer);

            int[] temp = new int[2] {
                0, bItem.ServerHandle
            };
            Array serverHandles = (Array)temp;

            object[] valueTemp = new object[2] {
                "", _value
            };
            Array values = (Array)valueTemp;
            Array Errors;
            int   cancelID;

            KepGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
            //KepItem.Write(txtWriteTagValue.Text);//这句也可以写入,但并不触发写入事件
            GC.Collect();
        }
Ejemplo n.º 13
0
 //向指定项写入内容
 public void WriteValue(string tagName, object value, string key)
 {
     lock ("write")
     {
         try
         {
             OPCItem  bItem         = _kepItems.GetOPCItem(_opcItemDic[tagName]);
             int[]    temp          = { 0, bItem.ServerHandle };
             Array    serverHandles = temp;
             object[] valueTemp     = { "", value };
             Array    values        = valueTemp;
             KepGroup.AsyncWrite(1, ref serverHandles, ref values, out Array errors, 2009, out int cancelId);
             GC.Collect();
         }
         catch (Exception ex)
         {
             LogHelper.WriteLog(typeof(OpcClient), ex.StackTrace + ex.Message);
         }
         //Thread.Sleep(100);//暂停100毫秒
     }
 }
Ejemplo n.º 14
0
        public string AsyncWriteTagValue(string tag, string writeStr)
        {
            try
            {
                Array Errors;
                if (itmHandleClientWriteData != 0)
                {
                    OPCItem bItem = KepItemsWrite.GetOPCItem(itmHandleServerWriteData);
                    //注:OPC中以1为数组的基数
                    int[] temp = new int[2] {
                        0, bItem.ServerHandle
                    };
                    Array serverHandle = (Array)temp;
                    //移除上一次选择的项
                    KepItemsWrite.Remove(KepItemsWrite.Count, ref serverHandle, out Errors);
                }
                itmHandleClientWriteData = Array.IndexOf(tagList, tag) + 1;
                KepItemWrite             = KepItemsWrite.AddItem(tag, itmHandleClientWriteData);
                itmHandleServerWriteData = KepItemWrite.ServerHandle;

                OPCItem bItem_ = KepItemsWrite.GetOPCItem(itmHandleServerWriteData);
                int[]   temp_  = new int[2] {
                    0, bItem_.ServerHandle
                };
                Array    serverHandles = (Array)temp_;
                object[] valueTemp     = new object[2] {
                    "", writeStr
                };
                Array values = (Array)valueTemp;
                int   cancelID;
                KepGroupWriteData.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
                GC.Collect();
                return("OK");
            }
            catch (Exception err)
            {
                return(err.Message);
            }
        }
Ejemplo n.º 15
0
        private void btnWrite_Click(object sender, EventArgs e)
        {
            //获取之前定位的标签
            OPCItem bItem = myItems.GetOPCItem(itmHandleServer);

            //根据用户界面输入生成数据对象
            int[] temp = new int[2] {
                0, bItem.ServerHandle
            };
            Array serverHandles = (Array)temp;

            object[] valueTemp = new object[2] {
                "", txtMyValue.Text
            };
            Array values = (Array)valueTemp;
            Array Errors;
            int   cancelID;

            //异步写入到OPC服务器
            myGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
            //回收资源
            GC.Collect();
        }
Ejemplo n.º 16
0
 private void btnWrite_Click(object sender, EventArgs e)
 {
     if (listboxAlias.SelectedIndex < 0)
     {
         MessageBox.Show("未选中变量!", "提示信息");
     }
     else
     {
         OPCItem bItem = KepItems.GetOPCItem(itmHandleServer);
         int[]   temp  = new int[2] {
             0, bItem.ServerHandle
         };
         Array    serverHandles = (Array)temp;
         object[] valueTemp     = new object[2] {
             "", txtWriteTagValue.Text
         };
         Array values = (Array)valueTemp;
         Array Errors;
         int   cancelID;
         KepGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
         GC.Collect();
     }
 }
Ejemplo n.º 17
0
 /// <summary>
 /// 写入
 /// </summary>
 public void WriteTagValue(string tagValue)
 {
     try
     {
         OPCItem bItem = KepItems.GetOPCItem(itmHandleServer);
         int[]   temp  = new int[2] {
             0, bItem.ServerHandle
         };
         Array    serverHandles = (Array)temp;
         object[] valueTemp     = new object[2] {
             "", tagValue
         };
         Array values = (Array)valueTemp;
         Array Errors;
         int   cancelID;
         KepGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
         //KepItem.Write(txtWriteTagValue.Text);//这句也可以写入,但并不触发写入事件
         GC.Collect();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Ejemplo n.º 18
0
        private void time2_Elapsed(object s, ElapsedEventArgs e, double a1, double a2, double a3, double a4, double a5, double a6, double a7)
        {
            KepGroup = KepGroups.Add("Group0");

            /*KepGroup2 = KepGroups.Add("Group2");
            *  KepGroup3 = KepGroups.Add("Group3");*/
            KepGroup.UpdateRate = 250;

            /* KepGroup2.UpdateRate = 250;
             * KepGroup3.UpdateRate = 250;*/
            KepGroup.IsActive = true;

            /* KepGroup2.IsActive = true;
             * KepGroup3.IsActive = true;*/
            KepGroup.IsSubscribed = true;

            /* KepGroup2.IsSubscribed = true;
             * KepGroup3.IsSubscribed = true;*/
            //当KepGroup中数据发生改变的触发事件
            KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
            // KepGroup2.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange2);
            //  KepGroup3.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange3);

            KepItems = KepGroup.OPCItems;
            //KepItems2 = KepGroup2.OPCItems;
            //KepItems3 = KepGroup3.OPCItems;

            /*int[] temp = new int[3];
             * temp[0] = 0;
             * KepItems.AddItem("123456:OPCAE", 1);
             * KepItems.AddItem("123456:lishile", 2);*/
            //OPCItem bItem = KepItems.Item(2);

            /* MyItem = new OPCItem[27];
             * MyItem[0] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 1);
             * MyItem[1] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.r_id", 2);
             * MyItem[2] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.r_id_back", 3);
             * MyItem[3] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.r_typeid", 4);
             * MyItem[4] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 5);
             * MyItem[5] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 6);
             * MyItem[6] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.r_hang", 7);
             * MyItem[7] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.r_lie", 8);
             * MyItem[8] = KepItems.AddItem("!UI4,PCR_LM1,Plc.PVL,Application.USERVARGLOBAL.r_high", 9);
             * MyItem[9] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 10);
             * MyItem[10] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_id", 11);
             * MyItem[11] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_id_back", 12);
             * MyItem[12] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_typeid", 13);
             * MyItem[13] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 14);
             * MyItem[14] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 15);
             * MyItem[15] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_hang", 16);
             * MyItem[16] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_lie", 17);
             * MyItem[17] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_high", 18);
             * MyItem[18] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 19);
             * MyItem[19] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_id", 20);
             * MyItem[20] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_id_back", 21);
             * MyItem[21] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_typeid", 22);
             * MyItem[22] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 23);
             * MyItem[23] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 24);
             * MyItem[24] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_hang", 25);
             * MyItem[25] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_lie", 26);
             * MyItem[26] = KepItems.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_high", 27);
             *
             * /*double[] items1 = new double[28];
             * items1[0] = 0;
             *
             * for (int i = 1; i < items1.Length; i++)
             * {
             *   items1[i] = MyItem[i - 1].Value;
             *   if(items1[i] == null)
             *   {
             *
             *   }
             * }
             * Array serverHandles = (Array)items1;
             * Array Errors;
             * int cancelID;
             * KepGroup.AsyncRead(2, ref serverHandles, out Errors, 1, out cancelID);*/



            /* if (s6 == 1)
             * {*/
            MyItem    = new OPCItem[9];
            MyItem[0] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 1);
            MyItem[1] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_id", 2);
            MyItem[2] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_id_back", 3);
            MyItem[3] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_typeid", 4);
            MyItem[4] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 5);
            MyItem[5] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 6);
            MyItem[6] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_hang", 7);
            MyItem[7] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_lie", 8);
            MyItem[8] = KepItems.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_high", 9);
            double[] items1 = new double[10];
            items1[0] = 0;
            for (int i = 1; i < items1.Length; i++)
            {
                items1[i] = MyItem[i - 1].Value;
            }

            Array serverHandles = (Array)items1;
            Array Errors;
            int   cancelID;
            int   JXSStatus = Int32.Parse(items1[1].ToString());   //机械手状态

            //KepGroup.AsyncRead(9, ref serverHandles, out Errors, 1, out cancelID);


            //龙门未动作时
            if (JXSStatus == 1)
            {
                int[]    tmp           = new int[] { 0, MyItem[1].ServerHandle, MyItem[3].ServerHandle, MyItem[4].ServerHandle, MyItem[5].ServerHandle, MyItem[6].ServerHandle, MyItem[7].ServerHandle };
                Array    ServerHandles = (Array)tmp;
                object[] valuetemp     = new object[7] {
                    "", /*s5, Bead, hang, lie, height*/ a1, a2, a3, a4, a5, a6
                };
                Array values = (Array)valuetemp;
                KepGroup.AsyncWrite(6, ref serverHandles, ref values, out Errors, 1, out cancelID);
                Thread.Sleep(500);
            }
            if (JXSStatus == 3)
            {
                int idback = int.Parse(items1[2].ToString());
                if (idback != 0)
                {
                    Trace.WriteLine("轮胎放置完成!");
                    MyItem[2].Write(0);    //置零
                    time2.Stop();
                }
            }
            /* }*/

            /*if (s6 == 2)
             * {
             *  MyItem2 = new OPCItem[9];
             *  MyItem2[0] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 1);
             *  MyItem2[1] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_id", 2);
             *  MyItem2[2] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_id_back", 3);
             *  MyItem2[3] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_typeid", 4);
             *  MyItem2[4] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 5);
             *  MyItem2[5] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 6);
             *  MyItem2[6] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_hang", 7);
             *  MyItem2[7] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_lie", 8);
             *  MyItem2[8] = KepItems2.AddItem("!UI4,PCR_LM2,Plc.PVL,Application.USERVARGLOBAL.r_high", 9);
             *  double[] items2 = new double[10];
             *  items2[0] = 0;
             *  for (int i = 1; i < items2.Length; i++)
             *      items2[i] = MyItem2[i - 1].Value;
             *
             *  Array serverHandles = (Array)items2;
             *  Array Errors;
             *  int cancelID;
             *  int JXSStatus = Int32.Parse(items2[1].ToString()); //机械手状态
             *  //KepGroup.AsyncRead(9, ref serverHandles, out Errors, 1, out cancelID);
             *
             *
             *  //龙门未动作时
             *  if (JXSStatus == 1)
             *  {
             *      int[] tmp = new int[] { 0, MyItem2[1].ServerHandle, MyItem2[4].ServerHandle, MyItem2[6].ServerHandle, MyItem2[7].ServerHandle, MyItem2[8].ServerHandle };
             *      Array ServerHandles = (Array)tmp;
             *      object[] valuetemp = new object[6] { "", s5, Bead, hang, lie, height };
             *      Array values = (Array)valuetemp;
             *      KepGroup.AsyncWrite(5, ref serverHandles, ref values, out Errors, 1, out cancelID);
             *      Thread.Sleep(500);
             *  }
             *  if (JXSStatus == 3)
             *  {
             *      int idback = int.Parse(items2[2].ToString());
             *      if (idback != 0)
             *      {
             *          Trace.WriteLine("轮胎放置完成!");
             *          MyItem2[2].Write(0);//置零
             *          time2.Stop();
             *      }
             *  }
             * }
             * if (s6 == 3)
             * {
             *  MyItem3 = new OPCItem[9];
             *  MyItem3[0] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.JXSStatus", 1);
             *  MyItem3[1] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_id", 2);
             *  MyItem3[2] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_id_back", 3);
             *  MyItem3[3] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_typeid", 4);
             *  MyItem3[4] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_cpzik", 5);
             *  MyItem3[5] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_cphigh", 6);
             *  MyItem3[6] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_hang", 7);
             *  MyItem3[7] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_lie", 8);
             *  MyItem3[8] = KepItems3.AddItem("!UI4,PCR_LM3,Plc.PVL,Application.USERVARGLOBAL.r_high", 9);
             *  double[] items3 = new double[10];
             *  items3[0] = 0;
             *  for (int i = 1; i < items3.Length; i++)
             *      items3[i] = MyItem3[i - 1].Value;
             *
             *  Array serverHandles = (Array)items3;
             *  Array Errors;
             *  int cancelID;
             *  int JXSStatus = Int32.Parse(items3[1].ToString()); //机械手状态
             *  //KepGroup.AsyncRead(9, ref serverHandles, out Errors, 1, out cancelID);
             *
             *
             *  //龙门未动作时
             *  if (JXSStatus == 1)
             *  {
             *      int[] tmp = new int[] { 0, MyItem3[1].ServerHandle, MyItem3[4].ServerHandle, MyItem3[6].ServerHandle, MyItem3[7].ServerHandle, MyItem3[8].ServerHandle };
             *      Array ServerHandles = (Array)tmp;
             *      object[] valuetemp = new object[6] { "", s5, Bead, hang, lie, height };
             *      Array values = (Array)valuetemp;
             *      KepGroup.AsyncWrite(5, ref serverHandles, ref values, out Errors, 1, out cancelID);
             *      Thread.Sleep(500);
             *  }
             *  if (JXSStatus == 3)
             *  {
             *      int idback = int.Parse(items3[2].ToString());
             *      if (idback != 0)
             *      {
             *          Trace.WriteLine("轮胎放置完成!");
             *          MyItem3[2].Write(0);//置零
             *          time2.Stop();
             *      }
             *  }
             * }*/
        }
        //测试用工作方法
        public void work()
        {
            //初始化item数组
            MyItem  = new OPCItem[4];
            MyItem2 = new OPCItem[4];

            GetLocalServer();
            //ConnectRemoteServer("TX1", "KEPware.KEPServerEx.V4");//用计算机名的局域网
            //ConnectRemoteServer("192.168.1.35", "KEPware.KEPServerEx.V4");//用IP的局域网
            if (ConnectRemoteServer("", "KEPware.KEPServerEx.V4"))//本机
            {
                if (CreateGroup())
                {
                    Thread.Sleep(500);              //暂停线程以让DataChange反映,否则下面的同步读可能读不到
                    //以下同步写
                    MyItem[3].Write("I love you!"); //同步写
                    MyItem[2].Write(true);          //同步写
                    MyItem[1].Write(-100);          //同步写
                    MyItem[0].Write(120);           //同步写

                    //以下同步读
                    object ItemValues;  object Qualities; object TimeStamps;          //同步读的临时变量:值、质量、时间戳
                    MyItem[0].Read(1, out ItemValues, out Qualities, out TimeStamps); //同步读,第一个参数只能为1或2
                    int q0 = Convert.ToInt32(ItemValues);                             //转换后获取item值
                    MyItem[1].Read(1, out ItemValues, out Qualities, out TimeStamps); //同步读,第一个参数只能为1或2
                    int q1 = Convert.ToInt32(ItemValues);                             //转换后获取item值
                    MyItem[2].Read(1, out ItemValues, out Qualities, out TimeStamps); //同步读,第一个参数只能为1或2
                    bool q2 = Convert.ToBoolean(ItemValues);                          //转换后获取item值
                    MyItem[3].Read(1, out ItemValues, out Qualities, out TimeStamps); //同步读,第一个参数只能为1或2
                    string q3 = Convert.ToString(ItemValues);                         //转换后获取item值,为防止读到的值为空,不用ItemValues.ToString()

                    Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                    Console.WriteLine("0-{0},1-{1},2-{2},3-{3}", q0, q1, q2, q3);

                    //以下为异步写
                    //异步写时,Array数组从下标1开始而非0!
                    int[]    temp          = new int[] { 0, MyItem[0].ServerHandle, MyItem[1].ServerHandle, MyItem[2].ServerHandle, MyItem[3].ServerHandle };
                    Array    serverHandles = (Array)temp;
                    object[] valueTemp     = new object[5] {
                        "", 255, 520, true, "Love"
                    };
                    Array values = (Array)valueTemp;
                    Array Errors;
                    int   cancelID;
                    MyGroup.AsyncWrite(4, ref serverHandles, ref values, out Errors, 1, out cancelID);//第一参数为item数量
                    //由于MyGroup2没有订阅,所以以下这句运行时将会出错!
                    //MyGroup2.AsyncWrite(4, ref serverHandles, ref values, out Errors, 1, out cancelID);

                    //以下异步读
                    MyGroup.AsyncRead(4, ref serverHandles, out Errors, 1, out cancelID);//第一参数为item数量


                    /*MyItem[0] = MyItems.AddItem("BPJ.Db1.dbb96", 0);//byte
                    *  MyItem[1] = MyItems.AddItem("BPJ.Db1.dbw10", 1);//short
                    *  MyItem[2] = MyItems.AddItem("BPJ.Db16.dbx0", 2);//bool
                    *  MyItem[3] = MyItems.AddItem("BPJ.Db11.S0", 3);//string*/



                    Console.WriteLine("************************************** hit <return> to Disconnect...");
                    Console.ReadLine();
                    //释放所有组资源
                    MyServer.OPCGroups.RemoveAll();
                    //断开服务器
                    MyServer.Disconnect();
                }
            }


            //END
            Console.WriteLine("************************************** hit <return> to close...");
            Console.ReadLine();
        }
Ejemplo n.º 20
0
        private void KepGroup_DataChange2(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            listBox1.Items.Add("动均3出库");
            for (int i = 1; i <= NumItems; i++)
            {
                listBox1.Items.Add(ItemValues.GetValue(i));//取到改变的值
            }
            if (MyItem2[9].Value == 2)
            {
                return;
            }
            DataSet myds11 = new DataSet();
            string  dstr11 = "select * from dongjunqiankuwei3 where status = '满' order by id asc;";

            myds11 = sql.GetDataSet(dstr11, "dongjunqiankuwei333");
            if (myds11.Tables[0].Rows.Count == 0)
            {
                return;
            }
            MyItem[3].Write(0);
            int ischuk_lk = MyItem2[9].Value;

            Thread.Sleep(500);
            int JXSStatus = int.Parse(MyItem2[10].Value.ToString());

            if (JXSStatus == 3)
            {
                MyItem2[2].Write(0);
                CD = false;
            }
            if (ischuk_lk == 1 && JXSStatus == 1 && !CD && MyItem[2].Value == 0)
            {
                DataSet myds3 = new DataSet();
                string  dstr3 = "select * from dongjunqiankuwei3 where status = '满' order by id asc;";
                myds3 = sql.GetDataSet(dstr3, "chuku");
                if (myds3.Tables[0].Rows.Count == 0)
                {
                    return;
                }
                int id    = int.Parse(myds3.Tables[0].Rows[0]["id"].ToString());
                int cpmun = int.Parse(myds3.Tables[0].Rows[0]["number"].ToString());
                c_hang = id / 100;
                c_lie  = id % 100;
                //c_lie1 = int.Parse(myds3.Tables[0].Rows[0]["lie"].ToString());
                //c_hang1 = int.Parse(myds3.Tables[0].Rows[0]["hang"].ToString());
                //ordernum = ordernum + 1;
                // c_lie = c_lie + 1;
                int[] temp11 = new int[9] {
                    0, MyItem2[0].ServerHandle, MyItem2[1].ServerHandle,
                    MyItem2[3].ServerHandle, MyItem2[4].ServerHandle,
                    MyItem2[5].ServerHandle, MyItem2[6].ServerHandle,
                    MyItem2[7].ServerHandle, MyItem2[8].ServerHandle
                };
                Array    serverHandles = temp11;
                object[] valueTemp     = new object[9] {
                    "", 200, id, zikou, c_hang, c_lie, 200, cpmun, 2
                };
                Array  values = (Array)valueTemp;
                Array  Errors;
                Object cancel;
                // Object Qualities;
                int cancelID;
                KepGroup2.AsyncWrite(8, ref serverHandles, ref values, out Errors, 1, out cancelID);//第一参数为item数量
                string dstr4 = "update dongjunqiankuwei3 set status = '空', number = 0 where id = " + id + ";";
                sql.ExecuteSqlCommand(dstr4);

                /*if(ordernum==4)
                 * {
                 *  ordernum = 0;
                 *  c_lie = 2;
                 *
                 * }*/
                CD = true;
            }
        }
Ejemplo n.º 21
0
        //当数据改变时触发的事件
        public void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            if (MyItem[9].Value == false)
            {
                return;
            }
            listBox1.Items.Add("动均3入库");
            for (int i = 1; i <= NumItems; i++)
            {
                listBox1.Items.Add(ItemValues.GetValue(i));//取到改变的值
            }
            DataSet myds4 = new DataSet();
            string  dstr4 = "select * from dongjunqiankuwei3 where status = '满' order by id asc;";

            myds4 = sql.GetDataSet(dstr4, "dongjunqiankuwei3");
            if (myds4.Tables[0].Rows.Count == 0)
            {
                notyre1 = true;
            }
            else
            {
                notyre1 = false;
            }
            if (MyItem2[9].Value == 1 && !notyre1)
            {
                return;
            }
            //Thread.Sleep(1000);
            MyItem2[2].Write(0);
            if (MyItem[0].Value == 3)
            {
                MyItem[3].Write(0);
                Thread.Sleep(1000);
                tyreflag = true;
            }
            if (/*aaa1 > fullnumber &&*/ aaa % fullnumber == 1 && !cd)
            {
                //DataSet myds1 = new DataSet();
                DataSet Myds = new DataSet();
                string  b1   = "select * from dongjunqiankuwei3 where status = '空' order by id asc;";
                Myds = sql.GetDataSet(b1, "dongjunqiankuwei33");
                if (Myds.Tables[0].Rows.Count == 0)
                {
                    return;
                }
                id          = int.Parse(Myds.Tables[0].Rows[0]["id"].ToString());
                hang        = id / 100;
                lie1        = id % 100;
                high        = higher1;
                tyrenumber1 = 1;
                string b2 = "update dongjunqiankuwei3 set status = '有'where id = " + id + " ;";
                sql.ExecuteSqlCommand(b2);


                cd = true;

                /* if (lie11 == 8)
                 * {
                 *   aaa1 = 1;
                 *   lie11 = 4;
                 *   hang1 = hang1 + 1;
                 *   if (hang1 == 6)
                 *   {
                 *       hang1 = 4;
                 *   }
                 *   cd1 = false;
                 *   return;
                 * }*/
            }

            if (MyItem[9].Value == true && MyItem[0].Value == 1 && MyItem2[1].Value == 0 && tyreflag)
            {
                MyItem[3].Write(0);
                Thread.Sleep(500);


                if (aaa % 4 == 1)
                {
                    high = higher1;
                }
                else if (aaa % 4 == 2)
                {
                    high = higher2;
                }
                else if (aaa % 4 == 3)
                {
                    high = higher3;
                }
                else if (aaa % 4 == 0)
                {
                    high = higher4;
                }
                int[] temp = new int[8] {
                    0, MyItem[1].ServerHandle, MyItem[2].ServerHandle, MyItem[4].ServerHandle,
                    MyItem[5].ServerHandle, MyItem[6].ServerHandle,
                    MyItem[7].ServerHandle, MyItem[8].ServerHandle
                };
                Array    serverHandles = temp;
                object[] valueTemp     = new object[8] {
                    "", 200, aaa, zikou, hang, lie1, high, 1
                };
                Array  values = (Array)valueTemp;
                Array  Errors;
                Object cancel;
                // Object Qualities;
                int cancelID;
                KepGroup.AsyncWrite(7, ref serverHandles, ref values, out Errors, 1, out cancelID);//第一参数为item数量
                DataSet myds = new DataSet();
                string  dstr = "update ruku3 set lie=" + lie1 + ",hang = " + hang + " where id = 1;";
                sql.ExecuteSqlCommand(dstr);

                if (tyrenumber1 >= fullnumber)
                {
                    string b3 = "update dongjunqiankuwei3 set number = " + fullnumber + ",status = '好' where id =" + id + " ;";
                    sql.ExecuteSqlCommand(b3);
                }
                else
                {
                    string b4 = "update dongjunqiankuwei3 set number = " + tyrenumber1 + "  where id = " + id + " ;";
                    sql.ExecuteSqlCommand(b4);
                }
                aaa = aaa + 1;
                string DStr = "update ruku3 set aaa = " + aaa + ";";
                sql.ExecuteSqlCommand(DStr);
                tyrenumber1 = aaa % fullnumber;
                if (tyrenumber1 == 0)
                {
                    tyrenumber1 = fullnumber;
                }

                // high = high + 200;
                tyreflag = false;
                cd       = false;
                //bbb = aaa;
            }
        }