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

📄 udmain.c

📁 工业组态软件modbus驱动源代码, 包括帮助文件.共享.
💻 C
📖 第 1 页 / 共 4 页
字号:
        return (FALSE);
    }

    /* indicate success */
    return (TRUE);
} /* ProtInit */

/***********************************************************************/
/** Close protocol.
    Called when the driver is shut down either individually or as the
    result of exiting Windows.  Should cleanup memory allocations and
    MUST close any comm ports that are open. **/

VOID
WINAPI
ProtClose(void)
{
#ifdef DEBUG_CALL_TRAFFIC
    if (Verbose)
        debug("ProtClose()");
#endif

    /* deactivate and delete all ports in the list */
    DeleteChain (&PortList, DeletePortItem);

    /* save size, position of window for next time server is run */
    WriteWindowSizeToWinIni(hWndParent);

    /* release heap */
    wwHeap_Release( hHeap );
} /* ProtClose */

/***********************************************************************/
/** Process Windows commands and messages **/

long
WINAPI
ProtDefWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT     ps;
    HDC             hDC;
    HBRUSH          hbr;
    HBRUSH          hbrOld;
    LPPORT          lpPort;
    CHAINSCANNER    port_scanner;

    /* handle Windows message according to type */
    switch (message) {

    case WM_SYSCOMMAND:
        /* handle Windows system command according to type */
        switch (wParam) {
        case MENU_SUSPEND:
            /* suspend operation of protocol */
            UdprotSuspend();
            break;

        case MENU_RESUME:
            /* resume operation of protocol */
            UdprotResume();
            break;

        case MENU_SIMULATOR_WRITE:
            /* toggle enablement flag for PLC simulator */
            SimulatorWritePaused = !SimulatorWritePaused;
            CheckMenuItem(GetSystemMenu(hWndParent, FALSE),
                      MENU_SIMULATOR_WRITE,
                      (UINT) (SimulatorWritePaused ? MF_CHECKED : MF_UNCHECKED));
            break;

        case MENU_SIMULATOR_READ:
            /* toggle enablement flag for PLC simulator */
            SimulatorReadPaused = !SimulatorReadPaused;
            CheckMenuItem(GetSystemMenu(hWndParent, FALSE),
                      MENU_SIMULATOR_READ,
                      (UINT) (SimulatorReadPaused ? MF_CHECKED : MF_UNCHECKED));
            break;

        case MENU_VERBOSE:
            /* toggle Verbose flag for logger */
            Verbose = !Verbose;
            CheckMenuItem(GetSystemMenu(hWndParent, FALSE),
                      MENU_VERBOSE,
                      (UINT) (Verbose ? MF_CHECKED : MF_UNCHECKED));
            break;

        case MENU_SHOWEVENTS:
            /* toggle showing events flag for logger */
            ShowingEvents = !ShowingEvents;
            CheckMenuItem(GetSystemMenu(hWndParent, FALSE),
                      MENU_SHOWEVENTS,
                      (UINT) (ShowingEvents ? MF_CHECKED : MF_UNCHECKED));
            break;

        case MENU_SHOWSEND:
            /* toggle whether logger shows messages being sent */
            ShowingSend = !ShowingSend;
            CheckMenuItem(GetSystemMenu(hWndParent, FALSE),
                      MENU_SHOWSEND,
                      (UINT) (ShowingSend ? MF_CHECKED : MF_UNCHECKED));
            break;

        case MENU_SHOWRECEIVE:
            /* toggle whether logger shows messages being received */
            ShowingReceive = !ShowingReceive;
            CheckMenuItem(GetSystemMenu(hWndParent, FALSE),
                      MENU_SHOWRECEIVE,
                      (UINT) (ShowingReceive ? MF_CHECKED : MF_UNCHECKED));
            break;

        case MENU_SHOWERROR:
            /* toggle whether logger shows errors */
            ShowingErrors = !ShowingErrors;
            CheckMenuItem(GetSystemMenu(hWndParent, FALSE),
                      MENU_SHOWERROR,
                      (UINT) (ShowingErrors ? MF_CHECKED : MF_UNCHECKED));
            break;

        case MENU_DUMP:
            /* dump information to logger about each port */
            lpPort = (LPPORT) FindFirstItem (&PortList, SCAN_FROM_HEAD,
                                             NULL, NULL, &port_scanner);
            while (lpPort != (LPPORT) NULL) {
                UdprotDumpPort(lpPort);
                lpPort = (LPPORT) FindNextItem (&port_scanner);
            }
            break;

        case MENU_DUMP_SCREEN:
            /* force update of client display */
            DumpScreen();
            break;

        default:
            /* pass command on for processing */
            return (DefWindowProc(hWnd, message, wParam, lParam));
        }
        break;

    case WM_COMMAND:
        /* handle windows command according to type */
        switch (LOWORD(wParam)) {

        case MENU_HELP_INDEX:
            /* provide help index */
            break;

        case MENU_HELP_ON_HELP:
            /* provide help on help */
            break;

        case MENU_HELP_ABOUT:
            /* display About screen */
            UdDisplayAbout( hWnd );
            break;

        case MENU_CONFIG_PORT:
            /* configure serial ComPort I/O channel */
            ConfigureComPort(hWnd);
            break;

        case MENU_CONFIG_BOARD:
            /* configure adapter board I/O channel */
            ConfigureBoard(hWnd);
            break;

        case MENU_CONFIG_TOPIC:
            /* configure topic */
            ConfigureTopic(hWnd);
            break;

        case MENU_CONFIG_SERVER:
            /* configure general server settings */
            ServerSettings (hWnd);
            break;
        }
        break;

    case WM_USER + 10:
        if (!ProtocolSuspended)
        {
            /* ensure timer count is updated */
            tickChange = 1L;
            lastTimer = GetCurrentTime ();
            /* scan through ports, handle protocol */
            lpPort = (LPPORT) FindFirstItem (&PortList, SCAN_FROM_HEAD,
                                             NULL, NULL, &port_scanner);
            while (lpPort != (LPPORT) NULL) {
                UdprotDoProtocol(lpPort);
                lpPort = (LPPORT) FindNextItem (&port_scanner);
            }
        }
        break;

    case WM_PAINT:
        /* paint the window */
        /* begin paint process, get device context */
        BeginPaint(hWnd, (LPPAINTSTRUCT) &ps);
        hDC = ps.hdc;
        /* check erase flag */
        if (ps.fErase) {
            /* clear window before repainting */
            hbr = CreateSolidBrush(0x00FFFFFFL /* white */ );
            UnrealizeObject(hbr);
            hbrOld = (HBRUSH) SelectObject(hDC, (HANDLE) hbr);
            FillRect(hDC, &ps.rcPaint, hbr);
            SelectObject(hDC, (HANDLE) hbrOld);
            DeleteObject((HANDLE) hbr);
        }

        /*
         * Use the client area to display information about the state
         * of the protocol.
         */

        /* initialize line count */
        yDebug = 0;
        /* dump information to screen about each port */
        lpPort = (LPPORT) FindFirstItem (&PortList, SCAN_FROM_HEAD,
                                         NULL, NULL, &port_scanner);
        while (lpPort != (LPPORT) NULL) {
            XUdprotDumpPort(hDC, lpPort);
            lpPort = (LPPORT) FindNextItem (&port_scanner);
        }
        /* terminate paint process */
        EndPaint(hWnd, (LPPAINTSTRUCT) &ps);
        break;

    default:
        /* pass message on for processing */
        return (DefWindowProc(hWnd, message, wParam, lParam));
    }

    return (0L);
} /* ProtDefWindowProc */

