Beispiel #1
0
        private void button3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Stopwatch sp = new System.Diagnostics.Stopwatch();
            sp.Start();
            string bitAdd = txtBitAdd.Text.Trim();
            short  bs;
            short  re = ENT.GetBitState(PlcMemory.DM, bitAdd, out bs);

            sp.Stop();
            txtUseTimeB.Text = sp.Elapsed.TotalMilliseconds.ToString() + "ms";
            if (re != 0)
            {
                //写入数据出错
                MessageBox.Show("获取指定位状态失败!");
            }
            else
            {
                if (bs == 1)
                {
                    MessageBox.Show("获取指定位状态成功:当前开打(1)!");
                }
                else
                {
                    MessageBox.Show("获取指定位状态成功:当前关闭(0)!");
                }
            }
        }
        public bool ReadSingleElement(PlcScanItems item, ref object value)
        {
            PlcMemory memorytype      = item.AddressType;
            string    bitstartaddress = string.Empty;
            short     startaddress    = 0;

            if (item.DataType == DataType.BIT)
            {
                bitstartaddress = item.Address;
            }
            else
            {
                startaddress = short.Parse(item.Address);
            }
            lock (readLock)
            {
                switch (item.DataType)
                {
                case DataType.BIT:
                    short shortvalue = 0;
                    if (0 != omronethernetplc.GetBitState(memorytype, bitstartaddress, out shortvalue))
                    {
                        bConnectOmronPLC = false;
                        return(false);
                    }
                    value = shortvalue == 1 ? true : false;
                    break;

                case DataType.INT16:
                    Int16 int16value = 0;
                    if (0 != omronethernetplc.ReadInt16(memorytype, startaddress, out int16value))
                    {
                        bConnectOmronPLC = false;
                        return(false);
                    }
                    value = int16value;
                    break;

                case DataType.INT32:
                    Int32 int32value = 0;
                    if (0 != omronethernetplc.ReadInt32(memorytype, startaddress, out int32value))
                    {
                        bConnectOmronPLC = false;
                        return(false);
                    }
                    value = int32value;
                    break;

                case DataType.REAL:
                    float floatvalue = 0.0f;
                    if (0 != omronethernetplc.ReadReal(memorytype, startaddress, out floatvalue))
                    {
                        bConnectOmronPLC = false;
                        return(false);
                    }
                    value = floatvalue;
                    break;

                case DataType.UINT16:
                    UInt16 uint16value = 0;
                    if (0 != omronethernetplc.ReadUInt16(memorytype, startaddress, out uint16value))
                    {
                        bConnectOmronPLC = false;
                        return(false);
                    }
                    value = uint16value;
                    break;

                case DataType.UINT32:
                    UInt32 uint32value = 0;
                    if (0 != omronethernetplc.ReadUint32(memorytype, startaddress, out uint32value))
                    {
                        bConnectOmronPLC = false;
                        return(false);
                    }
                    value = uint32value;
                    break;
                }
                return(true);
            }
        }