forked from xuos/xiuos
support json file parse for plc socket demo
This commit is contained in:
parent
4843ff9e61
commit
be61178e44
|
@ -224,6 +224,46 @@ void PlcShowUsage(void)
|
||||||
plc_notice("------------------------------------\n");
|
plc_notice("------------------------------------\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PlcGetParamFromFile(void)
|
||||||
|
{
|
||||||
|
PlcSocketParamType *param = &plc_socket_demo_data;
|
||||||
|
|
||||||
|
//for PLC socket parameter file
|
||||||
|
char file_buf[CTL_FILE_SIZE] = {0};
|
||||||
|
FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME);
|
||||||
|
|
||||||
|
if(fd == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
memset(file_buf, 0, CTL_FILE_SIZE);
|
||||||
|
|
||||||
|
CtlFileRead(fd, CTL_FILE_SIZE, file_buf);
|
||||||
|
CtlFileClose(fd);
|
||||||
|
CtlParseJsonData(file_buf);
|
||||||
|
|
||||||
|
memcpy(param->ip, ctl_file_param.ip, 4);
|
||||||
|
param->port = ctl_file_param.port;
|
||||||
|
param->cmd_num = ctl_file_param.cmd_num;
|
||||||
|
|
||||||
|
for(int i = 0; i < param->cmd_num; i++)
|
||||||
|
{
|
||||||
|
TestPlcCmd[i].cmd_len = ctl_file_param.cmd_len[i];
|
||||||
|
memcpy(TestPlcCmd[i].cmd, ctl_file_param.cmd[i], TestPlcCmd[i].cmd_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
plc_print("ip: %d.%d.%d.%d\n", param->ip[0], param->ip[1], param->ip[2], param->ip[3]);
|
||||||
|
plc_print("port: %d", param->port);
|
||||||
|
plc_print("cmd number: %d\n", param->cmd_num);
|
||||||
|
|
||||||
|
for(int i = 0; i < param->cmd_num; i++)
|
||||||
|
{
|
||||||
|
plc_print("cmd %d len %d: ", i, TestPlcCmd[i].cmd_len);
|
||||||
|
for(int j = 0; j < TestPlcCmd[i].cmd_len; j++)
|
||||||
|
plc_print("%x ", TestPlcCmd[i].cmd[j]);
|
||||||
|
plc_print("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void PlcCheckParam(int argc, char *argv[])
|
void PlcCheckParam(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -238,6 +278,13 @@ void PlcCheckParam(int argc, char *argv[])
|
||||||
|
|
||||||
plc_print("check %d %s\n", i, str);
|
plc_print("check %d %s\n", i, str);
|
||||||
|
|
||||||
|
if(strcmp(str, "file") == 0)
|
||||||
|
{
|
||||||
|
plc_notice("get parameter file %s\n", PLC_SOCK_FILE_NAME);
|
||||||
|
PlcGetParamFromFile();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(sscanf(str, "ip=%d.%d.%d.%d",
|
if(sscanf(str, "ip=%d.%d.%d.%d",
|
||||||
¶m->ip[0],
|
¶m->ip[0],
|
||||||
¶m->ip[1],
|
¶m->ip[1],
|
||||||
|
@ -302,11 +349,6 @@ void PlcCheckParam(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlcGetParamFromFile(char *file)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlcSocketTask(int argc, char *argv[])
|
void PlcSocketTask(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
@ -323,7 +365,6 @@ void PlcSocketTask(int argc, char *argv[])
|
||||||
PrivTaskCreate(&th_id, &attr, PlcSocketStart, param);
|
PrivTaskCreate(&th_id, &attr, PlcSocketStart, param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||||
PlcSocket, PlcSocketTask, Test PLC Socket);
|
PlcSocket, PlcSocketTask, Test PLC Socket);
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ typedef struct PlcSocketParamStruct{
|
||||||
}PlcSocketParamType;
|
}PlcSocketParamType;
|
||||||
|
|
||||||
//debug command
|
//debug command
|
||||||
#define plc_print //KPrintf
|
#define plc_print KPrintf
|
||||||
#define plc_error KPrintf
|
#define plc_error KPrintf
|
||||||
#define plc_notice KPrintf
|
#define plc_notice KPrintf
|
||||||
|
|
||||||
|
|
|
@ -23,36 +23,35 @@
|
||||||
#include "transform.h"
|
#include "transform.h"
|
||||||
#include "control_file.h"
|
#include "control_file.h"
|
||||||
|
|
||||||
#define CTL_FILE_NAME "/plc/control_cmd.txt"
|
//json file parameter for PLC socket communication as below:
|
||||||
|
//{
|
||||||
#define TEST_PLC_CMD_TXT \
|
// "ip": "192.168.250.6",
|
||||||
"cmd=03,00,00,16,11,E0,00,00,02,C8,00,C1,02,02,01,C2,02,02,01,C0,01,0A\r\n\r\n" \
|
// "port": 102,
|
||||||
"cmd=03,00,00,19,02,F0,80,32,01,00,00,00,0D,00,08,00,00,F0,00,00,01,00,01,00,F0\r\n\r\n" \
|
// "cmd": [3, 0, 0, 22, 17, 224, 0, 0, 2, 200, 0, 193, 2, 2, 1, 194, 2, 2, 1, 192, 1, 10],
|
||||||
"cmd=03,00,00,1F,02,F0,80,32,01,00,00,33,01,00,0E,00,00,04,01,12,0A,10,02,00,D2,00,34,84,00,00,00\r\n\r\n" \
|
// "cmd1": [3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 0, 13, 0, 8, 0, 0, 240, 0, 0, 1, 0, 1, 0, 240], \r\n" \
|
||||||
"port=102\r\n" \
|
// "cmd2": [3, 0, 0, 31, 2, 240, 128, 50, 1, 0, 0, 51, 1, 0, 14, 0, 0, 4, 1, 18, 10, 16, 2, 0, 210, 0, 52, 132, 0, 0, 0]\r\n" \
|
||||||
"ip=192.168.250.6\r\n"
|
//}"
|
||||||
|
|
||||||
|
|
||||||
#define TEST_PLC_JSON_TXT \
|
#define TEST_PLC_JSON_TXT \
|
||||||
"{ \r\n"\
|
"{ \r\n"\
|
||||||
" \"S1200\": [ \r\n"\
|
" \"ip\": \"192.168.250.6\", \r\n"\
|
||||||
" { \"ip\": \"192.168.250.6\"}, \r\n"\
|
" \"port\": 102, \r\n"\
|
||||||
" { \"port\": 102}, \r\n"\
|
" \"cmd\": [3, 0, 0, 22, 17, 224, 0, 0, 2, 200, 0, 193, 2, 2, 1, 194, 2, 2, 1, 192, 1, 10], \r\n"\
|
||||||
" { \"cmd\": [3, 0, 0, 22, 17, 224, 0, 0, 2, 200, 0, 193, 2, 2, 1, 194, 2, 2, 1, 192, 1, 10]}\r\n"\
|
" \"cmd1\": [3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 0, 13, 0, 8, 0, 0, 240, 0, 0, 1, 0, 1, 0, 240], \r\n" \
|
||||||
" ] \r\n"\
|
" \"cmd2\": [3, 0, 0, 31, 2, 240, 128, 50, 1, 0, 0, 51, 1, 0, 14, 0, 0, 4, 1, 18, 10, 16, 2, 0, 210, 0, 52, 132, 0, 0, 0]\r\n" \
|
||||||
"}"
|
"}"
|
||||||
//" { \"cmd\": [3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 0, 13, 0, 8, 0, 0, 240, 0, 0, 1, 0, 1, 0, 240]},\r\n" \
|
|
||||||
//" { \"cmd\": [3, 0, 0, 31, 2, 240, 128, 50, 1, 0, 0, 51, 1, 0, 14, 0, 0, 4, 1, 18, 10, 16, 2, 0, 210, 0, 52, 132, 0, 0, 0]}\r\n" \
|
|
||||||
|
|
||||||
|
|
||||||
FILE *CtlFileInit(void)
|
CtlPlcSockParamType ctl_file_param;
|
||||||
|
|
||||||
|
FILE *CtlFileInit(char *file)
|
||||||
{
|
{
|
||||||
FILE *fd = NULL;
|
FILE *fd = NULL;
|
||||||
|
|
||||||
fd = fopen(CTL_FILE_NAME, "a+");
|
fd = fopen(file, "a+");
|
||||||
if(fd == NULL)
|
if(fd == NULL)
|
||||||
{
|
{
|
||||||
ctl_error("open file %s failed\n", CTL_FILE_NAME);
|
ctl_error("open file %s failed\n", file);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
|
@ -65,8 +64,9 @@ void CtlFileClose(FILE *fd)
|
||||||
|
|
||||||
void CtlFileRead(FILE *fd, int size, char *buf)
|
void CtlFileRead(FILE *fd, int size, char *buf)
|
||||||
{
|
{
|
||||||
|
fseek(fd, 0, SEEK_SET);
|
||||||
fread(buf, size, 1, fd);
|
fread(buf, size, 1, fd);
|
||||||
ctl_print("read file %d: %s\n", size, buf);
|
ctl_print("read file %d: %.100s\n", size, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CtlFileWrite(FILE *fd, int size, char *buf)
|
void CtlFileWrite(FILE *fd, int size, char *buf)
|
||||||
|
@ -76,26 +76,52 @@ void CtlFileWrite(FILE *fd, int size, char *buf)
|
||||||
ctl_print("write size %d: %s\n", size, buf);
|
ctl_print("write size %d: %s\n", size, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CtlFileTest(void)
|
void CtlCreateFileTest(void)
|
||||||
{
|
{
|
||||||
FILE *fd = CtlFileInit();
|
FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME);
|
||||||
|
if(fd == NULL)
|
||||||
|
return;
|
||||||
char *file_buf = TEST_PLC_JSON_TXT;
|
char *file_buf = TEST_PLC_JSON_TXT;
|
||||||
CtlFileWrite(fd, strlen(file_buf), file_buf);
|
CtlFileWrite(fd, strlen(file_buf), file_buf);
|
||||||
CtlFileRead(fd, CTL_FILE_SIZE, file_buf);
|
|
||||||
CtlFileClose(fd);
|
CtlFileClose(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||||
CtlTestFile, CtlFileTest, Test control file);
|
CtlCreateFile, CtlCreateFileTest, Test control file);
|
||||||
|
|
||||||
#ifdef LIB_USING_CJSON
|
#ifdef LIB_USING_CJSON
|
||||||
|
|
||||||
|
void CtlParseJsonArray(cJSON *dat, int *cmd_len, char *cmd)
|
||||||
|
{
|
||||||
|
int len, i;
|
||||||
|
if(cJSON_IsArray(dat))
|
||||||
|
{
|
||||||
|
len = cJSON_GetArraySize(dat);
|
||||||
|
ctl_print("json cmd %d\n", len);
|
||||||
|
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
cJSON *cmd_val = cJSON_GetArrayItem(dat, i);
|
||||||
|
if(NULL == cmd_val)
|
||||||
|
continue;
|
||||||
|
ctl_print("0x%x ", cmd_val->valueint);
|
||||||
|
cmd[i] = cmd_val->valueint;
|
||||||
|
}
|
||||||
|
*cmd_len = len;
|
||||||
|
ctl_print("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void CtlParseJsonData(char *buf)
|
void CtlParseJsonData(char *buf)
|
||||||
{
|
{
|
||||||
cJSON *file_dat = NULL;
|
cJSON *file_dat = NULL;
|
||||||
cJSON *ip_dat = NULL;
|
cJSON *ip_dat = NULL;
|
||||||
cJSON *port_dat = NULL;
|
cJSON *port_dat = NULL;
|
||||||
cJSON *cmd_dat = NULL;
|
cJSON *cmd_dat = NULL;
|
||||||
|
int cmd_num = 0;
|
||||||
|
int cmd_index = 1;
|
||||||
|
char cmd_str[10] = {0};
|
||||||
|
CtlPlcSockParamType *file_param = &ctl_file_param;
|
||||||
|
|
||||||
file_dat = cJSON_Parse(buf);
|
file_dat = cJSON_Parse(buf);
|
||||||
if(file_dat == NULL)
|
if(file_dat == NULL)
|
||||||
|
@ -106,17 +132,35 @@ void CtlParseJsonData(char *buf)
|
||||||
|
|
||||||
ip_dat = cJSON_GetObjectItem(file_dat, "ip");
|
ip_dat = cJSON_GetObjectItem(file_dat, "ip");
|
||||||
port_dat = cJSON_GetObjectItem(file_dat, "port");
|
port_dat = cJSON_GetObjectItem(file_dat, "port");
|
||||||
cmd_dat = cJSON_GetObjectItem(file_dat, "cmd");
|
|
||||||
|
|
||||||
ctl_print("ip : %s\n", ip_dat->string);
|
ctl_print(" ip : %s\n", ip_dat->valuestring);
|
||||||
ctl_print("port: %d\n", port_dat->valueint);
|
sscanf(ip_dat->valuestring, "%d.%d.%d.%d", &file_param->ip[0],
|
||||||
ctl_print("cmd : %s\n", cmd_dat->valueint);
|
&file_param->ip[1],
|
||||||
|
&file_param->ip[2],
|
||||||
|
&file_param->ip[3]);
|
||||||
|
|
||||||
|
ctl_print(" port: %s %d\n", ip_dat->string, port_dat->valueint);
|
||||||
|
file_param->port = port_dat->valueint;
|
||||||
|
|
||||||
|
strcpy(cmd_str, "cmd");
|
||||||
|
while(cmd_dat = cJSON_GetObjectItem(file_dat, cmd_str))
|
||||||
|
{
|
||||||
|
CtlParseJsonArray(cmd_dat, &file_param->cmd_len[cmd_index - 1], file_param->cmd[cmd_index - 1]);
|
||||||
|
snprintf(cmd_str, sizeof(cmd_str), "cmd%d", cmd_index++);
|
||||||
|
}
|
||||||
|
file_param->cmd_num = cmd_index - 1;
|
||||||
|
|
||||||
|
cJSON_Delete(file_dat);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CtlParseFileTest(void)
|
void CtlParseFileTest(void)
|
||||||
{
|
{
|
||||||
|
//for PLC socket parameter file
|
||||||
char file_buf[CTL_FILE_SIZE] = {0};
|
char file_buf[CTL_FILE_SIZE] = {0};
|
||||||
FILE *fd = CtlFileInit();
|
FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME);
|
||||||
|
if(fd == NULL)
|
||||||
|
return;
|
||||||
|
memset(file_buf, 0, CTL_FILE_SIZE);
|
||||||
CtlFileRead(fd, CTL_FILE_SIZE, file_buf);
|
CtlFileRead(fd, CTL_FILE_SIZE, file_buf);
|
||||||
CtlFileClose(fd);
|
CtlFileClose(fd);
|
||||||
CtlParseJsonData(file_buf);
|
CtlParseJsonData(file_buf);
|
||||||
|
|
|
@ -21,15 +21,34 @@
|
||||||
#ifndef __CONTROL_FILE_H_
|
#ifndef __CONTROL_FILE_H_
|
||||||
#define __CONTROL_FILE_H_
|
#define __CONTROL_FILE_H_
|
||||||
|
|
||||||
#define CTL_FILE_SIZE 1024
|
#define CTL_FILE_SIZE 1000
|
||||||
|
#define CTL_CMD_NUM 10 // max command number
|
||||||
|
#define CTL_CMD_LEN 100
|
||||||
|
#define CTL_IP_LEN 32
|
||||||
|
|
||||||
|
#define PLC_SOCK_FILE_NAME "/plc/socket_param.json"
|
||||||
|
|
||||||
#define ctl_print KPrintf
|
#define ctl_print KPrintf
|
||||||
#define ctl_error KPrintf
|
#define ctl_error KPrintf
|
||||||
|
|
||||||
FILE *CtlFileInit(void);
|
typedef struct CtlPlcSockParamStruct
|
||||||
|
{
|
||||||
|
char ip[CTL_IP_LEN];
|
||||||
|
int port;
|
||||||
|
int cmd_num; //command number
|
||||||
|
int cmd_len[CTL_CMD_NUM]; // command length
|
||||||
|
char cmd[CTL_CMD_NUM][CTL_CMD_LEN];
|
||||||
|
}CtlPlcSockParamType;
|
||||||
|
|
||||||
|
extern CtlPlcSockParamType ctl_file_param;
|
||||||
|
|
||||||
|
FILE *CtlFileInit(char *file);
|
||||||
void CtlFileClose(FILE *fd);
|
void CtlFileClose(FILE *fd);
|
||||||
void CtlFileRead(FILE *fd, int size, char *buf);
|
void CtlFileRead(FILE *fd, int size, char *buf);
|
||||||
void CtlFileWrite(FILE *fd, int size, char *buf);
|
void CtlFileWrite(FILE *fd, int size, char *buf);
|
||||||
|
|
||||||
|
#ifdef LIB_USING_CJSON
|
||||||
|
void CtlParseJsonData(char *buf);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue