示例#1
0
        /// <summary>
        /// 构建PLC FX命令
        /// 支持各种“读数据”或“查询”类命令,默认数据类型UInt16DataType
        /// </summary>
        /// <param name="response">命令字</param>
        /// <param name="addr">起始地址</param>
        public static string Make(FxCommandConst cmd, FxAddress addr)
        {
            int length = 0;

            if (cmd == FxCommandConst.FxCmdForceOff | cmd == FxCommandConst.FxCmdForceOn)
            {
                length = 0;
            }
            else
            {
                switch (addr.AddressSpaceType)
                {
                case FxAddressLayoutType.AddressLayoutBin:
                    length = 0;
                    break;

                case FxAddressLayoutType.AddressLayoutByte:
                    length = 1;
                    break;

                case FxAddressLayoutType.AddressLayoutInt16:
                    length = 2;
                    break;

                case FxAddressLayoutType.AddressLayoutInt32:
                    length = 4;
                    break;
                }
            }
            return(Make(cmd, addr, length));
        }
示例#2
0
        public static string Make(FxCommandConst cmd, FxAddress addr, int length, string data)
        {
            Debug.Assert(length == data.Length, "length(长度)必须与给定的数据实际一致");

            StringBuilder sb = new StringBuilder(64 + data.Length);

            sb.Append((char)FxControlCode._STX);
            sb.Append((char)cmd);
            sb.Append(addr.ToAddressHexString());
            sb.Append(FxConvert.DecToHex((uint)(length / 2), 2));
            sb.Append(data);
            sb.Append((char)FxControlCode._ETX);
            sb.Append(FxConvert.DecToHex(GetCheckSum(sb.ToString(), 1), 2));

            return(sb.ToString());
        }
示例#3
0
        /// <summary>
        /// 构建PLC FX命令
        /// 支持各种“读数据”或“查询”类命令,支持多种数据类型,例如 UInt16DataType,UIntDataType
        /// </summary>
        /// <param name="response">命令字</param>
        /// <param name="addr">起始地址</param>
        /// <param name="length">数据长度,以字节为单位。例如:如果是16bits则为2、32bits则为4</param>
        /// <returns>返回构造完成的命令串</returns>
        public static string Make(FxCommandConst cmd, FxAddress addr, int length)
        {
            StringBuilder sb = new StringBuilder(64);

            sb.Append((char)FxControlCode._STX);
            sb.Append((char)cmd);
            sb.Append(addr.ToAddressHexString());
            if (length > 0)
            {
                sb.Append(FxConvert.DecToHex((uint)length, 2));
            }
            sb.Append((char)FxControlCode._ETX);
            sb.Append(FxConvert.DecToHex(GetCheckSum(sb.ToString(), 1), 2));

            return(sb.ToString());
        }
示例#4
0
        public static string Make(FxCommandConst cmd, FxAddress addr, int length, byte[] data)
        {
            Debug.Assert(length == data.Length, "length(长度)必须与给定的数据实际一致");

            return(Make(cmd, addr, length, ASCIIEncoding.ASCII.GetString(data)));
        }
示例#5
0
 /// <summary>
 /// 构建PLC FX命令
 /// 这是针对 ushort 的特殊命令,也可使用 Make<UShortDataType>(...) 函数达到同样目的
 /// </summary>
 /// <param name="response">命令字</param>
 /// <param name="addr">起始地址</param>
 /// <param name="pureData">数据部分</param>
 /// <returns>返回构造完成的命令串</returns>
 public static string Make(FxCommandConst cmd, FxAddress addr, List <ushort> data)
 {
     return(Make(cmd, addr, data.Count * sizeof(ushort) * 2, ConvertToString(data)));
 }
示例#6
0
        /// <summary>
        /// 构建PLC FX命令
        /// 支持各种“写数据”命令,支持多种数据类型,例如 UInt16DataType,UInt32DataType
        /// </summary>
        /// <param name="response">命令字</param>
        /// <param name="addr">起始地址</param>
        /// <param name="pureData">数据部分</param>
        /// <returns>返回构造完成的命令串</returns>
        public static string Make <T>(FxCommandConst cmd, FxAddress addr, List <uint> data) where T : ICellDataType, new()
        {
            T t = new T();

            return(Make(cmd, addr, data.Count * t.DataItemHexStringSize, ConvertToString <T>(data)));
        }