我有以下代码
char inputs []="3,0,23.30,3,30/55,55,55,55,55,55,55,55,55,55,55,55,55,64,64,64,100,100,100,100,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,55,55,70/1.5,0.5,0.2,0.2,0.3,0.1";
char parameters[18];
strcpy(parameters,strtok(inputs,"/"));
然后一些代码通过uart传输我的字符并在监视器上看到它们。当我传输输入时,我能很好地看到它们,但是当我传输参数时,我什么也看不到,字符串是空的。
我看过strtok的例子,它使用这些代码分割字符串。我在visual studio中也尝试过这种代码,当我打印它们时,它可以很好地显示字符串。strtok有没有可能在微处理器上运行不好????
最佳答案
在使用微控制器时,您必须注意所使用的内存区域。在运行在PC上的软件中,所有的东西都是从RAM中存储和运行的。但是,在flash微控制器上,代码是从flash(也称为程序存储器)运行的,而数据是从RAM(也称为数据存储器)处理的。
在您正在处理的情况下,inputs
变量正在存储一个硬编码字符数组,它可以是const,我们不知道编译器选择将它放在哪个区域。所以,我们可以重写你的小程序,以确保所有的数据都存储在程序数据中,我们将使用“P”函数来操作这些数据。
#include <avr/pgmspace.h > // to play in program space
const char inputs PROGMEM []="3,0,23.30,3,30/55,55,55,55,55,55,55,55,55,55,55,55,55,64,64,64,100,100,100,100,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,55,55,70/1.5,0.5,0.2,0.2,0.3,0.1"; // Now, we are sure this is in program memory space
char buffer[200]; // should be long enough to contain a copy of inputs
char parameters[18];
int length = strlen_P(inputs); // Should contains string length of 186 (just to debug)
strcpy_P(buffer,inputs); // Copy the PROGMEM data in data memory space
strcpy(parameters,strtok_P(buffer,"/")); // Parse the data now in data memory space
有关avr gcc的程序空间的更多信息:http://www.nongnu.org/avr-libc/user-manual/pgmspace.html