Esempio n. 1
0
        public static void Set2PLC <T>(PLCParameter <T> pLCParameter, Type targetType, float factor, AbstractComm Comm)
        {
            if (String.IsNullOrEmpty(pLCParameter.Addr))
            {
                return;
            }
            String TypeName = targetType == null ? typeof(T).Name : targetType.Name;

            switch (TypeName)
            {
            case "Boolean":
            {
                Comm.SetCoil((bool)(Object)pLCParameter.SetValue, Comm.ConvertFatekAddrToModbusAddr(pLCParameter.Addr));
                break;
            }

            case "Single":
            {
                Comm.SetRegister(pLCParameter.Addr, (float)(Object)pLCParameter.SetValue * factor);
                break;
            }

            case "Int32":
            {
                Comm.SetRegister(pLCParameter.Addr, Convert.ToInt32(Convert.ToSingle((Object)pLCParameter.SetValue) * factor));
                break;
            }

            case "Int16":
            {
                var val = Convert.ToInt16(Convert.ToSingle((Object)pLCParameter.SetValue) * factor);
                //Console.WriteLine("val=" + val);
                Comm.SetRegister(pLCParameter.Addr, val);
                break;
            }

            default:
            {
                break;
            }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 移到指定位置,移动前关闭反馈完成信号
        /// </summary>
        /// <param name="Motor"></param>
        /// <param name="Comm"></param>
        /// <returns></returns>
        public static bool MoveMotor(Electromotor Motor, AbstractComm Comm, decimal?Distance, bool OnlyStart = false, bool Start = true, decimal gap = 0m)
        {
            Constants.PauseResetEvent.WaitOne();
            if (Constants.UserAbort)
            {
                return(false);
            }
            if (!Distance.HasValue)
            {
                return(false);
            }
            if (Distance.Value < 0)
            {
                Distance = 0;
            }
            var realdis = Motor.GetRealDistance(Distance.Value, gap);

            if (Motor.CurrentDistance == realdis)
            {
                return(true);
            }
            else
            {
                //关闭完成信号
                Comm.SetCoil(false, Motor.DoneCoil.Addr);
            }
            Motor.CurrentDistance = realdis;
            int distance = Convert.ToInt32(realdis * Convert.ToDecimal(Motor.Factor.SetValue));
            var result   = Comm.SetRegister(Motor.Distance.Addr, distance);

            if (Start)
            {
                StartMotor(Motor, Comm);
            }
            if (!OnlyStart)
            {
                result = Comm.Doned(30000, true, Motor.DoneCoil);
            }
            return(result);
        }