Arduino myFile.write(buff, len) 不工作
Arduino myFile.write(buff, len) don't work
我最近在写一些关于获取 IMU 数据并将其保存到 SD 卡的代码。为了提高写入数据的速度,我需要使用二进制格式来保存它,这是我的代码:
byte myData[20];
((long int*)myData)[0] = count;
((long int*)myData)[4] = microSec;
((short int*)myData)[8] = ax;
((short int*)myData)[10] = ay;
((short int*)myData)[12] = az;
((short int*)myData)[14] = gx;
((short int*)myData)[16] = gy;
((short int*)myData)[18] = gz;
myFile3.write(myData, sizeof(myData));
但是完全不行。(但是可以编译,真是迷惑我了。。。)
有人遇到这个问题吗...?
试试这个怎么样:
byte myData[20];
((long int*)myData)[0] = count;
((long int*)myData)[1] = microSec;
((short int*)myData)[4] = ax;
((short int*)myData)[5] = ay;
((short int*)myData)[6] = az;
((short int*)myData)[7] = gx;
((short int*)myData)[8] = gy;
((short int*)myData)[9] = gz;
myFile3.write(myData, sizeof(myData));
谢谢大家!通过数百次测试解决了这个问题(缺乏 C 编码技能水平真的困扰我 Arduino 编码......)
这是新代码(根据@frarugi87 的建议编辑)
byte myData[20];
*((long int *)(myData+0)) = count;
*((long int *)(myData+4)) = microSec;
*((short int *)(myData+8)) = ax;
*((short int *)(myData+10)) = ay;
*((short int *)(myData+12)) = az;
*((short int *)(myData+14)) = gx;
*((short int *)(myData+16)) = gy;
*((short int *)(myData+18)) = gz;
它确实有效并且数据是正确的,例如
数据:0C 6A 30 00
回答:3172876
而且我还找到了另一种方法:
myFile3.write( (byte*)&count, sizeof(long));
myFile3.write( (byte*)µSec, sizeof(long));
myFile3.write( (byte*)&ax, sizeof(short));
myFile3.write( (byte*)&ay, sizeof(short));
myFile3.write( (byte*)&az, sizeof(short));
myFile3.write( (byte*)&gx, sizeof(short));
myFile3.write( (byte*)&gy, sizeof(short));
myFile3.write( (byte*)&gz, sizeof(short));
同样有效,数据格式与第一种编码方式相同
此致!
我最近在写一些关于获取 IMU 数据并将其保存到 SD 卡的代码。为了提高写入数据的速度,我需要使用二进制格式来保存它,这是我的代码:
byte myData[20];
((long int*)myData)[0] = count;
((long int*)myData)[4] = microSec;
((short int*)myData)[8] = ax;
((short int*)myData)[10] = ay;
((short int*)myData)[12] = az;
((short int*)myData)[14] = gx;
((short int*)myData)[16] = gy;
((short int*)myData)[18] = gz;
myFile3.write(myData, sizeof(myData));
但是完全不行。(但是可以编译,真是迷惑我了。。。)
有人遇到这个问题吗...?
试试这个怎么样:
byte myData[20];
((long int*)myData)[0] = count;
((long int*)myData)[1] = microSec;
((short int*)myData)[4] = ax;
((short int*)myData)[5] = ay;
((short int*)myData)[6] = az;
((short int*)myData)[7] = gx;
((short int*)myData)[8] = gy;
((short int*)myData)[9] = gz;
myFile3.write(myData, sizeof(myData));
谢谢大家!通过数百次测试解决了这个问题(缺乏 C 编码技能水平真的困扰我 Arduino 编码......)
这是新代码(根据@frarugi87 的建议编辑)
byte myData[20];
*((long int *)(myData+0)) = count;
*((long int *)(myData+4)) = microSec;
*((short int *)(myData+8)) = ax;
*((short int *)(myData+10)) = ay;
*((short int *)(myData+12)) = az;
*((short int *)(myData+14)) = gx;
*((short int *)(myData+16)) = gy;
*((short int *)(myData+18)) = gz;
它确实有效并且数据是正确的,例如
数据:0C 6A 30 00
回答:3172876
而且我还找到了另一种方法:
myFile3.write( (byte*)&count, sizeof(long));
myFile3.write( (byte*)µSec, sizeof(long));
myFile3.write( (byte*)&ax, sizeof(short));
myFile3.write( (byte*)&ay, sizeof(short));
myFile3.write( (byte*)&az, sizeof(short));
myFile3.write( (byte*)&gx, sizeof(short));
myFile3.write( (byte*)&gy, sizeof(short));
myFile3.write( (byte*)&gz, sizeof(short));
同样有效,数据格式与第一种编码方式相同
此致!