Ejemplo n.º 1
0
        public object ReadItem(string address)
        {
            //lock (this)
            //{
            if (_dbAddress.ContainsKey(address))
            {
                ABLogixPLCAddress addr = _dbAddress[address];

                if (addr.Type == "bool")
                {
                    byte[] _buf = _dbAreaBuf[addr.ArrayName];
                    if (0 <= addr.BoolOffset && addr.BoolOffset < 8)
                    {
                        bool result = ((_buf[addr.index * 2] & (1 << addr.BoolOffset)) != 0);
                        return(result);
                    }
                    else if (8 <= addr.BoolOffset && addr.BoolOffset < 16)
                    {
                        bool result = ((_buf[addr.index * 2 + 1] & (1 << (addr.BoolOffset - 8))) != 0);
                        return(result);
                    }
                    //else if (16 <= addr.BoolOffset && addr.BoolOffset < 24)
                    //{
                    //    bool result = ((_buf[addr.index * 4+2] & (1 << (addr.BoolOffset-16))) != 0);
                    //    return result;
                    //}
                    //else if (24 <= addr.BoolOffset && addr.BoolOffset < 32)
                    //{
                    //    bool result = ((_buf[addr.index * 4+3] & (1 << (addr.BoolOffset-24))) != 0);
                    //    return result;
                    //}
                    else
                    {
                        throw new Exception("addr.BoolOffset大于15");
                    }
                }
                else if (addr.Type == "byte")
                {
                    byte[] _buf   = _dbAreaBuf[addr.ArrayName];
                    byte   result = _buf[addr.index];
                    return(result);
                }
                else if (addr.Type == "int16")
                {
                    byte[] _buf       = _dbAreaBuf[addr.ArrayName];
                    byte   _low_byte  = _buf[addr.index * 2];
                    byte   _high_byte = _buf[addr.index * 2 + 1];

                    Int16 result = (Int16)((_high_byte << 8) + _low_byte);
                    return(result);
                }
                else if (addr.Type == "int32")
                {
                    int    begin = addr.index * 4;
                    byte[] _buf  = _dbAreaBuf[addr.ArrayName];

                    Int32 result = (Int32)((_buf[begin + 3] << 24) + (_buf[begin + 2] << 16) + (_buf[begin + 1] << 8) + _buf[begin]);
                    return(result);
                }
                else if (addr.Type == "string")
                {
                    int tempstrlen = addr.StrLen + 4;
                    if (tempstrlen % 4 == 0)
                    {
                    }
                    else
                    {
                        tempstrlen = ((tempstrlen + 4) / 4) * 4;
                    }
                    int    begin = addr.index * tempstrlen + 2; //String的字节数组,从索引2开始
                    byte[] _buf  = _dbAreaBuf[addr.ArrayName];
                    //int strlen= (Int32)((_buf[begin] << 24) + (_buf[begin+1] << 16) + (_buf[begin+2] << 8) + _buf[begin+3]);
                    int    strlen = (_buf[begin + 3] << 24) + (_buf[begin + 2] << 16) + (_buf[begin + 1] << 8) + _buf[begin];
                    string result = System.Text.Encoding.ASCII.GetString(_buf, begin + 4, strlen).Replace("\0", ""); //AB字符串前四个字节是实际长度
                    return(result);                                                                                  //= System.Text.RegularExpressions.Regex.Replace(result, @"[\u0001-\u001F]", ""); //过滤不可显示字符
                }
                else
                {
                    throw new Exception("Tag类型不支持");
                }
            }
            else
            {
                throw new Exception("读取Tag集合中未找到该地址");
            }
            return(null);
            //}
        }
Ejemplo n.º 2
0
        private Dictionary <string, byte[]> _dbAreaBuf            = new Dictionary <string, byte[]>(); //前一个数组名,后一个缓存区
        public void AddAddress(Tag tag)
        {
            try
            {
                if (tag.AccessType == TagAccessType.Read || tag.AccessType == TagAccessType.ReadWrite)
                {
                    string            address = tag.Address;
                    ABLogixPLCAddress addr    = ABLogixPLCAddress.ConvertAddress(address, tag.TagType);
                    _dbAddress.Add(address, addr);

                    if (addr != null)
                    {
                        Int16 len = (Int16)(addr.index + 1);
                        if (ReadAreaMaxLen.ContainsKey(addr.ArrayName))
                        {     //如果存在该DB ID
                            if (ReadAreaMaxLen[addr.ArrayName] < len)
                            { //如果是最大地址
                                ReadAreaMaxLen[addr.ArrayName] = len;
                                if (addr.Type == "int32")
                                {
                                    ReceiveByteMaxLen[addr.ArrayName] = (len * 4);
                                }
                                else if (addr.Type == "byte")
                                {
                                    ReceiveByteMaxLen[addr.ArrayName] = (len);
                                }
                                else if (addr.Type == "int16" || addr.Type == "bool")
                                {
                                    ReceiveByteMaxLen[addr.ArrayName] = (len * 2);
                                }
                                else if (addr.Type == "string")
                                {
                                    int tempstrlen = addr.StrLen + 4;
                                    if (tempstrlen % 4 == 0)
                                    {
                                    }
                                    else
                                    {
                                        tempstrlen = ((tempstrlen + 4) / 4) * 4;
                                    }
                                    ReceiveByteMaxLen[addr.ArrayName] = (len * tempstrlen + 2);
                                }
                                else
                                {
                                    throw new Exception("Tag类型不识别.");
                                }
                            }
                        }
                        else
                        {
                            ReadAreaMaxLen.Add(addr.ArrayName, len);
                            if (addr.Type == "int32")
                            {
                                ReceiveByteMaxLen.Add(addr.ArrayName, (len * 4));
                            }
                            else if (addr.Type == "byte")
                            {
                                ReceiveByteMaxLen.Add(addr.ArrayName, (len));
                            }
                            else if (addr.Type == "int16" || addr.Type == "bool")
                            {
                                ReceiveByteMaxLen.Add(addr.ArrayName, (len * 2));
                            }
                            else if (addr.Type == "string")
                            {
                                int tempstrlen = addr.StrLen + 4;
                                if (tempstrlen % 4 == 0)
                                {
                                }
                                else
                                {
                                    tempstrlen = ((tempstrlen + 4) / 4) * 4;
                                }
                                ReceiveByteMaxLen.Add(addr.ArrayName, (len * tempstrlen + 2));
                            }
                            else
                            {
                                throw new Exception("Tag类型不识别.");
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("ABLogixPLCAddress.ConvertAddress后的addr为null");
                    }
                }
            }
            catch (Exception ex)
            {
                LOG.Error(string.Format("AB驱动添加地址{0}出错{1}", tag.Address, ex));
            }
        }