예제 #1
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);
            }
        }