示例#1
0
        /// <summary>
        /// 异步写数据
        /// </summary>
        /// <param name="dataAddress">格式为 ; 分开 s=Station;f=读取功能;d=地址;l=长度 </param>
        /// <param name="dataValues"></param>
        /// <returns></returns>
        public OperateResult AsyncWriteData(string dataAddress, string dataValues)
        {
            #region 检验

            if (string.IsNullOrEmpty(dataValues))
            {
                return(new OperateResult("传入的参数都不能为空."));
            }

            if (socketClient == null)
            {
                return(new OperateResult("Modbus 设备未初始化,请先初始化."));
            }

            if (!socketClient.Connected)
            {
                return(new OperateResult("Modbus 设备连接未连接,请重试."));
            }

            #endregion

            lock (lockwrite)
            {
                //发送数据
                CurServerResult.Clear();

                var    tag  = DeviceTags.FirstOrDefault(c => c.Address == dataAddress);
                string pkno = (tag != null) ? tag.PKNO : "AsyncWriteData";

                byte[] data = AnalysisWriteAddress(dataAddress, dataValues, WriteRegister);

                if (data == null)
                {
                    return(new OperateResult("Modbus 异步写数据时,地址转换失败!"));
                }

                int result = socketClient.AsyncSend(data);

                if (result != 0)
                {
                    return(new OperateResult("Modbus 异步写数据时,发送失败,错误:" + ((result == 1) ? "未连接" : "发送错误")));
                }

                #region  照特殊协议分别处理


                #endregion
            }

            return(OperateResult.CreateSuccessResult());
        }