Esempio n. 1
0
        /// <summary>
        /// 将给定节点的“输出值”写入外部设备或PLC
        /// </summary>
        /// <param name="sourceList">节点列表</param>
        /// <param name="timeout">最大超时值</param>
        /// <returns>返回成功写的点数</returns>
        public override int WritePoints(List <AcquirePoint> outputList, TimeSpan timeout)
        {
            int               ct = 0;
            string            cmd;
            FxCommandResponse res;

            foreach (AcquirePoint ap in outputList)
            {
                if (ap.ControllerObject.ControllerId != base.ControllerId)
                {
                    continue;
                }
                if (ap.AV.Output.IsNull)
                {
                    continue;
                }

                if (ap.AV.Output.B)
                {
                    cmd = FxCommandHelper.Make(FxCommandConst.FxCmdForceOn, new FxAddress(ap.ChannelNoAlias, FxAddressLayoutType.AddressLayoutByte));
                }
                else
                {
                    cmd = FxCommandHelper.Make(FxCommandConst.FxCmdForceOff, new FxAddress(ap.ChannelNoAlias, FxAddressLayoutType.AddressLayoutByte));
                }

                res = Send(0, cmd);

#if DEBUG
                //Logger.Instance.WriteLine("FxPLC输出: {0} ", res.ToString());
#endif

                ct++;
            }

            return(ct);
        }
