Exemplo n.º 1
0
        public void Initialize()
        {
            try
            {
                var command  = ProtocolFinsCommand.GetHandshakeCommand(pcNode);
                var response = new byte[] { };

                if (!comPort.IsConnected)
                {
                    comPort.Open(false);
                }

                response = Send(command);

                if (!response.Any() || GetErrorCode(response) != ErrorCodeOK)
                {
                    throw new ApplicationException($"握手失败,错误码:{GetErrorCode(response)}");
                }
                else
                {
                    pcNode  = response[19];
                    plcNode = response[23];
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException("建立与PLC连接失败,请确认", e);
            }
        }
Exemplo n.º 2
0
        public IEnumerable <TValue> Read <TValue>(DataAddress address)
        {
            if (address.Equals(DataAddress.Empty))
            {
                throw new ApplicationException("地址为空");
            }

            var command = ProtocolFinsCommand.GetReadCommand(address, pcNode, plcNode);

            byte[] response = { };

            using (new StopwatchWrapper($"读取PLC"))
            {
                response = Send(command);
            }

            if (!response.Any() || GetErrorCode(response) != ErrorCodeOK)
            {
                Reconnect();

                throw new ApplicationException($"读取失败,地址:{address },错误码:{GetErrorCode(response)}");
            }

            return(GetValues <TValue>(address, response));
        }
Exemplo n.º 3
0
        public void Write <TValue>(DataAddress address, IEnumerable <TValue> values)
        {
            if (address.Equals(DataAddress.Empty))
            {
                throw new ApplicationException("地址为空");
            }
            if (values == null || !values.Any())
            {
                throw new ApplicationException($"参数异常,values为null或空");
            }

            var command = ProtocolFinsCommand.GetWriteCommand(address, values, pcNode, plcNode);

            byte[] response = { };

            using (new StopwatchWrapper($"写入PLC"))
            {
                response = Send(command);
            }

            if (!response.Any() || GetErrorCode(response) != ErrorCodeOK)
            {
                Reconnect();

                throw new ApplicationException($"写入失败,地址:{address },错误码:{GetErrorCode(response)}");
            }
        }