Exemplo n.º 1
0
 public void WriteItems(Tag tag, object value)
 {
     lock (this)
     {
         try
         {
             CNCAddress addr = CNCAddress.ConvertAddress(tag.Address);
             //if (addr.ValType == "Bool" || tag.TagType =="bool")
             if (addr.ValType == "Bool")
             {
                 if (addr.AddrType == "Y")
                 {
                     WritePmcYAddrBool(addr.BlockID, addr.BitOffset, Convert.ToBoolean(value));
                 }
                 else
                 {
                     throw new Exception("暂时不支持该地址的bool类型的写入");
                 }
             }
             else
             {
                 if (addr.AddrType == "R")
                 {
                     WritePmcRAddr(addr.BlockID, Convert.ToInt16(value));
                 }
                 else if (addr.AddrType == "K")
                 {
                     WritePmcKAddr(addr.BlockID, Convert.ToInt16(value));
                 }
                 else if (addr.AddrType == "M")
                 {
                     WritePmcMacroAddr(Convert.ToInt16(addr.BlockID), Convert.ToInt32(value));
                 }
                 else
                 {
                     throw new Exception("暂时不支持该类型地址的写入");
                 }
             }
         }
         catch (Exception ex)
         {
             LOG.Error(string.Format("CNC Driver 写数据出错:{0}", ex.Message));
         }
     }
 }
Exemplo n.º 2
0
        public object ReadItem(string address)
        {
            lock (this)
            {
                try
                {
                    CNCAddress addr = CNCAddress.ConvertAddress(address);
                    if (addr.AddrType == "Y")
                    {
                        short ret = GetYValue(addr.BlockID);

                        if (addr.ValType == "Bool")
                        {
                            return(Convert.ToBoolean(ret & Convert.ToByte(Math.Pow(2, addr.BitOffset))));
                        }
                        else
                        {
                            return(ret);
                        }
                    }
                    else if (addr.AddrType == "R")
                    {
                        short val = 0;
                        short ret = ReadPmcRAddr(Convert.ToUInt16(addr.BlockID), out val);
                        if (ret == -16)
                        {
                            throw new Exception("CNC读取R出错" + ret);
                        }
                        else
                        {
                            if (addr.ValType == "Bool")
                            {
                                return(Convert.ToBoolean(val & Convert.ToByte(Math.Pow(2, addr.BitOffset))));
                            }
                            else
                            {
                                return(val);
                            }
                        }
                    }
                    else if (addr.AddrType == "F")
                    {
                        short val = 0;
                        short ret = ReadPmcFAddr(Convert.ToUInt16(addr.BlockID), out val);
                        if (ret == -16)
                        {
                            throw new Exception("CNC读取F出错" + ret);
                        }
                        else
                        {
                            if (addr.ValType == "Bool")
                            {
                                return(Convert.ToBoolean(val & Convert.ToByte(Math.Pow(2, addr.BitOffset))));
                            }
                            else
                            {
                                return(val);
                            }
                        }
                    }
                    else if (addr.AddrType == "X")
                    {
                        short val = 0;
                        short ret = ReadPmcXAddr(Convert.ToUInt16(addr.BlockID), out val);
                        if (ret == -16)
                        {
                            throw new Exception("CNC读取X出错" + ret);
                        }
                        else
                        {
                            if (addr.ValType == "Bool")
                            {
                                return(Convert.ToBoolean(val & Convert.ToByte(Math.Pow(2, addr.BitOffset))));
                            }
                            else
                            {
                                return(val);
                            }
                        }
                    }
                    else if (addr.AddrType == "K")
                    {
                        short val = 0;
                        short ret = ReadPmcKAddr(Convert.ToUInt16(addr.BlockID), out val);
                        if (ret == -16)
                        {
                            throw new Exception("CNC读取K出错" + ret);
                        }
                        else
                        {
                            if (addr.ValType == "Bool")
                            {
                                return(Convert.ToBoolean(val & Convert.ToByte(Math.Pow(2, addr.BitOffset))));
                            }
                            else
                            {
                                return(val);
                            }
                        }
                    }
                    else if (addr.AddrType == "M")
                    {
                        short val = 0;
                        short ret = ReadPmcMacroAddr(Convert.ToInt16(addr.BlockID), out val);
                        if (ret == -16)
                        {
                            throw new Exception("CNC读取K出错" + ret);
                        }
                        else
                        {
                            if (addr.ValType == "Bool")
                            {
                                return(Convert.ToBoolean(val & Convert.ToByte(Math.Pow(2, addr.BitOffset))));
                            }
                            else
                            {
                                return(val);
                            }
                        }
                    }
                    else if (addr.AddrType == "A")
                    {
                        short val = 0;
                        short ret = ReadPmcAAddr(Convert.ToUInt16(addr.BlockID), out val);
                        if (ret == -16)
                        {
                            throw new Exception("CNC读取A出错" + ret);
                        }
                        else
                        {
                            if (addr.ValType == "Bool")
                            {
                                return(Convert.ToBoolean(val & Convert.ToByte(Math.Pow(2, addr.BitOffset))));
                            }
                            else
                            {
                                return(val);
                            }
                        }
                    }
                    else if (addr.AddrType == "P")
                    {
                        return(ReadParameterValue(Convert.ToInt16(addr.ParaOffset)));
                    }
                    else if (addr.AddrType == "V")
                    {
                        return(ReadPmcAlarm());
                    }
                    else
                    {
                        throw new Exception("地址错误");
                    }
                }
                catch (Exception ex)
                {
                    LOG.Error(string.Format("CNC Driver 读数据出错:{0}", ex.Message));
                    return(null);
                }
            }
        }