Example #1
0
        /// <summary>
        /// 报文结构:
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="addressid"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static byte[] PackWriteByteCmd(int[] addr, int[] addressid, int[] count, List <byte[]> value, List <byte> cmdInLst)
        {
            if (addressid.Count() != addr.Count() || count.Count() != addr.Count() || value.Count() != addr.Count())
            {
                return(null);
            }

            //协议号+后面的字节数+站号+功能码+后面字节数+地址加设置字节数+...+CRC校验
            List <byte> cmdOutLst = new List <byte>();

            byte[] byteHeader = getRandom4Byte();

            //地址变换
            for (int i = 0; i < addr.Count(); i++)
            {
                cmdOutLst.AddRange(GetAbsAddr(addr[i], addressid[i], 4));
                cmdOutLst.AddRange(ConstantMethod.getDataHighLowByte(value[i].Count()));
                cmdOutLst.AddRange(value[i]);
            }

            DTTcpCmdPackAndDataUnpack.InserCount(cmdOutLst, 0);

            cmdOutLst.InsertRange(0, Constant.DTTcpFunctionWriteByteCmd);
            cmdOutLst.InsertRange(0, Constant.DTDeviceId);

            cmdOutLst.AddRange(ConstantMethod.CRC16_C(cmdOutLst.ToArray()));

            DTTcpCmdPackAndDataUnpack.InserCount(cmdOutLst, 0);



            cmdOutLst.InsertRange(0, byteHeader);
            //返回数据打包

            // for (int i = 0; i < count.Count(); i++)
            //{
            //  for (int j = 0; j < count[i]; j++)
            //  {
            cmdInLst.Add(0x00);
            cmdInLst.Add(0x00);
            // }

            // }
            // DTTcpCmdPackAndDataUnpack.InserCount(cmdInLst, 0);
            cmdInLst.InsertRange(0, Constant.DTTcpFunctionWriteByteCmd);
            cmdInLst.InsertRange(0, Constant.DTDeviceId);
            DTTcpCmdPackAndDataUnpack.InserCount(cmdInLst, 0);

            cmdInLst.InsertRange(0, byteHeader);



            return(cmdOutLst.ToArray());
        }
Example #2
0
        //读取多个位 IX MX QX IB QB
        //设置多个位  IX MX QX

        /// <summary>
        //0E 77 00 00      协议号 发什么返回什么
        //00 37            后面的字节数
        //01               站号
        //43 01            功能码
        //00 30            后面字节数
        //01 C0 20 01 00 01  MW0.1  地址加读取个数
        //01 C0 20 02 00 01  MW0.2
        //01 C0 1C 01 00 01  QX0.1
        //01 C0 1C 02 00 01  QX0.2
        //01 C0 20 03 00 01  MX0.3
        //01 C0 20 05 00 01  MX0.5
        //01 C0 20 2D 00 01  MX5.5
        //01 C0 20 00 00 01  MX0.0
        //A4 D4
        //回复:
        //B7 68 00 00     协议号 发什么返回什么
        //00 1D           后面的字节数
        //01              站号
        //43 01           功能码
        //00 18           后面的字节数
        //00 01 00        字节数00 01+值00--- MW0.1   false
        //00 01 01        字节数00 01+值00--- MW0.2   true
        //00 01 00       QX0.1
        //00 01 00       QX0.2
        //00 01 00       MX0.3
        //00 01 00       MX0.5
        //00 01 00       MX5.5
        //00 01 00       MX0.0

        /// </summary>
        /// 输入地址 地址属性 地址后面要读取的个数
        /// <param name="addr"></param>
        /// <param name="addressid"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static byte[] PackReadBitCmd(int[] addr, int[] addressid, int[] count, List <byte> cmdInLst)
        {
            if (addressid.Count() != addr.Count() || count.Count() != addr.Count())
            {
                return(null);
            }

            //协议号+后面的字节数+站号+功能码+后面字节数+地址加读取字节数+...+CRC校验
            List <byte> cmdOutLst = new List <byte>();

            //地址变换
            for (int i = 0; i < addr.Count(); i++)
            {
                cmdOutLst.AddRange(GetAbsAddr(addr[i], addressid[i], 4));
                cmdOutLst.AddRange(ConstantMethod.getDataHighLowByte(count[i]));
            }

            DTTcpCmdPackAndDataUnpack.InserCount(cmdOutLst, 0);

            cmdOutLst.InsertRange(0, Constant.DTTcpFunctionReadBitCmd);
            cmdOutLst.InsertRange(0, Constant.DTDeviceId);

            cmdOutLst.AddRange(ConstantMethod.CRC16_C(cmdOutLst.ToArray()));

            DTTcpCmdPackAndDataUnpack.InserCount(cmdOutLst, 0);

            cmdOutLst.InsertRange(0, Constant.DTTcpHeader);


            /***
             * 输入数据
             *
             ****/

            for (int i = 0; i < count.Count(); i++)
            {
                int cnt = (int)Math.Ceiling((double)count[i] / 8);
                cmdInLst.AddRange(ConstantMethod.getDataHighLowByte(cnt));
                for (int j = 0; j < cnt; j++)
                {
                    cmdInLst.Add(0x00);
                }
            }
            DTTcpCmdPackAndDataUnpack.InserCount(cmdInLst, 0);
            cmdInLst.InsertRange(0, Constant.DTTcpFunctionReadBitCmd);
            cmdInLst.InsertRange(0, Constant.DTDeviceId);
            DTTcpCmdPackAndDataUnpack.InserCount(cmdInLst, 0);

            cmdInLst.InsertRange(0, Constant.DTTcpHeader);


            return(cmdOutLst.ToArray());
        }