Esempio n. 2
0
        /// <summary>
        /// 读取设备所有IO点数据,并返回这些点的数据值
        /// </summary>
        public override List <AcquireRawValue> ReadAllPoints(List <AcquirePoint> sourceAPList, TimeSpan timeout)
        {
            if (_SerialPort == null || !_SerialPort.IsOpen)
            {
                return(null);
            }

            List <AcquireRawValue> vlist = new List <AcquireRawValue>();

            if (sourceAPList != null)
            {
                sourceAPList.ForEach(o => {
                    if (o.ControllerObject.ControllerId == this.ControllerId)
                    {
                        vlist.Add(new AcquireRawValue(o.Id, o.ControllerObject, o.ChannelNoAlias));
                    }
                });
            }
            else
            {
                //for(int result = 0; result < 127; result++) {
                //    vlist.Add(new AcquireRawValue(0, 0, string.Format("X{0}", result)));
                //    vlist.Add(new AcquireRawValue(0, 0, string.Format("Y{0}", result)));
                //}
            }

            string            cmd;
            FxCommandResponse res;

            // 读取 X0..X177,一次性读取所有X
            cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("X0", ControllerTypeConst.ctPLC_Fx), 16);
            res = Send(0, cmd);
            //Debug.Print("X0--X177 = {0}", res.ToString());

            // 根据通道号别名,例如"X001","Y177","Mxxx"等内容更新
            if (res.ResultCode == ResultCodeConst.rcSuccess)
            {
                foreach (AcquireRawValue o in vlist)
                {
                    if (o.PLCAddr != null && o.PLCAddr.AddressType == FxAddressType.X)
                    {
                        int byteIndex = (int)(o.PLCAddr.TagOffset / 8);
                        int byteOff   = (int)(o.PLCAddr.TagOffset % 8);

                        if (byteIndex >= 0 && byteIndex < res.ResponseValue.Count)
                        {
                            o.Value = new ValueStruct(((res.ResponseValue[byteIndex] >> byteOff) & 0x01) == 0x01);

                            //Debug.Print("X{0} <== {1}", Convert.ToString(o.PLCAddr.TagOffset, 8), o.Value);
                        }
                        else
                        {
                            //Debug.Print("X{0} <== {1}", Convert.ToString(o.PLCAddr.TagOffset, 8), o.Value);
                        }
                    }
                }
            }
            else
            {
                Debug.Print("{0}\t{1} 读取失败(X001-X177)", DateTime.Now, this.ToString());
            }

            // 读取 Y0..Y177,一次性读取所有Y
            cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("Y0", ControllerTypeConst.ctPLC_Fx), 16);
            res = Send(0, cmd);
            //Debug.Print("Y0--Y177 = {0}", res.ToString());
            // 根据通道号别名,例如"Y177"等内容更新
            if (res.ResultCode == ResultCodeConst.rcSuccess)
            {
                foreach (AcquireRawValue o in vlist)
                {
                    if (o.PLCAddr != null && o.PLCAddr.AddressType == FxAddressType.Y)
                    {
                        int byteIndex = (int)(o.PLCAddr.TagOffset / 8);
                        int byteOff   = (int)(o.PLCAddr.TagOffset % 8);

                        if (byteIndex >= 0 && byteIndex < res.ResponseValue.Count)
                        {
                            o.Value = new ValueStruct(((res.ResponseValue[byteIndex] >> byteOff) & 0x01) == 0x01);
                        }
                        else
                        {
                            //Debug.Print("Y{0} <== {1}", Convert.ToString(o.PLCAddr.TagOffset, 8), o.Value);
                        }
                    }
                }
            }
            else
            {
                Debug.Print("{0}\t{1} 读取失败(Y001-Y177)", DateTime.Now, this.ToString());
            }

            // 一次性读取 M0..M499
            cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("M0", ControllerTypeConst.ctPLC_Fx), 64);
            res = Send(0, cmd);

            //Debug.Print("M0--M499 = {0}", res.ToString());
            // 根据通道号别名,例如"M19"等内容更新
            if (res.ResultCode == ResultCodeConst.rcSuccess)
            {
                foreach (AcquireRawValue o in vlist)
                {
                    if (o.PLCAddr != null && o.PLCAddr.AddressType == FxAddressType.M && o.PLCAddr.TagOffset < 1000)
                    {
                        int byteIndex = (int)(o.PLCAddr.TagOffset / 8);
                        int byteOff   = (int)(o.PLCAddr.TagOffset % 8);

                        if (byteIndex >= 0 && byteIndex < res.ResponseValue.Count)
                        {
                            o.Value = new ValueStruct(((res.ResponseValue[byteIndex] >> byteOff) & 0x01) == 0x01);
                        }
                        else
                        {
                            //Debug.Print("M{0} <== {1}", Convert.ToString(o.PLCAddr.TagOffset, 8), o.Value);
                        }
                    }
                }
            }
            else
            {
                Debug.Print("{0}\t{1} 读取失败(M001-M499)", DateTime.Now, this.ToString());
            }

            #region 一次性读取 M1000-1999 ---- 目前不读取这个范围的 M 点
            if (false)
            {
                cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("M1000", ControllerTypeConst.ctPLC_Fx), 32);
                res = Send(0, cmd);
                //Debug.Print("M1000--M1127 = {0}", res.ToString());
                // 根据通道号别名,例如"M1099"等内容更新
                if (res.ResultCode == ResultCodeConst.rcSuccess)
                {
                    foreach (AcquireRawValue o in vlist)
                    {
                        if (o.PLCAddr != null && o.PLCAddr.AddressType == FxAddressType.M && o.PLCAddr.TagOffset >= 1000)
                        {
                            int byteIndex = (int)((o.PLCAddr.TagOffset - 1000) / 8);
                            int byteOff   = (int)((o.PLCAddr.TagOffset - 1000) % 8);

                            if (byteIndex >= 0 && byteIndex < res.ResponseValue.Count)
                            {
                                o.Value = new ValueStruct(((res.ResponseValue[byteIndex] >> byteOff) & 0x01) == 0x01);
                            }
                            else
                            {
                                //Debug.Print("M{0} <== {1}", o.PLCAddr.TagOffset, o.Value);
                            }
                        }
                    }
                }
            }
            #endregion

            // 读取其他内容:C/D/T
            foreach (AcquireRawValue o in vlist)
            {
                if (o.PLCAddr != null)
                {
                    switch (o.PLCAddr.AddressType)
                    {
                    case FxAddressType.C:
                        cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, o.PLCAddr, 2);
                        break;

                    case FxAddressType.D:
                        cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, o.PLCAddr, 2);
                        break;

                    case FxAddressType.T:
                        cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, o.PLCAddr, 2);
                        break;

                    case FxAddressType.S:
                        cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, o.PLCAddr, 2);
                        break;

                    default:
                        continue;
                    }

                    res = Send(0, cmd);
                    if (res.ResultCode == ResultCodeConst.rcSuccess)
                    {
                        o.Value = new ValueStruct((int)res.ResponseValue[0]);
                        //Debug.Print("{0} <== {1}", o.PLCAddr.ToString(), o.Value);
                    }
                    else
                    {
                        //Debug.Print("{0} <== {1}. Error", o.PLCAddr.ToString(), o.Value);
                    }
                }
            }

            return(vlist);
        }