Exemplo n.º 1
0
        /// <summary>
        /// 获取Write命令
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected byte[] GetWriteCommand(OmronFinsAddress arg, byte[] value)
        {
            bool isBit = arg.IsBit;

            byte[] command = new byte[26 + 8 + value.Length];

            Array.Copy(BasicCommand, 0, command, 0, 4);
            byte[] tmp = BitConverter.GetBytes(command.Length - 8);
            Array.Reverse(tmp);
            tmp.CopyTo(command, 4);
            command[11] = 0x02;

            command[16] = 0x80;        //ICF 信息控制字段
            command[17] = 0x00;        //RSV 保留字段
            command[18] = 0x02;        //GCT 网关计数
            command[19] = 0x00;        //DNA 目标网络地址 00:表示本地网络  0x01~0x7F:表示远程网络
            command[20] = 0x13;        //DA1 目标节点编号 0x01~0x3E:SYSMAC LINK网络中的节点号 0x01~0x7E:YSMAC NET网络中的节点号 0xFF:广播传输
            command[21] = UnitAddress; //DA2 目标单元地址
            command[22] = 0x00;        //SNA 源网络地址 取值及含义同DNA字段
            command[23] = 0x0B;        //SA1 源节点编号 取值及含义同DA1字段
            command[24] = 0x00;        //SA2 源单元地址 取值及含义同DA2字段
            command[25] = 0x00;        //SID Service ID 取值0x00~0xFF,产生会话的进程的唯一标识

            command[26] = 0x01;
            command[27] = 0x02; //Command Code 内存区域写入
            command[28] = isBit ? arg.BitCode : arg.WordCode;
            arg.BitAddress.CopyTo(command, 29);
            command[32] = isBit ? (byte)(value.Length / 256) : (byte)(value.Length / 2 / 256);
            command[33] = isBit ? (byte)(value.Length % 256) : (byte)(value.Length / 2 % 256);
            value.CopyTo(command, 34);

            return(command);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 地址信息解析
        /// </summary>
        /// <param name="address"></param>
        /// <param name="isBit"></param>
        /// <returns></returns>
        private OmronFinsAddress ConvertArg(string address, bool isBit)
        {
            address = address.ToUpper();
            var addressInfo = new OmronFinsAddress()
            {
                IsBit = isBit
            };

            switch (address[0])
            {
            case 'D':    //DM区
            {
                addressInfo.BitCode  = 0x02;
                addressInfo.WordCode = 0x82;
                break;
            }

            case 'C':    //CIO区
            {
                addressInfo.BitCode  = 0x30;
                addressInfo.WordCode = 0xB0;
                break;
            }

            case 'W':    //WR区
            {
                addressInfo.BitCode  = 0x31;
                addressInfo.WordCode = 0xB1;
                break;
            }

            case 'H':    //HR区
            {
                addressInfo.BitCode  = 0x32;
                addressInfo.WordCode = 0xB2;
                break;
            }

            case 'A':    //AR区
            {
                addressInfo.BitCode  = 0x33;
                addressInfo.WordCode = 0xB3;
                break;
            }

            case 'E':
            {
                string[] address_split = address.Split('.');
                int      block_length  = Convert.ToInt32(address_split[0].Substring(1), 16);
                if (block_length < 16)
                {
                    addressInfo.BitCode  = (byte)(0x20 + block_length);
                    addressInfo.WordCode = (byte)(0xA0 + block_length);
                }
                else
                {
                    addressInfo.BitCode  = (byte)(0xE0 + block_length - 16);
                    addressInfo.WordCode = (byte)(0x60 + block_length - 16);
                }

                if (isBit)
                {
                    // 位操作
                    ushort address_location = ushort.Parse(address_split[1]);
                    addressInfo.BitAddress    = new byte[3];
                    addressInfo.BitAddress[0] = BitConverter.GetBytes(address_location)[1];
                    addressInfo.BitAddress[1] = BitConverter.GetBytes(address_location)[0];

                    if (address_split.Length > 2)
                    {
                        addressInfo.BitAddress[2] = byte.Parse(address_split[2]);
                        if (addressInfo.BitAddress[2] > 15)
                        {
                            //输入的位地址只能在0-15之间
                            throw new Exception("位地址数据异常");
                        }
                    }
                }
                else
                {
                    // 字操作
                    ushort address_location = ushort.Parse(address_split[1]);
                    addressInfo.BitAddress    = new byte[3];
                    addressInfo.BitAddress[0] = BitConverter.GetBytes(address_location)[1];
                    addressInfo.BitAddress[1] = BitConverter.GetBytes(address_location)[0];
                }
                break;
            }

            default:
                //类型不支持
                throw new Exception("Address解析异常");
            }

            if (address[0] != 'E')
            {
                if (isBit)
                {
                    // 位操作
                    string[] address_split    = address.Substring(1).Split('.');
                    ushort   address_location = ushort.Parse(address_split[0]);
                    addressInfo.BitAddress    = new byte[3];
                    addressInfo.BitAddress[0] = BitConverter.GetBytes(address_location)[1];
                    addressInfo.BitAddress[1] = BitConverter.GetBytes(address_location)[0];

                    if (address_split.Length > 1)
                    {
                        addressInfo.BitAddress[2] = byte.Parse(address_split[1]);
                        if (addressInfo.BitAddress[2] > 15)
                        {
                            //输入的位地址只能在0-15之间
                            throw new Exception("位地址数据异常");
                        }
                    }
                }
                else
                {
                    // 字操作
                    ushort address_location = ushort.Parse(address.Substring(1));
                    addressInfo.BitAddress    = new byte[3];
                    addressInfo.BitAddress[0] = BitConverter.GetBytes(address_location)[1];
                    addressInfo.BitAddress[1] = BitConverter.GetBytes(address_location)[0];
                }
            }

            return(addressInfo);
        }