📄 udcfgtxt.c
字号:
/** write COMPORT as ASCII command to configuration file.
returns TRUE if successful **/
/************************************************************\
* Note: To support the custom parameters, you can add fields *
* such as the following: *
* EDIT=< contents of edit field > *
* Radio=< 1, 2, or 3 > *
* Check1=< 1 or 0, or Y or N > *
* Check2=< 1 or 0, or Y or N > *
* reserved=< contents of reserved area > *
* Of course, it will be more meaningful if you give *
* these parameters appropriate names, which will *
* depend upon how your server uses them. *
* Also, be sure to include processing for any *
* additional parameters in ReadPortCfgASCII(). *
* The example code shows how to handle different kinds *
* of parameters, including numbers, strings, and *
* named items (e.g. PARITY=NONE,ODD,EVEN, etc.). *
\************************************************************/
static
BOOL
WINAPI
WritePortCfgASCII (BUFFERED_FILE *DestFile, LPCOMPORT_CFG_WITHID lpComPortCfgWithID)
{
BOOL ok;
unsigned long BaudRate;
int len;
int DataBits, StopBits;
char Parity [20+1];
char st [300+1];
/* convert indexes to values or strings, as is appropriate */
BaudRate = BaudFromIndex (lpComPortCfgWithID->comport.uBaud);
DataBits = DataBitsFromIndex (lpComPortCfgWithID->comport.uDataBits);
StopBits = StopBitsFromIndex (lpComPortCfgWithID->comport.uStopBits);
ParityFromIndex (lpComPortCfgWithID->comport.uParity, Parity);
/* output command for COM port to file */
sprintf (st, "COMPORT PORTID=%u BAUD=%lu DATA_BITS=%u "
"STOP_BITS=%u PARITY=%s REPLYTIMEOUT=%lu;\r\n",
(int) lpComPortCfgWithID->channelID,
(unsigned long) BaudRate, (int) DataBits,
(int) StopBits, Parity,
(unsigned long) lpComPortCfgWithID->comport.uReplyTimeout);
len = strlen (st);
ok = WriteNextBuffer (DestFile, st, len);
/* indicate success or failure */
return (ok);
} /* WritePortCfgASCII */
/********************************************************************/
/** write BOARD_CFG as ASCII command to configuration file.
returns TRUE if successful **/
static
BOOL
WINAPI
WriteBoardCfgASCII (BUFFERED_FILE *DestFile, LPBOARD_CFG lpBoardCfg)
{
BOOL ok;
int len;
char st [300+1];
/* output command for BOARD_CFG to file */
sprintf (st, "BOARD NAME=\"%Fs\" PORTID=%lu MEM_SEGMENT=0x%lX IO_ADDR=0x%lX "
"REPLYTIMEOUT=%lu;\r\n",
(LPSTR) lpBoardCfg->cp_name,
(unsigned long) lpBoardCfg->cp_channelID,
(unsigned long) lpBoardCfg->cp_memSegment,
(unsigned long) lpBoardCfg->cp_ioAddr,
(unsigned long) lpBoardCfg->cp_replyTimeout);
len = strlen (st);
ok = WriteNextBuffer (DestFile, st, len);
/* indicate success or failure */
return (ok);
} /* WriteBoardCfgASCII */
/**************************************************************************/
/** write topic as ASCII command to configuration file.
returns TRUE if successful **/
static
BOOL
WINAPI
WriteTopicCfgASCII (BUFFERED_FILE *DestFile, LPTOPIC_CFG lpTopicCfg)
{
BOOL ok;
int len;
char st [300+1];
/* output TOPIC_CFG to file */
sprintf (st, "TOPIC NAME=\"%Fs\" PORTID=%lu UPDATEINTERVAL=%lu "
"ADDRESS=%lu COIL_READ_SIZE=%lu REG_READ_SIZE=%lu;\r\n",
(LPSTR) lpTopicCfg->tc_name,
(unsigned long) lpTopicCfg->tc_channelID,
(unsigned long) lpTopicCfg->tc_updateInterval,
(unsigned long) lpTopicCfg->tc_topicAddress,
(unsigned long) lpTopicCfg->tc_coilReadSize,
(unsigned long) lpTopicCfg->tc_regReadSize);
len = strlen (st);
ok = WriteNextBuffer (DestFile, st, len);
/* indicate success or failure */
return ok;
} /* WriteTopicCfgASCII */
/**************************************************************************
**************************************************************************
* Routines for reading CFG files in ASCII text *
**************************************************************************
**************************************************************************/
/**************************************************************************/
BOOL
WINAPI
ReadConfigsASCII ( BUFFERED_FILE *SourceFile )
{
BOOL ok;
char ch;
int len;
int i;
int cmd_id;
char *scan_ptr;
char command_name [31];
char scan_buf [300+1];
TOPIC_CFG TopicCfg;
COMPORT_CFG_WITHID ComPortCfgWithID;
BOARD_CFG BoardCfg;
/* initialize return value */
ok = TRUE;
/* process each command in file */
while ((ok) && (SourceFile->more_data) && (SourceFile->error == 0)) {
/* get line of text from file */
ok = ReadNextLine(SourceFile, scan_buf, sizeof(scan_buf));
scan_ptr = scan_buf;
if (ok) {
/* skip blanks, search for start of command */
ch = *scan_ptr;
while ((ch) && (ch == ' '))
ch = *(++scan_ptr);
if (ch) {
/* non-blank found, get command name */
len = 0;
while ((ch) && (ch != ' ')) {
if (len < sizeof(command_name)-1)
command_name[len++] = ch;
ch = *(++scan_ptr);
}
command_name [len] = '\0';
/* identify command */
cmd_id = 0;
i = 0;
while ((cmd_id == 0) && (i < NUM_CONFIG_CMDS)) {
if (stricmp (command_name, command_table[i].cmd_name) == 0)
cmd_id = command_table[i].cmd_index;
i++;
}
/* handle command */
switch (cmd_id) {
case TOPIC_CMD : /* TOPIC */
/* read topic command from file */
ok = ReadTopicCfgASCII (SourceFile, &TopicCfg, scan_ptr,
scan_buf, sizeof(scan_buf));
if( ok ) {
/* validate topic settings, add to list */
ok = StoreNewTopicCfg (&TopicCfg);
}
break;
case COMPORT_CMD : /* COMPORT */
if (bUsesComPorts) {
/* read comport command from file */
ok = ReadPortCfgASCII (SourceFile, &ComPortCfgWithID, scan_ptr,
scan_buf, sizeof(scan_buf));
if( ok ) {
/* validate board settings, add to list */
ok = StoreNewComPortCfg (&ComPortCfgWithID);
}
} else {
ok = FALSE;
}
break;
case BOARD_CMD : /* BOARD */
if (bUsesBoards) {
/* read board command from file */
ok = ReadBoardCfgASCII (SourceFile, &BoardCfg, scan_ptr,
scan_buf, sizeof(scan_buf));
if( ok ) {
/* validate board settings, add to list */
ok = StoreNewBoardCfg (&BoardCfg);
}
} else {
ok = FALSE;
}
break;
default : /* unrecognized command */
/* indicate error */
ok = FALSE;
break;
} /* switch */
}
}
}
/* indicate success or failure */
return (ok);
} /* ReadConfigsASCII */
/**************************************************************************/
static
BOOL
WINAPI
ReadTopicCfgASCII ( BUFFERED_FILE *SourceFile,
LPTOPIC_CFG lpTopicCfg, char *scan_ptr,
char *scan_buf, int scan_buf_size )
{
BOOL ok;
BOOL done;
char ch;
int i;
char param_name [31];
int param_id;
unsigned long new_long;
unsigned long len;
BOOL truncated;
char string_status;
TOPIC_CFG TopicCfg;
/* initialize return value */
ok = TRUE;
/* initialize flag */
done = FALSE;
/* initialize topic structure to default settings */
SetupDefaultTopicCfgParams ( &TopicCfg );
/* get current character */
ch = *scan_ptr;
/* process each parameter in command */
while (ok && !done) {
/* skip blanks, search for start of parameter name */
ch = *scan_ptr;
while ((ch) && (ch == ' '))
ch = *(++scan_ptr);
if (ch) {
/* non-blank found, get parameter name */
GetParamName (&scan_ptr, param_name, sizeof(param_name));
/* identify parameter */
if (stricmp (param_name, ";") == 0) {
/* end of command reached */
done = TRUE;
} else {
param_id = 0;
i = 0;
while ((param_id == 0) && (i < NUM_TOPIC_PARAMS)) {
if (stricmp (param_name, topic_param_table[i].param_name) == 0)
param_id = topic_param_table[i].param_index;
i++;
}
switch (param_id) {
case TOPIC_NAME_PARAM : /* NAME=<string in quotes> */
/* attempt to get string in quotes */
ok = GetDelimitedString (&scan_ptr, &TopicCfg.tc_name[0],
sizeof(TopicCfg.tc_name), &len,
&truncated, &string_status);
if (truncated || (string_status != 'y'))
/* string too long or error encountered */
ok = FALSE;
break;
case TOPIC_PORTID_PARAM : /* PORTID=<number> */
/* accept value only if valid integer and in range */
ok = GetParamNumber (&scan_ptr, &new_long);
if (ok && (1 <= new_long))
TopicCfg.tc_channelID = new_long;
else
ok = FALSE;
break;
case TOPIC_UPDTINT_PARAM : /* UPDATEINTERVAL=<number> */
/* accept value only if valid integer */
ok = GetParamNumber (&scan_ptr, &new_long);
if (ok)
TopicCfg.tc_updateInterval = new_long;
break;
case TOPIC_ADDR_PARAM : /* UPDATEINTERVAL=<number> */
/* accept value only if valid integer and in range */
ok = GetParamNumber (&scan_ptr, &new_long);
if (ok && (0 <= new_long) && (new_long <= 255))
TopicCfg.tc_topicAddress = (BYTE) new_long;
else
ok = FALSE;
break;
case TOPIC_COILREADSIZE_PARAM : /* COIL_READ_SIZE=<number> */
/* accept value only if valid integer and in range */
ok = GetParamNumber (&scan_ptr, &new_long);
if (ok && (0 <= new_long) && (new_long <= 0xFFFF))
TopicCfg.tc_coilReadSize = (WORD) new_long;
else
ok = FALSE;
break;
case TOPIC_REGREADSIZE_PARAM : /* REG_READ_SIZE=<number> */
/* accept value only if valid integer and in range */
ok = GetParamNumber (&scan_ptr, &new_long);
if (ok && (0 <= new_long) && (new_long <= 0xFFFF))
TopicCfg.tc_regReadSize = (WORD) new_long;
else
ok = FALSE;
break;
default : /* unrecognized parameter */
ok = FALSE;
break;
} /* switch */
}
}
/* check for end of line */
if (ok && (!done) && (*scan_ptr == 0)) {
/* get another line of text from file */
if ((SourceFile->more_data) && (SourceFile->error == 0)) {
ok = ReadNextLine(SourceFile, scan_buf, scan_buf_size);
scan_ptr = scan_buf;
}
}
}
/* return configured structure */
if (ok)
*lpTopicCfg = TopicCfg;
/* indicate success or failure */
return (ok);
} /* ReadTopicCfgASCII */
/**************************************************************************/
static
BOOL
WINAPI
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -