⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udldcfg.c

📁 工业组态软件modbus驱动源代码, 包括帮助文件.共享.
💻 C
📖 第 1 页 / 共 5 页
字号:
    lpBestFitMsg = (LPUDMSG) NULL;
    first = TRUE;

    /* try to find an existing message into which the read can be placed */
    while ((lpMsg != (LPUDMSG) NULL) && (!found)) {
        /* Don't check message that are waiting to be deleted
           (mmActiveCt == 0) */
        if ((lpMsg->mmActiveCt != 0 ) &&
            (lpMsg->mmMsgType == msgType)) {
            if ((lpMsg->mmStartAddr <= myAddr) && (endAddr <= lpMsg->mmEndAddr)) {
                /* message includes this symbol, folding is possible */
                found = TRUE;
                lpFoundMsg = lpMsg;
                folding = TRUE;
            } else {
                /* get new start, end address for possible combined message */
                newStart = (WORD) min (lpMsg->mmStartAddr, myAddr);
                newEnd   = (WORD) max (lpMsg->mmEndAddr,   endAddr);
                /* check size of possible new message */
                size = (WORD) ((newEnd + 1) - newStart);
                if (size <= MAX_PTS) {
                    /* it will fit, but is it the best choice?
                       check difference between present range size and new one */
                    delta = size - lpMsg->mmCount;
                    if (first || (delta < bfDelta)) {
                        /* this is the new best fit, save size delta and pointers */
                        first = FALSE;
                        lpBestFitMsg = lpMsg;
                        bfDelta = delta;
                        bfStart = newStart;
                        bfEnd   = newEnd;
                        bfCount = size;
                    }
                }
            }
        }
        if (!found) {
            /* advance to next message */
            lpMsg = (LPUDMSG) FindNextItem (&message_scanner);
        }
    }

    /** check whether a compatible existing message was found,
        even if we couldn't fold **/
    if ((!folding) && (lpBestFitMsg != (LPUDMSG) NULL)) {
        /* indicate message found, get its pointer */
        found = TRUE;
        lpFoundMsg = lpBestFitMsg;
    }

    /* if match found, extend the message to handle the activated item **/
    if (found) {
        /* get pointer to message, indicate message not ready */
        lpMsg = lpFoundMsg;
        msg_ready = FALSE;
        /* check whether folding this read into an existing message */
        if (folding) {
            /* no change to message needed */
            msg_ready = TRUE;
        } else {
            /* build modified read message */
			BYTE plcAddr = lpTopic->statTopic;
            msgSize = BldRead (plcAddr,
							   &lpData,
                               plcDataType, DDEType,
                               bfStart, bfCount);
            if (msgSize != 0) {
                /* copy modified message to message structure */
                lpMsg->mmSize = msgSize;
                _fmemcpy (lpMsg->mmData, lpData, msgSize);

                /* update polling range for message */
                lpMsg->mmStartAddr = bfStart;
                lpMsg->mmEndAddr   = bfEnd;
                lpMsg->mmCount     = bfCount;

                /* indicate message has changed and is ready */
                lpMsg->mmChanged = TRUE;
                msg_ready = TRUE;
            }
        }

        /* check whether message generation was successful */
        if (msg_ready) {
            /* store pointer to message in symbol table, bump active count */
            lpSymEnt->msPollMsg = lpMsg;
            lpMsg->mmActiveCt++;

            /* get pointer to first symbol entry associated with the read */
            lpSymOld = &lpSymTab[lpMsg->mmFirstSym-SYM_OFFSET];
            /* compare to this symbol */
            if (LogicalAddrCmp (lpSymEnt, lpSymOld) < 0) {
                /* store handle for new first symbol */
                lpMsg->mmFirstSym = SymHandle;
            }

            /* get pointer to last symbol entry associated with the read */
            lpSymOld = &lpSymTab[lpMsg->mmLastSym-SYM_OFFSET];
            /* compare to this symbol */
            if (LogicalAddrCmp (lpSymOld, lpSymEnt) < 0) {
                /* store handle for new last symbol */
                lpMsg->mmLastSym = SymHandle;
            }
        }
    } else {
        /****************************************************************\
           create a new read message for the new data point
        \****************************************************************/

        /* build new message */
		BYTE plcAddr = lpTopic->statTopic;
        msgSize = BldRead (plcAddr,
						   &lpData,
                           plcDataType, DDEType,
                           myAddr, count);

        /* allocate new message structure */
        allocSize = MAX_MSG_SIZE + sizeof(UDMSG);
        lpMsg = (LPUDMSG) wwHeap_AllocPtr(hHeap,
                                          GMEM_MOVEABLE | GMEM_ZEROINIT,
                                          (DWORD) allocSize);
        if (lpMsg != (LPUDMSG) NULL) {

            /* copy new message to message structure */
            lpMsg->mmSize = msgSize;
            _fmemcpy (lpMsg->mmData, lpData, (size_t) msgSize);

            /* set pointers to station, symbol, etc. */
            lpMsg->mmTopic      = lpTopic;
            lpMsg->mmTopicId    = lpTopic->statTopic;
            lpMsg->mmIdLogDev   = lpTopic->statIdLogDev;
            lpMsg->mmFirstSym   = SymHandle;
            lpMsg->mmLastSym    = SymHandle;
            lpMsg->mmActiveCt   = 1;
            lpMsg->mmDue        = TRUE;    /* Read immediately */
            lpMsg->mmRead       = TRUE;
            lpMsg->mmScanTimer  = lpTopic->statUpdatePeriod;
            lpMsg->mmScanReload = lpTopic->statUpdatePeriod;
            /**************************************************************\
              Initialize the protocol dependent message fields here.
            \**************************************************************/
            lpMsg->mmMsgInBinary= bBldSendMsgInBinary;
            lpMsg->mmMsgType    = msgType;
            lpMsg->mmStartAddr  = myAddr;
            lpMsg->mmEndAddr    = endAddr;
            lpMsg->mmCount      = count;
            lpMsg->mmDiscrete   = discrete;

            /* indicate message has changed */
            lpMsg->mmChanged    = TRUE;

            /* store pointer to message in symbol table */
            lpSymEnt->msPollMsg = lpMsg;

            /*
             * Chain the newly allocated message into the read
             * message list on the topic.
             */

            AppendItemAtTail (&lpTopic->statReadMsgList, (LPCHAINLINK) lpMsg);

            /* check whether station is currently servicing a message */
            if (lpTopic->statCurMsg == (LPUDMSG) NULL) {
                /* no, set current message to new message */
                lpTopic->statCurMsg = lpMsg;
            }
        } else {
#ifdef DEBUG_CALL_TRAFFIC
            if (Verbose)
                debug ("UdprotAddPoll - can't alloc for message");
#endif
            return FALSE;   /* Couldn't allocate memory */
        }
    }

    assert(lpMsg);

    /* Force an immediate read */
    lpMsg->mmScanTimer = lpMsg->mmScanReload;
    lpMsg->mmDue = TRUE;

    /* indicate success */
    return ok;
} /* UdprotAddPoll */

