问题描述
如上所述,我正在尝试使用JSON有效负载来管理HTTP响应,而没有任何类似curl或其他库的库。 一切都写在Simulink环境中的S函数中,但“主要”是(希望)完美的C。
我的程序:
- 使用带有ASCII响应(标头+ JSON文本)的uint8数组,例如:[72 84 84 80 32 50 48 48 32 79 75 ....] // HTTP 200 OK .... {“ Val”:100 ,“ Val2”:2,....}
- 将此数组复制到char数组“ str”中,并找到第二个“ {”和最后一个“}”,因为还有其他用于cookie的括号。我不在乎“ \ r \ n \ r \ n”,因为它始终是JSON响应。
- 我保存了开始索引和结束索引
- 我创建另一个char数组cut_str,并从头到尾复制str值,并在末尾添加一个'\ 0'。
- 获取cut_str并将其用于tiny-json中定义的json_create函数中。
- 一些其他代码可提取JSON字段的值并...瞧!
InputPtrsType u = ssGetInputPortSignalPtrs(S,0);
InputUInt8PtrsType resp8 = (InputUInt8PtrsType)u; // resp8 is my input uint8 array
real_T *y0 = (real_T *) ssGetoutputPortRealSignal(S,0); // y0 is the output
int j=0;
int h=0;
int start;
int end;
// Cut HTTP headers
char str[3000]; // 3000 is the resp8 size (constant)
for ( int i=0; i<=3000; i++ ){
str[i]=*resp8[i]; // copy values in char str.
if (str[i]== 123) // If str[i]== "{"...
{
j=j+1;
if (j==2)
{
start=i; // At the second "{" in the text will start the JSON payload
}
}
if (str[i]== 125) // If str[i]== "}"...
{
h=h+1;
if (h==2)
{
end=i; // At the second "}" in the text will finish the JSON payload
}
}
}
char cut_str[1000]; // Create a new char to store the just the JSON payload
strncpy(cut_str,str+start,end-start+1); // Can be done also with a for cycle.
cut_str[end-start+1]='\0'; // Null terminated.
// JSON PARSING using tiny-json.h and tiny-json.c
json_t mem[32];
json_t const* json = json_create( cut_str,mem,sizeof mem / sizeof *mem );
json_t const* key = json_getProperty( json,"Val1" );
int const value = (int)json_getInteger( key );
printf( "Val1: %d\n",value );
y0[0]= value;
注意:
- 一切在Simulink离线模拟中均能完美运行。
问题:
- 当我尝试在嵌入式系统中编译整个项目时,会出现一些有关循环的警告,并且可能会用end和start进行初始化
- 如果我尝试创建cut_str [end-start + 1]会给我错误。
- ,更重要的是,内存冲突异常!由于对地址0x00000bb8的非法内存访问,实时应用程序(ID 0x008748b1)崩溃。执行在符号'Application_008748b1.x86@_btext+0x24eb'附近的地址0x0804c3eb处停止,并且应用程序停止了!
问题:
- 我在哪里错?似乎我在做违法的事情...但是为什么在Simulink中可以执行所有操作?
- 我对end,start,str和cut_str有一些疑问...它们应该是指针吗?为什么我不能使用普通数组?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)