Beispiel #1
0
        /// <summary>
        /// 同步发送接收
        /// </summary>
        /// <param name="buffer">Can't be null! </param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public DtuMsg Ssend(byte[] buffer, int timeout)
        {
            if (buffer == null || buffer.Length == 0)
            {
                log.Error("Null send buffer required. ");
                return(NewErrorMsg(Errors.ERR_NULL_SEND_DATA));
            }
            if (!this.IsOnline)
            {
                log.Error("Connection Not Ready");
                return(NewErrorMsg(Errors.ERR_NOT_CONNECTED));
            }
            if (this.Status != WorkingStatus.IDLE)
            {
                log.Error("I'm BUSY!");
                return(NewErrorMsg(Errors.ERR_DTU_BUSY));
            }
            this.Status = WorkingStatus.WORKING_SYNC;
            log.DebugFormat("SSending message, size={0}, timeout={1} s", buffer.Length, timeout);
            IDtuDataHandler  oldHandler = this.handler;
            AsyncMsgReceiver worker     = new AsyncMsgReceiver(this.DtuID, timeout);

            this.handler = worker;
            ServerManager.Send(this.DtuID, buffer);
            Thread thread = new Thread(worker.DoWork);

            thread.Start();
            thread.Join(); // wait thread dead.
            this.Status  = WorkingStatus.IDLE;
            this.handler = oldHandler;
            return(worker.Received());
        }
        /// <summary>
        /// 同步发送接收
        /// </summary>
        /// <param name="buffer">Can't be null! </param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public DtuMsg Ssend(byte[] buffer, int timeout)
        {
            if (buffer == null || buffer.Length == 0)
            {
                this._log.Error("Null send buffer required. ");
                return(NewErrorMsg((int)Errors.ERR_NULL_SEND_DATA));
            }
            if (!this.IsOnline)
            {
                this._log.Error("DtuConnection Not Ready");
                return(NewErrorMsg((int)Errors.ERR_NOT_CONNECTED));
            }
            if (this.Status != WorkingStatus.IDLE)
            {
                this._log.Error("I'm BUSY!");
                return(NewErrorMsg((int)Errors.ERR_DTU_BUSY));
            }
            this.Status = WorkingStatus.WORKING_SYNC;
            this._log.DebugFormat("DTU {0} SSending message, size={1}, {2}, timeout={3} s", this.DtuID, buffer.Length, ValueHelper.BytesToHexStr(buffer), timeout);
            IDtuDataHandler oldHandler = this._handler;
            var             worker     = new AsyncDtuMsgHandler(timeout);

            this._handler = worker;
            this._server.Send(this.DtuID, buffer);
            var thread = new Thread(worker.DoWork);

            thread.Start();
            thread.Join(); // wait thread dead.
            this.Status   = WorkingStatus.IDLE;
            this._handler = oldHandler;
            return(worker.Received());
        }
        public DtuMsg Ssend(byte[] buffer, int timeout)
        {
            try
            {
                if (!this.Connect())
                {
                    return(this.NewErrorMsg((int)Errors.ERR_NOT_CONNECTED));
                }
                if (this.Status != WorkingStatus.IDLE)
                {
                    return(this.NewErrorMsg((int)Errors.ERR_DTU_BUSY));
                }
                if (buffer == null || buffer.Length == 0)
                {
                    return(this.NewErrorMsg((int)Errors.ERR_NULL_SEND_DATA));
                }
                this.Status = WorkingStatus.WORKING_SYNC;
                IDtuDataHandler oldHandler = this._handler;

                this._port.DiscardInBuffer();
                this._port.DataReceived -= this.OnDataReceived;
                SyncComReceiver worker    = new SyncComReceiver(this, this.DtuID, timeout);
                Thread          t         = new Thread(worker.DoWork);
                long            sentStart = DateTime.Now.Ticks;
                t.Start();
                Log.DebugFormat("Sending message: {0} , timeout = {1}", buffer.Length, timeout);
                this._port.Write(buffer, 0, buffer.Length);
                Log.DebugFormat("Sent in {0} ms.", (System.DateTime.Now.Ticks - sentStart) / 10000);
                t.Join();
                this._port.DataReceived += this.OnDataReceived;

                return(worker.Received());
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("SSend error: {0}", ex.Message);
                return(this.NewErrorMsg((int)Errors.ERR_WRITE_COM, ex.Message));
            }
            finally
            {
                this.Status = WorkingStatus.IDLE;
            }
        }
 public void registerDataHandler(IDtuDataHandler handler)
 {
     this._handler = handler;
 }