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

📄 udmain.c

📁 工业组态软件modbus驱动源代码, 包括帮助文件.共享.
💻 C
📖 第 1 页 / 共 4 页
字号:
    This function is called when a Topic is being activated.
    If the topic name is valid, a data structure representing
    the topic will be constructed. **/

HLOGDEV
WINAPI
ProtAllocateLogicalDevice(LPSTR lpszTopic, IDLDEV idLogDev)
{
    LPSTNPARAM      lpStnParam;
    LPPORT          lpPort;
    LPSTAT          lpTopic;
    HLOGDEV         hLogDev;

    /* initialize return value */
    hLogDev = (HLOGDEV) NULL;
    /* attempt to allocate structure and lock it */
    lpStnParam = (LPSTNPARAM) wwHeap_AllocPtr(hHeap,
                                              GMEM_MOVEABLE | GMEM_ZEROINIT,
                                              (DWORD) sizeof(STNPARAM));
    if (lpStnParam == (LPSTNPARAM) NULL) {
        /* unable to allocate or lock structure, indicate error */
        MessageBox(GetFocus(),
               GetString(STRUSER + 77)  /* "No Memory for Topic Info" */ ,
               GetAppName(),
               MB_ICONEXCLAMATION | MB_OK);
        return (HLOGDEV) NULL;
    }
    /*
     * ValidateTopic() will check the topic name for validity and if OK
     * will return the pertinent information about the topic in the data
     * structure *lpStnParam.
     */

    if (ValidateTopic(lpszTopic, lpStnParam)) {

        /* Node (Topic) name is ok, param block has been filled-in */

        /* attempt to find an available port structure */
        lpPort = UdprotFindPort(lpStnParam);
        if (lpPort == (LPPORT) NULL) {
            /* Port doesn't already exist, so build one */
            lpPort = UdprotSetupPort(lpStnParam);
        }

        if (lpPort != (LPPORT) NULL) {
            /* port structure available, set up PORT and TOPIC */

            /* Set up some fields in the PORT data structure */
            lpPort->mbReplyTime = lpStnParam->spReplyTimeout * 1000;

            /* Create or initialize the TOPIC data structure */
            lpTopic = UdprotSetupTopic(lpPort, lpStnParam, idLogDev, lpszTopic);
            if (lpTopic != (LPSTAT) NULL) {
                /* This will be the handle returned for future use */
                hLogDev = (HLOGDEV) lpTopic;
            }
        }
        /** At this point, if the port wasn't found to exist already and
            it wasn't created by the preceeding logic, we will fail
            by virtue of the fact that hLogDev is NULL. **/
    }
    /* unlock and free the memory we used */
    wwHeap_FreePtr( hHeap, lpStnParam);
    /* update client display */
    DumpScreen();

#ifdef DEBUG_CALL_TRAFFIC
    if (Verbose)
        debug("ProtAllocateLogicalDevice( \"%Fs\", %04X ) => %8lX",
              lpszTopic, idLogDev, hLogDev);
#endif

    /* if successful, increment the count of allocated devices */
    if (hLogDev != (HLOGDEV) NULL) {
        ++iAllocatedDevices;
    }
    /* return handle to device, if any */
    return hLogDev;
} /* ProtAllocateLogicalDevice */

/***********************************************************************/
/** Free logical device.
    Called when no active points remain on a topic.
    The data structures associated with the topic should be deleted. **/

BOOL
WINAPI
ProtFreeLogicalDevice(HLOGDEV hLogDev)
{
    LPPORT          lpPort;

#ifdef DEBUG_CALL_TRAFFIC
    if (Verbose)
        debug("ProtFreeLogicalDevice( %8lX ) => TRUE", hLogDev);
#endif

    /* unchain this station, get handle to port */
    lpPort = UdprotUnchainTopic((LPSTAT) hLogDev);

    /* attempt to free the station */
    if (UdprotFreeTopic((LPSTAT) hLogDev) != (LPSTAT) NULL) {
        ASSERT_ERROR;
    }
    /* decrement the count of allocated devices */
    --iAllocatedDevices;

    /* free the port associated with this device */
    if (lpPort != (LPPORT) NULL) {
        if (lpPort->mbTopicList.first_item.ptr == NULL) {
            lpPort = UdprotUnchainPort(lpPort);
            if (lpPort != (LPPORT) NULL) {
                UdprotFreePort(lpPort);
            }
        }
    }
    /* update client display */
    DumpScreen();
    /* indicate success */
    return (TRUE);
} /* ProtFreeLogicalDevice */

/***********************************************************************/
/** Create a point
    Called when a new point name is to be added
    to the collection of points being handled on a topic **/

HPROT
WINAPI
ProtCreatePoint(HLOGDEV hLogDev,  /* Topic of item */
                HDB hDb,          /* Handle to use later */
                LPSTR lpszName,   /* Item name */
                LPPTTYP lpPtType) /* Pointer to item type return variable */
{
    unsigned long   SymHandle;
    LPSTAT          lpTopic;
    PPS             pps;

    /* indicate error if node or DB handle is NULL */
    assert(hLogDev);
    assert(hDb);

    /* get station structure */
    lpTopic = (LPSTAT) hLogDev;
    if (lpTopic == (LPSTAT) NULL) {
        /* unable to access structure, indicate error */
        return (HPROT) NULL;
    }

    /*
     * Check first for the reserved point name "STATUS" which
     * represents the health of the node.  It will be handled specially.
     * This item indicates TRUE when the protocol is successfully
     * gathering data for this topic.  An error condition that cannot
     * be recovered and affects all points for this logical device
     * should result in a FALSE status being sent to the Toolkit.
     * The client can use this to show an alarm condition.  In this
     * example an HPROT of 0xFFFF will indicate the status item.
     */

    if (lstrcmpi("STATUS", lpszName) == 0) {
        /* STATUS, save DB handle, return STATUS point type and handle */
        lpTopic->statHdbStatus = hDb;
        *lpPtType = PTT_DISCRETE;   /* Return the point type */
        return (HPROT_STATUS);      /* HPROT reserved for status */
    }

    /*
     * Validate point will check the point name.  If OK, it will return
     * all of the pertinent information about the point the the data
     * structure *pps.
     */

    if (!ValidatePoint(lpszName, (LPPPS) &pps)) {
        /*
         * Point name not valid.
         * Return NULL as an error indication.
         */

#ifdef DEBUG_CALL_TRAFFIC
        if (Verbose)
            debug("ProtCreatePoint( %8lX, %8lX, \"%Fs\" ) => NULL (invalid point name)",
                  hLogDev, hDb, lpszName);
#endif
        return (HPROT) NULL;
    }

    /** Point name is valid.  Set up a symbol table entry for the point.
        Symbol handle is symbol table index + SYM_OFFSET. **/
    SymHandle = UdprotAddSymbol(lpTopic, (LPPPS) &pps, hDb);
    if (SymHandle == 0) {
        /*
         * Couldn't set up a symbol table entry.
         * Return NULL as an error indicator.
         */
        return (HPROT) NULL;
    }

    /* Success setting up the point's symbol table entry. */
    *lpPtType = pps.ppsDdeType;       /* Return the point type */

#ifdef DEBUG_CALL_TRAFFIC
    if (Verbose)
        debug("ProtCreatePoint( %8lX, %8lX, \"%Fs\" ) => %04X (type: %d)",
              hLogDev, hDb, lpszName, SymHandle, *lpPtType);
#endif

    /* return handle based on symbol table index */
    return (SymHandle);  /* Return hProt (index+SYM_OFFSET) for later use */
} /* ProtCreatePoint */

/***********************************************************************/
/** Activate a point.
    Called when a point must be added to the g roup of points being
    polled regularly.  All of the necessary information has already
    been set up in the symbol table entry identified by hProt.
    The topic is identified by hLogDev. **/

BOOL
WINAPI
ProtActivatePoint(HLOGDEV hLogDev,
                  HPROT   hProt)
{
    BOOL            success = TRUE;
    LPSTAT          lpTopic;
    SYMPTR          lpSymEnt;
    unsigned long   SymIndex;

    /* indicate error if either logical device or handle is NULL */
    assert(hLogDev);
    assert(hProt);

    /* get pointer to the station structure */
    lpTopic = (LPSTAT) hLogDev;
    if (lpTopic == (LPSTAT) NULL) {
        /* unable to access structure, indicate error */
        return FALSE;
    }

    /*
     * Check first whether this is the "STATUS" point.
     * It is associated with the reserved hProt of HPROT_STATUS
     */

    if (hProt == HPROT_STATUS) {
       /* STATUS point */
       if (lpTopic->statHdbStatus == (HDB)NULL) {
            /* DB handle not set, cannot poll the point */
            success = FALSE;
        } else {
            /* DB handle set, indicate point active and due for update */
            lpTopic->statStatusActive = TRUE;
            lpTopic->statStatusDue = TRUE;
            success = TRUE;
        }
        return success;
    }

    /* not STATUS, check for valid handle */
    if (hProt < SYM_OFFSET) {
        /* invalid handle, indicate error */
        return FALSE;
    }

    /* get symbol table index based on handle */
    SymIndex = (unsigned long) (hProt - SYM_OFFSET);

    /* get pointer to point's symbol table entry */
    lpSymEnt = (SYMPTR) GetExtArrayMemberPtr (&lpTopic->statSymTab, SymIndex);
    if (lpSymEnt == (SYMPTR) NULL) {
        /* indicate error if no symbol table or out of range */
        success = FALSE;
    } else {
        /* check whether point is already active */
        if (!lpSymEnt->msActive) {
            /* not active, prepare to activate point */
            /*
             * Add the point to an existing poll message if possible.
             * If not possible, build another poll message.
             */
            success = UdprotAddPoll(lpTopic, lpSymEnt);
            if (success) {
                /* set point active status in symbol table */
                lpSymEnt->msActive = TRUE;
            }
        }
    }

    /* update screen */
    DumpScreen();

#ifdef DEBUG_CALL_TRAFFIC
    if (Verbose)
        debug("ProtActivatePoint( %8lX, %8lX ) => %d",
              hLogDev, hProt, (int) success);
#endif
    /* return TRUE if successful */
    return (success);
} /* ProtActivatePoint */

/***********************************************************************/
/** Send a new value to the device.
    Called when a new value must be written
    via the protocol to the point. **/

BOOL
WINAPI
ProtNewValueForDevice(HLOGDEV hLogDev,  /* Identifies the topic   */
                      HPROT hProt,      /* Identifies which item  */
                      PTVALUE ptValue)  /* New value for the item */
{
    unsigned long   SymIndex;
    LPSTAT          lpTopic;
    SYMPTR          lpSymEnt;
    BOOL            Ok = FALSE;

    /* indicate error if either logical device or handle is NULL */
    assert(hLogDev);
    assert(hProt);

    /* First check whether this is the special STATUS point */
    if (hProt == HPROT_STATUS) {
        /* STATUS point -- value is read only */
        return FALSE;
    }

    /* not STATUS, get pointer to station data structure */
    lpTopic = (LPSTAT) hLogDev;
    if (lpTopic == (LPSTAT) NULL) {
        /* unable to access structure, indicate error */
        return FALSE;
    }

    /* check for valid handle */
    if (hProt < SYM_OFFSET) {
        /* invalid handle, indicate error */
        return FALSE;
    }

    /* get symbol table index based on handle */
    SymIndex = (unsigned long) (hProt - SYM_OFFSET);

    /* get pointer to point's symbol table entry */
    lpSymEnt = (SYMPTR) GetExtArrayMemberPtr (&lpTopic->statSymTab, SymIndex);
    if (lpSymEnt == (SYMPTR) NULL) {
        /* indicate error if no symbol table or out of range */

⌨️ 快捷键说明

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