arduino IDE编译器相当宽容,可以将变量定义为“字节”,它是uint8_t或无符号字符,并且似乎可以运行。但是它不会直接与此测试代码一起插入。我正在使用Arduino IDE和PlatformIO来比较结果。为了调试,我将此代码插入到OnlineGBD C ++模拟器中。我需要以十六进制形式提供表示二进制值的字节,就像我像这样插入它一样:
const char s [] = {0x37,0x74,0x37,0}; // 00110111,01110100,00110111,0writeTextPacket
我必须将模拟器中的“字节b [6]”更改为“ char b [6]”才能运行,但是printf看起来像是在给我指针,而不是在内存位置。我的第二个Printf尝试将“ b”变量格式化为%p以及%x进行测试,并在值前面加上0x7ffe,因此(void **)b为0x7ffexxxxxxxx,b的值为xxxxxxxx。
最后,nBytes的返回值始终为“ 7”。它显然应该给我找到的字节数。我认为我对指针和字符感到困惑。谢谢。
void RegisterList::writeTextPacket(const char *s) volatile{
int nReg;
byte b[6];
int nBytes;
nBytes=sscanf(s,"%x %x %x %x %x %x",&nReg, b, b+1, b+2, b+3, b+4)-1; // return number of bytes
printf("%d\n",nBytes);
printf("%d, %p, %x, %x, %x, %x, %x", nReg, (void**) b, b, b+1, b+2, b+3, b+4);
loadPacket(nReg,b,nBytes,0,1); // loadPacket(int nReg, byte *b, int nBytes, int rep, int flag)
}
成功!感谢您的大力帮助。将“ byte”更改为“ uint8_t”(Arduino有一个“ typdef uint8_t字节”来处理),并用%hhx代替%x摆脱了警告。而这个测试代码:
const char *s="16 A9 69 55 C6 A8";
int nReg;
uint8_t b[6];
int nBytes;
nBytes=sscanf(s,"%d %hhx %hhx %hhx %hhx %hhx",&nReg, b, b+1, b+2, b+3, b+4)-1; // return number of bytes
printf("%d\n",nBytes);
printf("%d, %X, %X, %X, %X, %X", nReg, *b, *(b+1), *(b+2), *(b+3), *(b+4));
给出正确的结果,仅计算在整数nReg数字之后的十六进制数字:
5
16,A9,69,55,C6,A8