/***********************************************************************/
/** Return the setting for valid data timeout **/

DWORD
WINAPI
ProtGetValidDataTimeout(void)
{
#ifdef DEBUG_CALL_TRAFFIC
    if (Verbose)
        debug("ProtGetValidDataTimeout()");
#endif
    return (dwValidDataTimeout);
} /* ProtGetValidDataTimeout */

/***********************************************************************/
/** Copy name of driver to string at indicated location **/

BOOL
WINAPI
ProtGetDriverName(LPSTR lpszName,
                  int   nMaxLength)
{
    /*
     * WARNING: No calls to debug()... debug() calls ProtGetDriverName(),
     * therefore ProtGetDriverName() cannot call debug().
     */
    int  len;
    PSTR pStr;

    WWSetAffinityToFirstCPU(); // log server to the first CPU

    if (nMaxLength > 0) {
        /* get string and length */
        pStr = GetString(STRUSER + 76);   /* "UDSAMPLE" */
        len = strlen(pStr);
        /* ensure string will fit in destination buffer */
        if (len > nMaxLength-1)
            len = nMaxLength-1;
        /* copy string to destination, terminate it */
        f_lstrncpy(lpszName, pStr, len);
        lpszName[len] = '\0';
    }
#ifdef WIN32
    GetServerNameExtension();
#endif
    return (TRUE);
} /* ProtGetDriverName */

/***********************************************************************/
/** Process timer event.
    Perform the tasks necessary to obtain the requested data.
    This function is called periodically to give the driver the
    opportunity to execute the protocol.
    The period is set up in ProtInit() above. **/

VOID
WINAPI
ProtTimerEvent(DWORD dwMsecSinceLastCall)
{
    LPPORT          lpPort;
    CHAINSCANNER    port_scanner;

    /* Due to the nature of Windows, the actual time between calls to
     * this function may vary somewhat.
     * tickChange indicates the actual time since the last time.
     */

    tickChange = (WORD) dwMsecSinceLastCall;
    lastTimer = GetCurrentTime ();

    /*
     * Being able to suspend the protocol is not mandatory, but it may be
     * helpful during debugging.
     */

    /* nothing to do if protocol is suspended */
    if (!ProtocolSuspended) {

#ifdef DEBUG
        if (Verbose)
        {
            hDC = GetDC (hWndParent);
            TextOut (hDC, 4, 0, "\002", 1);
            ReleaseDC (hWndParent, hDC);
        }
#endif

        /* scan through ports, handle protocol */
        lpPort = (LPPORT) FindFirstItem (&PortList, SCAN_FROM_HEAD,
                                         NULL, NULL, &port_scanner);
        while (lpPort != (LPPORT) NULL) {
            UdprotDoProtocol(lpPort);
            lpPort = (LPPORT) FindNextItem (&port_scanner);
        }

#ifdef DEBUG
        if (Verbose)
        {
            hDC = GetDC (hWndParent);
            TextOut (hDC, 4, 0, " ", 1);
            ReleaseDC (hWndParent, hDC);
        }
#endif

    }
} /* ProtTimerEvent */

/***********************************************************************/
/** Allocate logical device.

⌨️ 快捷键说明

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