/***********************************************************************/
/** delete polling for indicated point from queue of messages for station
    returns TRUE if successful **/

BOOL
WINAPI
UdprotDelPoll(LPSTAT lpTopic,
              SYMPTR lpSymEnt)
{
    LPSTR           lpData;
    LPUDMSG         lpMsg;
    unsigned long   SymHandle, newHandle;
    unsigned long   firstIdx, lastIdx;
    SYMPTR          lpSymTab;
    BOOL            ok;
    BOOL            msgChanged, blockSizeChanged;
    WORD            msgSize;
    BYTE            plcDataType;
    BYTE            DDEType;
	BYTE			plcAddr;

#ifdef DEBUG_CALL_TRAFFIC
    if (Verbose)
        debug ("UdprotDelPoll( %Fp, %Fp )", lpTopic, lpSymEnt);
#endif

    /* indicate error if pointers are NULL */
    assert(lpTopic);
    assert(lpSymEnt);

    /* get pointer to symbol table for station */
    lpSymTab = (SYMPTR) lpTopic->statSymTab.first_member;
    assert(lpSymTab);

    /* get handle for symbol table entry */
    SymHandle = lpSymEnt->msIndex + SYM_OFFSET;

    /* set type of data message from symbol table entry */
    plcDataType = (BYTE) lpSymEnt->msPlcDataType;
    DDEType     = (BYTE) lpSymEnt->msDdeType;

    /* intialize return value */
    ok = TRUE;

    /* initialize flags */
    msgChanged = FALSE;
    blockSizeChanged = FALSE;

    /* get pointer to polling message */
    lpMsg = lpSymEnt->msPollMsg;
    /* clear polling message from symbol table entry */
    lpSymEnt->msPollMsg = (LPUDMSG) NULL;

    if (lpMsg == (LPUDMSG) NULL) {
        /* unable to access message, indicate error and return */
        return (FALSE);
    }

    /* decrement number of points active for this message */
    lpMsg->mmActiveCt--;
    if (lpMsg->mmActiveCt == 0) {
        /* deactivating the last point */
        /* clear range of points polled for this message */
        lpMsg->mmStartAddr = 0;
        lpMsg->mmEndAddr = 0;
        lpMsg->mmCount = 0;
        lpMsg->mmFirstSym = 0;
        lpMsg->mmLastSym = 0;
        /* indicate message changed, it will be deleted later */
        lpMsg->mmChanged = TRUE;
        /* indicate success, return */
        return TRUE;
    }

    /* at least one point is still being polled by this message,
       remove this symbol while leaving the rest of the message intact */
    lpSymEnt->msActive = FALSE;
    if (SymHandle == lpMsg->mmFirstSym) {
        /* Deactivating a point that spans the first alias in the poll */

        /* scan forward through symbol table
           for next active symbol in same message */
        newHandle = FindNextActive(lpTopic, lpSymEnt, lpMsg);
        if (newHandle == 0) {
            /* no symbol found -- error */
            ASSERT_ERROR;
        } else {
            /* update handle for first symbol */
            lpMsg->mmFirstSym = newHandle;
            /* indicate message changed */
            msgChanged = TRUE;
            /* indicate block size changed */
            blockSizeChanged = TRUE;
        }
    } else if (SymHandle == lpMsg->mmLastSym) {
        /* Deactivating a point that spans the last alias in the poll */

        /* scan backward through symbol table
           for previous active symbol in same message */
        newHandle = FindPrevActive(lpTopic, lpSymEnt, lpMsg);
        if (newHandle == 0) {
            /* no symbol found -- error */
            ASSERT_ERROR;
        } else {
            /* update handle for last symbol */
            lpMsg->mmLastSym = newHandle;
            /* indicate message changed */
            msgChanged = TRUE;
            /* indicate block size changed */
            blockSizeChanged = TRUE;
        }
    }

    /* check whether message has been changed */
    if (msgChanged) {
        if (blockSizeChanged) {
            /* set pointers, start and end addresses */
            firstIdx = lpMsg->mmFirstSym;
            lastIdx  = lpMsg->mmLastSym;
            lpMsg->mmStartAddr = lpSymTab[firstIdx-SYM_OFFSET].msAddr1;
            lpMsg->mmEndAddr   = lpSymTab[lastIdx -SYM_OFFSET].msAddr1;
            lpMsg->mmCount = (WORD)(lpMsg->mmEndAddr - lpMsg->mmStartAddr + 1);
        }
        /***************************************************************\
            Build the correct new message here.
        \***************************************************************/
        plcAddr = lpTopic->statTopic;
		msgSize = BldRead(plcAddr,
						  &lpData,
                          plcDataType, DDEType,
                          lpMsg->mmStartAddr, lpMsg->mmCount);
        if (msgSize != 0) {
            /* copy modified message to message structure */
            lpMsg->mmSize = msgSize;
            _fmemcpy(lpMsg->mmData, lpData, msgSize);
            /* indicate message has changed */
            lpMsg->mmChanged = TRUE;
        } else {
            /* unable to build message -- error */
            ASSERT_ERROR;
        }
    }

    /* indicate success or failure */
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -