コード例 #1
0
        private bool                    readBitSegment(byte aSlaveID, ERegisterType aRegisterType, ushort aRegister, ref bool[] aValue)
        {
            int lFrame = (int)mFrame * 16;
            int lDiv   = aValue.Length / lFrame;
            int lStart = 0;

            bool[] lArray;

            for (int i = 0; i < lDiv; i++)
            {
                try
                {
                    if (aRegisterType == ERegisterType.COIL_BIT)
                    {
                        lArray = mMaster.ReadCoils(aSlaveID, (ushort)(aRegister + lStart), (ushort)lFrame);
                    }
                    else
                    {
                        lArray = mMaster.ReadInputs(aSlaveID, (ushort)(aRegister + lStart), (ushort)lFrame);
                    }
                }
                catch (Exception lExc)
                {
                    reportError("Error reading data for one or group of Items: " + lExc.Message);
                    return(false);
                }

                Array.Copy(lArray, 0, aValue, lStart, lFrame);

                lStart = lStart + lFrame;
            }

            int lLastPart = aValue.Length - lStart;

            if (lLastPart > 0)
            {
                try
                {
                    if (aRegisterType == ERegisterType.COIL_BIT)
                    {
                        lArray = mMaster.ReadCoils(aSlaveID, (ushort)(aRegister + lStart), (ushort)lLastPart);
                    }
                    else
                    {
                        lArray = mMaster.ReadInputs(aSlaveID, (ushort)(aRegister + lStart), (ushort)lLastPart);
                    }
                }
                catch (Exception lExc)
                {
                    reportError("Error reading data for one or group of Items: " + lExc.Message);
                    return(false);
                }

                Array.Copy(lArray, 0, aValue, lStart, lLastPart);
            }

            return(true);
        }
コード例 #2
0
        public void                 setRegister(ERegisterType aRegisterType, ushort aRegister, Type aDataType, ushort aLength)
        {
            if (aLength == 0 || (aRegister + aLength) > 65535)
            {
                throw new ArgumentException("Length is wrong. ");
            }

            if (aRegisterType == ERegisterType.INPUT_BIT || aRegisterType == ERegisterType.COIL_BIT)
            {
                if (aDataType != typeof(Boolean))
                {
                    throw new ArgumentException("DataType is wrong. ");
                }
            }
            else
            {
                if (aDataType != typeof(Int16) && aDataType != typeof(UInt16) &&
                    aDataType != typeof(Int32) && aDataType != typeof(UInt32) && aDataType != typeof(Single))
                {
                    throw new ArgumentException("DataType is wrong. ");
                }
            }

            mRegisterType = aRegisterType;
            mRegister     = aRegister;
            mDataType     = aDataType;
            mLength       = aLength;
            mValue        = InitValue;

            if (mRegisterType == ERegisterType.INPUT_REGISTER || mRegisterType == ERegisterType.HOLDING_REGISTER)
            {
                if (mDataType == typeof(Int16) || mDataType == typeof(UInt16))
                {
                    mSegLength = (ushort)mLength;
                }
                else
                {
                    mSegLength = (ushort)(2 * mLength);
                }
            }
            else
            {
                mSegLength = (ushort)mLength;
            }
        }