Exemplo n.º 1
0
        /// <summary>
        /// Handle recieved data. This should be called when data is recieved from TCP or Serial handler
        /// </summary>
        /// <param name="pData"></param>
        public void OnDataRecieved(byte[] pData)
        {
            lock (lockQue) {
                //create MBMsg object based on RTU setting
                MBMsg msg;
                if (IsRTU)
                {
                    msg = new MBMsgRTU();
                }
                else
                {
                    msg = new MBMsgTCP();
                }

                //make a Message from the buffer
                if (msg.FromArray(pData))                                       //if message from array success
                {
                    _que[0].Cmd.HandleMsg(msg);                                 //forward msg to first cmd in buffer
                }
                if (!IsRTU && ((MBMsgTCP)msg).TransactionID != ((MBMsgTCP)(_que[0].Msg)).TransactionID)
                {
                    _OnError("Lost transaction");
                }

                _que.RemoveAt(0);                                                       //remove from que, either successfully handled or something wrong with incomming data. Either way, this transaction is done
            }
        }
Exemplo n.º 2
0
        /*
         * public MBController()
         *      : this("default") {
         * }
         */
        #endregion


        /// <summary>
        /// Que a new message in the buffer
        /// </summary>
        /// <param name="nUnitID"></param>
        /// <param name="nFuncID"></param>
        /// <param name="pMsgData"></param>
        /// <param name="cmd"></param>
        public void QueMsg(byte nUnitID, MBFunctionType nFuncID, byte[] pMsgData, MBCommand cmd)
        {
            MBMsg msg;

            lock (lockQue) {
                //create a message
                if (IsRTU)
                {
                    msg = new MBMsgRTU(nUnitID, nFuncID, pMsgData);
                }
                else
                {
                    msg = new MBMsgTCP(nUnitID, nFuncID, pMsgData)
                    {
                        TransactionID = _nTransactionID
                    }
                };
                _nTransactionID++;                              //increment transaction id

                //add to que
                _que.Add(new MBMsgCmdItem(msg, cmd));

                if (_que.Count > MAX_QUE)
                {
                    int nRemove = _rnd.Next(MAX_QUE - 1) + 1;
                    _que.RemoveAt(nRemove);                     //remove at random to avoid any kind of pattern, not 0, since 0 may be in the process of beeing send. 1 never is (due to lock on buffer)
                    _OnError("Buffer full, removing item " + nRemove.ToString());
                }
            }
        }