📄 read_conf.c
字号:
/*---------------------------------------------------------------------------*/
/*
* Modbus protocol program
*
*
/*---------------------------------------------------------------------------*/
#include "read_conf.h"
char Attribute_Table[ATTR_NUM][30] ={
"Port", ///0
"Mode", ///1
"BaudRate", ///2
"FlowControl", ///3
"Parity", ///4
"DataBit", ///5
"StopBit", ///6
"ProtocolId", ///7
"RtuTable", ///8
"Modbus_tcp_connect_number", ///9
"Allow_Function_Code", ///10
"[Main:]", ///11
"[PortSetting:]" ///12
};
int ReadLine(char * buf, FILE *fp)
{
char c=0;
int i= 0;
while ((!feof(fp)) && ((c = fgetc(fp))!=0x0a)&& c!=0x0d)
{
buf[i] = c;
buf[i+1] = 0;
i++;
}
if ((c==0x0d)&&(i==0))
i=1;
return i;
}
int GetTableInf(int * table, char *inf_buf)
{
char temp[10];
int i,j,k;
int value;
int begin_id,end_id;
int error_count=0;
int flag = 0;
int buf_len=strlen(inf_buf);
j=0;
while (inf_buf[j]!='{')
j++;
while(inf_buf[j]=='{'|| inf_buf[j]==0x20 || inf_buf[j]==0x09)
j++;
k=0;
while (1)
{
i=0;
memset((void *)temp, 0, 10);
while(inf_buf[j]!=',' && inf_buf[j]!=0x20 && inf_buf[j]!='-' && inf_buf[j]!='}')
{
temp[i] = inf_buf[j];
i++;
j++;
}
if (temp[0]!=0)
{
error_count = 0;
sscanf(temp, "%d", &value);
if (value <= 0)
continue;
if (value < 255)
{
if (inf_buf[j]==',' || inf_buf[j]==' ' || inf_buf[j]=='}')
{
if (flag==0 && k<DEV_NUM)
{
table[k] = value;
k++;
j++;
if (j>=buf_len)
{
return OK;
}
}
else
{
flag =0;
end_id = value;
if (end_id<begin_id)
{
printf("Get Table information Error\n");
return ERROR;
}
for(i =0; ((begin_id+i)<=end_id) && k<DEV_NUM; i++,k++)
{
table[k]=begin_id+i;
}
}
}
if (inf_buf[j]=='-')
{
if (flag == 1)
{
printf("Get Table information Error\n");
return ERROR;
}
flag = 1;
begin_id = value;
j++;
}
if (inf_buf[j]=='}')
{
return OK;
}
}
}
else
{
if (error_count == 5)
{
printf("Get Table information Error\n");
return ERROR;
}
error_count++;
if (inf_buf[j]=='}')
return OK;
}
}
}
int FixAttrInf(AttrInf * ai, char * linebuf)
{
char temp[30];
int value;
int i,j,k=0;
j=0;
i = 0;
memset((void *)temp, 0, 30);
while(linebuf[j] == 0x09 || linebuf[j]==0x20)
j++;
while (linebuf[j] != 0x09 && linebuf[j] != 0 && linebuf[j]!=0x20 && linebuf[j]!='=')
{
temp[i] = linebuf[j];
i++;
j++;
}
for (k=0; k<ATTR_NUM; k++)
{
if (strcmp(temp,Attribute_Table[k])==0)
{
ai->attribute_id = k;
if (k==11 || k==12)
{
ai->value=0;
return OK;
}
memset((void *)temp, 0, 30);
while(linebuf[j] == 0x09 || linebuf[j]==0x20 || linebuf[j] == '=' || linebuf[j] == ':')
j++;
i=0;
if (k!=8 || k!=10)
{
while (linebuf[j] != 0x09 && linebuf[j] != 0 && linebuf[j]!=0x20 && linebuf[j]!='=')
{
temp[i] = linebuf[j];
i++;
j++;
}
sscanf(temp,"%d",&ai->value);
}
else
{
ai->value = j;
}
return OK;
}
}
return ERROR;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -