示例#1
0
        private bool ReadGO(string adr, ref ushort data)
        {
            var clinet = new ModbusMachine(ModbusType.Tcp, IP, null, false, 2, 0);

            clinet.Connect();

            int iAdr;
            var ret = int.TryParse(adr, out iAdr);

            if (ret != true)
            {
                return(ret);
            }

            var addresses = new List <AddressUnit>()
            {
                new AddressUnit
                {
                    Id               = "LOWPART",
                    Area             = "1X",
                    Address          = iAdr,
                    SubAddress       = 0,
                    CommunicationTag = "LOWPART",
                    DataType         = typeof(byte)
                },
                new AddressUnit
                {
                    Id               = "HIGHPART",
                    Area             = "1X",
                    Address          = iAdr + 8,
                    SubAddress       = 0,
                    CommunicationTag = "HIGHPART",
                    DataType         = typeof(byte)
                },
            };

            for (int j = 0; j < MaxRecon; j++)
            {
                clinet.GetAddresses = addresses;
                var ans = clinet.GetDatas(MachineGetDataType.CommunicationTag);

                if (ans != null)
                {
                    data = (ushort)(ans["LOWPART"].PlcValue + ans["HIGHPART"].PlcValue * 256);
                    return(true);
                }
            }

            clinet.Disconnect();
            return(false);
        }
示例#2
0
        private bool ReadDO(string adr, ref bool data)
        {
            var clinet = new ModbusMachine(ModbusType.Tcp, IP, null, false, 2, 0);

            clinet.Connect();

            int iAdr;
            var ret = int.TryParse(adr, out iAdr);

            if (ret != true)
            {
                return(ret);
            }

            var addresses = new List <AddressUnit>()
            {
                new AddressUnit
                {
                    Id               = adr,
                    Area             = "1X",
                    Address          = iAdr,
                    SubAddress       = 0,
                    CommunicationTag = "DO" + adr,
                    DataType         = typeof(bool)
                }
            };

            for (int j = 0; j < MaxRecon; j++)
            {
                clinet.GetAddresses = addresses;
                var ans = clinet.GetDatas(MachineGetDataType.CommunicationTag);

                if (ans != null)
                {
                    data = ans["DO" + adr].PlcValue > 0 ? true : false;
                    clinet.Disconnect();
                    return(true);
                }
            }

            clinet.Disconnect();
            return(false);
        }
示例#3
0
        private bool WriteDI(string adr, bool data)
        {
            var clinet = new ModbusMachine(ModbusType.Tcp, IP, null, false, 2, 0);

            clinet.Connect();

            int iAdr;
            var ret = int.TryParse(adr, out iAdr);

            if (ret != true)
            {
                return(ret);
            }

            var div_adr = "0X " + iAdr.ToString();

            double div_data = data == false ? 0 : 1;

            var dic = new Dictionary <string, double>()
            {
                {
                    div_adr, div_data
                }
            };

            for (int j = 0; j < MaxRecon; j++)
            {
                ret = AsyncHelper.RunSync(() =>
                                          clinet.BaseUtility.GetUtilityMethods <IUtilityMethodWriteSingle>().SetSingleDataAsync(div_adr, dic[div_adr] >= 1));

                if (ret == true)
                {
                    break;
                }
            }

            clinet.Disconnect();
            return(ret);
        }