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

📄 udconfig.c

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

    if (bUsesBoards) {
        /* initialize the list of boards to empty */
        InitializeChain (&BoardCfgList);
        /* setup default board structure */
        SetupDefaultBoardCfgParams( (LPBOARD_CFG) &DefaultBoardParams );
    }

    /* initialize the list of topics to empty */
    InitializeChain (&TopicCfgList);

    /* get pathname for config file, identify where it came from */
    path_source = GetConfigFilePath (ConfigFile);

    /* construct full path and filename for config file */
    strcat (ConfigFile, configFileName);
    strupr (ConfigFile);

    /* have logger identify config file and path source */
    debug (path_source, ConfigFile);

    /* initialize return value */
    ok = TRUE;

    /* attempt to read the config file */
    AssignBufferedFile (&SourceFile, ConfigFile, (LPSTR) SourceBuf, sizeof(SourceBuf));
    if (OpenForBufferedRead (&SourceFile) == 0) {
        /* attempt to read file version */
        ok = ReadNextVersion (&SourceFile, FMAGIC, &lVersion);
        if( !ok ) {
            debug("File version read error.");
        } else {
            if (lVersion == CFGTXT) {
                /* attempt to read configuration as ASCII commands */
                ok = ReadConfigsASCII (&SourceFile);
            } else {
                /* attempt to read configuration structures from file */
                ok = ReadConfigsStruct (&SourceFile, lVersion);
            }
        }

        /* close the file */
        CloseForBufferedRead (&SourceFile);

        if( !ok )  {
            WWDisplayErrorReading( GetAppName(), (LPSTR)ConfigFile);
        }
    }
    return (ok);
} /* ConfigureInit */

/***********************************************************************
 ***********************************************************************
 * Routines for writing the CFG file                                   *
 ***********************************************************************
 ***********************************************************************/

/***********************************************************************/
/** write the configuration file;
    returns TRUE if successful **/

static
BOOL
WINAPI
ConfigureSave( void )
{
    BOOL            ok;
    BUFFERED_FILE   DestFile;
    char            DestBuf [512];
    char            ConfigFile[PATH_STRING_SIZE];
    char            ConfigTemp[PATH_STRING_SIZE];
    char            ConfigBak[PATH_STRING_SIZE];

    strcpy(ConfigFile, szCfgPath);
    strcat(ConfigFile, configFileName);

    /* check for existence */
    if ( access( ConfigFile, 0 ) != 0 ) {
        /* does not exist -- get file path and name from user */
        WW_CONFIRM  wwConfirm;
        memset( &wwConfirm, 0, sizeof(wwConfirm) );
        wwConfirm.hwndOwner        = WWGetDialogHandle();
        wwConfirm.szCfgPath        = (LPSTR)szCfgPath;
        wwConfirm.iSizeOfszCfgPath = PATH_STRING_SIZE;
        wwConfirm.szDriverName     = GetAppName();
        WWConfirm((LPWW_CONFIRM)&wwConfirm);
        strcpy(ConfigFile, szCfgPath);
        strcat(ConfigFile, configFileName);
    }

    /* get complete path to configuration file */
    strcpy(ConfigTemp, szCfgPath);
    strcat(ConfigTemp, configTempName);
    strcpy(ConfigBak, szCfgPath);
    strcat(ConfigBak, configBackupName);

    /* set up temporary file */
    unlink(ConfigTemp);
    AssignBufferedFile (&DestFile, ConfigTemp, (LPSTR) DestBuf, sizeof(DestBuf));
    ok = (OpenForBufferedWrite (&DestFile) == 0);
    if (ok) {
        if (!WriteConfigInASCII) {
            /* write configuration file as binary structures */
            ok = WriteConfigsStruct (&DestFile);
        } else {
            /* write configuration file as ASCII commands */
            ok = WriteConfigsASCII (&DestFile);
        }

        /* close file */
        if (CloseForBufferedWrite (&DestFile) != 0)
            ok = FALSE;

        if (ok) {
            /* erase backup file, rename old config file as backup */
            unlink(ConfigBak);
            rename(ConfigFile, ConfigBak);
            /* rename temp file as new config file */
            rename(ConfigTemp, ConfigFile);
        } else {
            WWDisplayErrorWriting(GetAppName(), (LPSTR) ConfigTemp);
        }
    } else {
        WWDisplayErrorCreating(GetAppName(), (LPSTR) ConfigTemp);
        ok = FALSE;
    }

    return (ok);
} /* ConfigureSave */

/***********************************************************************/
/** set up file version header and write it to configuration file;
    returns TRUE if successful **/

BOOL
WINAPI
WriteVersion( BUFFERED_FILE *DestFile, long verNum, char pad_val )
{
    /* cannot put time acquisition in DLL because C run time
       library calls to get time do not use far pointers and
       are not suitable for use in a re-entrant DLL */

    struct tm far  *ftime_now;
    struct tm       time_now;
    time_t          secs_now;
    char            szDate[20];
    char            szTime[20];
    BOOL            ok;

    /* get date/time stamp as strings */
    secs_now = time((time_t *)NULL);
    ftime_now = gmtime(&secs_now);
    time_now = *ftime_now;
    strftime(szDate, sizeof szDate, "%m/%d/%y  ", &time_now);
    strftime(szTime, sizeof szTime, "%H:%M:%S  ", &time_now);

    /* put version info in FILEVER structure, attempt to write to disk */
    ok = WriteNextVersion (DestFile, FMAGIC, verNum, (LPSTR)szDate, (LPSTR)szTime, pad_val);

    /* indicate success or failure */
    return (ok);
} /* WriteVersion */

/***********************************************************************/
/* get full path for the configuration file from WIN.INI or command line */

static
LPSTR
WINAPI
GetConfigFilePath(LPSTR ConfigFile)
{
    char            driverName[20];
    extern char     szCmdLine[];
    LPSTR           path_source;

    /* get name of driver application */
    ProtGetDriverName (driverName, sizeof(driverName));

    /* try reading config path name from the command line */
    szCfgPath[0] = 0;
    CheckConfigFileCmdLine (szCmdLine, szCfgPath, PATH_STRING_SIZE);
    if (strlen (szCfgPath) > 0) {
        /* config path is from the command line */
        path_source = (LPSTR) "Using command line config file %s";
    } else {
        /* no command line config path found,
           try reading one from the WIN.INI file */
        GetProfileString(driverName,
                         NAME_PATH,
                         DEFAULT_PATH,
                         szCfgPath,
                         PATH_STRING_SIZE);
        if (strlen (szCfgPath) > 0) {
            /* config path is from WIN.INI */
            path_source = (LPSTR) "Using WIN.INI config file %s";
        } else {
            /* no other config path, use default path to EXE */
            WWGetExeFilePath (szCfgPath, PATH_STRING_SIZE);
            path_source = (LPSTR) "Using Default config file %s";
        }
    }

    /* ensure pathname ends with a backslash */
    if (szCfgPath[strlen (szCfgPath) - 1] != '\\') {
        strcat(szCfgPath, "\\");
    }

    /* copy config path to indicated buffer */
    _fstrcpy (ConfigFile, szCfgPath);

    /* return indication of where path came from */
    return (path_source);
} /* GetConfigFilePath */

/***********************************************************************
 ***********************************************************************
 * Routines for handling General I/O Server Settings Dialog            *
 ***********************************************************************
 ***********************************************************************/

/**************************************************************************/
/* set general I/O server settings */

VOID
WINAPI
ServerSettings (HWND hWnd)
{
    WW_SERV_PARAMS  ServParams;
    char driverName [20];
    char temp_szCfgPath [PATH_STRING_SIZE];
    char temp_szCaption [81];

    /* get name of driver application */
    ProtGetDriverName (driverName, sizeof (driverName));

    /* set up caption for dialog */
    strcpy (temp_szCaption, GetString(STRUSER+138) /* "UDSAMPLE Server Settings" */ );

    /* try reading the configuration path from WIN.INI */
    temp_szCfgPath [0] = 0;
    GetProfileString(driverName,
                     NAME_PATH,
                     DEFAULT_PATH,
                     temp_szCfgPath,
                     PATH_STRING_SIZE);
    if (strlen (temp_szCfgPath) == 0) {
        /* no config path defined, use default path to EXE */
        WWGetExeFilePath (temp_szCfgPath, PATH_STRING_SIZE);
    }

    /* ensure pathname ends with a backslash */
    if (temp_szCfgPath[strlen (temp_szCfgPath) - 1] != '\\') {
        strcat (temp_szCfgPath, "\\");
    }

    /* set up server settings via common dialog */
    memset (&ServParams, 0, sizeof ServParams);
    ServParams.hwndOwner = hWnd;
    ServParams.szCaption = (LPSTR) temp_szCaption;
    ServParams.szCfgPath = (LPSTR) temp_szCfgPath;
    ServParams.iSizeOfszCfgPath = PATH_STRING_SIZE;
    ServParams.szDriverName = GetAppName();
    ServParams.bIndefWriteRetrySupported = FALSE;
    if (iAllocatedDevices) {
        /* protocol running, don't reconfigure now */
        WWDisplayConfigNotAllow (GetAppName ());
        ServParams.bPreventChanges = 1;
    } else {
        /* nothing allocated, go ahead and configure */
        ServParams.bPreventChanges = 0;
    }
    /* configure, or allow user to look at configuration */
    WWConfigureServer((LPWW_SERV_PARAMS) &ServParams);
} /* ServerSettings */

