示例#1
0
        public bool WriteSingleElement(PlcScanItems item, 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);
            }
            short result = 0;

            //lock (writeLock)
            lock (readLock)
            {
                switch (item.DataType)
                {
                case DataType.BIT:
                    bool boolvalue = (bool)value;
                    if (boolvalue)
                    {
                        result = omronethernetplc.SetBitState(memorytype, bitstartaddress.ToString(), BitState.ON);
                    }
                    else
                    {
                        result = omronethernetplc.SetBitState(memorytype, bitstartaddress.ToString(), BitState.OFF);
                    }
                    break;

                case DataType.INT16:
                    Int16 int16value = Convert.ToInt16(value.ToString());
                    result = omronethernetplc.WriteInt16(memorytype, startaddress, int16value);
                    break;

                case DataType.INT32:
                    Int32 int32value = Convert.ToInt32(value.ToString());
                    result = omronethernetplc.WriteInt32(memorytype, startaddress, int32value);
                    break;

                case DataType.REAL:
                    float floatvalue = Convert.ToSingle(value.ToString());
                    result = omronethernetplc.WriteFloat(memorytype, startaddress, floatvalue);
                    break;

                case DataType.UINT16:
                    UInt16 uint16value = Convert.ToUInt16(value.ToString());
                    result = omronethernetplc.WriteUint16(memorytype, startaddress, uint16value);
                    break;

                case DataType.UINT32:
                    UInt32 uint32value = Convert.ToUInt32(value.ToString());
                    result = omronethernetplc.WriteUint32(memorytype, startaddress, uint32value);
                    break;

                case DataType.STRING:
                    if ((value.ToString()).Length >= 64)
                    {
                        return(false);
                    }
                    return(WriteString(item, 32, value.ToString()));
                }
                if (result == 0)
                {
                    return(true);
                }
                else
                {
                    bConnectOmronPLC = false;
                    return(false);
                }
            }
        }