Example #3
0
        static byte[] ASPLCPackReadByteCmd(int[] addr, int[] addressid, int[] count, List <byte> cmdInLst)
        {
            if (addressid.Count() != addr.Count() || count.Count() != addr.Count())
            {
                return(null);
            }
            //协议号+后面的字节数+站号+功能码+后面字节数+地址加读取字节数+...+
            List <byte> cmdOutLst = new List <byte>();

            //地址变换
            for (int i = 0; i < addr.Count(); i++)
            {
                cmdOutLst.AddRange(AsPlcGetAbsAddr(addr[i], addressid[i], 4));
                cmdOutLst.AddRange(ConstantMethod.getDataHighLowByte(count[i]));
            }

            DTTcpCmdPackAndDataUnpack.InserCount(cmdOutLst, 0);

            cmdOutLst.InsertRange(0, Constant.DTAsPlcTcpFunctionReadByteCmd);
            cmdOutLst.InsertRange(0, Constant.DTAsPlcDeviceId);

            DTTcpCmdPackAndDataUnpack.InserCount(cmdOutLst, 0);

            cmdOutLst.InsertRange(0, Constant.DTTcpHeader);

            /***
             * 输入数据
             *
             ****/

            for (int i = 0; i < count.Count(); i++)
            {
                cmdInLst.AddRange(ConstantMethod.getDataHighLowByte(count[i]));
                for (int j = 0; j < count[i]; j++)
                {
                    //一个单元 两个8位
                    cmdInLst.Add(0x00);
                    cmdInLst.Add(0x00);
                }
            }
            DTTcpCmdPackAndDataUnpack.InserCount(cmdInLst, 0);
            cmdInLst.InsertRange(0, Constant.DTAsPlcTcpFunctionReadByteCmd);
            cmdInLst.InsertRange(0, Constant.DTAsPlcDeviceId);
            DTTcpCmdPackAndDataUnpack.InserCount(cmdInLst, 0);

            cmdInLst.InsertRange(0, Constant.DTTcpHeader);


            return(cmdOutLst.ToArray());
        }
Example #4
0
        /// <summary>
        /// 报文结构:
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="addressid"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static byte[] PackWriteBitCmd(int[] addr, int[] addressid, int[] count, List <byte[]> value, List <byte> cmdInLst)
        {
            if (addressid.Count() != addr.Count() || count.Count() != addr.Count() || value.Count() != addr.Count())
            {
                return(null);
            }

            //协议号+后面的字节数+站号+功能码+后面字节数+地址加设置字节数+...+CRC校验
            List <byte> cmdOutLst = new List <byte>();

            byte[] byteHeader = getRandom4Byte();

            //地址变换  位的话 个数 和 字节数 不一定相等 8个位才算一个字节
            for (int i = 0; i < addr.Count(); i++)
            {
                cmdOutLst.AddRange(GetAbsAddr(addr[i], addressid[i], 4));
                cmdOutLst.AddRange(ConstantMethod.getDataHighLowByte(count[i]));  //设置多少个位
                int cnt = (int)Math.Ceiling((double)count[i] / 8);
                if (value[i].Count() != cnt)
                {
                    return(null);
                }
                cmdOutLst.AddRange(ConstantMethod.getDataHighLowByte(value[i].Count()));  //后面有几个字节
                cmdOutLst.AddRange(value[i]);
            }

            DTTcpCmdPackAndDataUnpack.InserCount(cmdOutLst, 0);

            cmdOutLst.InsertRange(0, Constant.DTTcpFunctionWriteBitCmd);
            cmdOutLst.InsertRange(0, Constant.DTDeviceId);

            cmdOutLst.AddRange(ConstantMethod.CRC16_C(cmdOutLst.ToArray()));

            DTTcpCmdPackAndDataUnpack.InserCount(cmdOutLst, 0);

            cmdOutLst.InsertRange(0, byteHeader);

            //返回的数据
            cmdInLst.Add(0x00);
            cmdInLst.Add(0x00);
            cmdInLst.InsertRange(0, Constant.DTTcpFunctionWriteBitCmd);
            cmdInLst.InsertRange(0, Constant.DTDeviceId);
            DTTcpCmdPackAndDataUnpack.InserCount(cmdInLst, 0);

            cmdInLst.InsertRange(0, byteHeader);

            return(cmdOutLst.ToArray());
        }