/***********************************************************************
 ***********************************************************************
 * Routines for setting up default values for CFG structures           *
 ***********************************************************************
 ***********************************************************************/

/***********************************************************************/
/** copy default values to the indicated COM port structure **/

void
WINAPI
SetupDefaultPortCfgParams( LPWW_CP_PARAMS lpDefaultCpParams )
{
    _fmemset(lpDefaultCpParams, 0, sizeof(WW_CP_PARAMS));
    lpDefaultCpParams->uBaud      = WWTranslateWinBaudToCDlg(CBR_19200);
    lpDefaultCpParams->uDataBits  = WWTranslateWinDataToCDlg(8);
    lpDefaultCpParams->uStopBits  = WWTranslateWinStopToCDlg(ONESTOPBIT);
    lpDefaultCpParams->uParity    = WWTranslateWinParityToCDlg(ODDPARITY);
    lpDefaultCpParams->uReplyTimeout = 3; /* seconds */

    return;
} /* SetupDefaultPortCfgParams */

/***********************************************************************/
/** copy default values to the indicated BOARD_CFG structure **/

void
WINAPI
SetupDefaultBoardCfgParams( LPBOARD_CFG lpBoardCfg )
{
    _fmemset(lpBoardCfg, 0, sizeof(BOARD_CFG));
    lpBoardCfg->cp_channelID = 0;
    _fstrcpy (lpBoardCfg->cp_name, defBoardName);
    lpBoardCfg->cp_memSegment   = DEFAULT_SEGMENT;
    lpBoardCfg->cp_ioAddr       = DEFAULT_IOBASE;
    lpBoardCfg->cp_replyTimeout = DEFAULT_REPLY_TIMEOUT;
} /* SetupDefaultBoardCfgParams */

/***********************************************************************/
/* store default settings in topic structure */

VOID
WINAPI
SetupDefaultTopicCfgParams (LPTOPIC_CFG lpTopicCfg)
{
    _fmemset (lpTopicCfg, 0, sizeof(TOPIC_CFG));
    lstrcpy(lpTopicCfg->tc_name, defTopicName);
    lpTopicCfg->tc_channelID = dwEditChannelID;
    ClearChainLink (&lpTopicCfg->tc_chainLink);
    lpTopicCfg->tc_topicAddress   = defTopicAddress;
    lpTopicCfg->tc_coilReadSize   = defCoilReadSize;
    lpTopicCfg->tc_regReadSize    = defRegReadSize;
    lpTopicCfg->tc_updateInterval = defUpdateInterval;
} /* SetupDefaultTopicCfgParams */

/***********************************************************************
 ***********************************************************************
 * Routines for managing linked list of TOPIC configuration structures *
 ***********************************************************************
 ***********************************************************************/

/***********************************************************************/
/** Create new TOPIC configuration structure,
    return pointer to structure, if successful **/

LPTOPIC_CFG
WINAPI
TopicCfgAlloc()
{
    LPTOPIC_CFG     lpTopicCfg;

    /* attempt to allocate memory */
    lpTopicCfg = (LPTOPIC_CFG) wwHeap_AllocPtr(hHeap,
                                               GMEM_ZEROINIT | GMEM_MOVEABLE,
                                               (DWORD) sizeof(TOPIC_CFG));
    if (lpTopicCfg != (LPTOPIC_CFG) NULL) {

⌨️ 快捷键说明

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