C Arduino串行,接收和存储并集和结构中的十六进制
我正试图将二进制数据发送到我的arduino。数据来自具有“command”和CRC32的结构 我使用union将所有十六进制数据放在一个字节数组中,并将其转换为两个独立的数据“command”和“CRC32” 在我的for循环中,从串行读取的每个字节都写入字节数组,当我逐字节读取数组时,存储的数据似乎是正确的,但当我打印整个数组时,输出不反映数据,我无法读取CRC32 有点不对劲,但我看不出是什么C Arduino串行,接收和存储并集和结构中的十六进制,c,arduino,C,Arduino,我正试图将二进制数据发送到我的arduino。数据来自具有“command”和CRC32的结构 我使用union将所有十六进制数据放在一个字节数组中,并将其转换为两个独立的数据“command”和“CRC32” 在我的for循环中,从串行读取的每个字节都写入字节数组,当我逐字节读取数组时,存储的数据似乎是正确的,但当我打印整个数组时,输出不反映数据,我无法读取CRC32 有点不对劲,但我看不出是什么 #define CMD_READ_ALL 0x01 #define CMD_READ_AL
#define CMD_READ_ALL 0x01
#define CMD_READ_ALL_CRC32 0xA505DF1B
typedef struct s_command {
uint8_t command;
uint32_t CRC32;
} t_command;
union command_transformer {
t_command command;
byte bytes[sizeof(t_command)];
};
void handleCommandAndResponde() {
union command_transformer transformer;
//LOG("Waiting for Serial...\r\n");
if ((unsigned)Serial.available() >= sizeof(t_command)) {
LOG("Serial data available: %d\r\n", Serial.available());
for (uint8_t i=0; i<sizeof(t_command); i++) {
transformer.bytes[i] = (byte)Serial.read();
LOG("incoming data: i=%d, data:%02x\r\n", i, transformer.bytes[i]);
}
LOG("transformer.bytes=0x%010x\r\n",transformer.bytes);
for (uint8_t i=0; i<sizeof(t_command); i++) {
LOG("data: i=%d, data:%02x\r\n", i, transformer.bytes[i]);
}
LOG("Serial read: 0x%010x\r\n", transformer.bytes);
LOG("Serial command: 0x%02x\r\n", transformer.command.command);
LOG("Serial CRC32: 0x%08x\r\n", transformer.command.CRC32);
if (transformer.command.command == CMD_READ_ALL && transformer.command.CRC32 == CMD_READ_ALL_CRC32) {
LOG("Command 0x01 OK");
sendData(BME280_readAll());
}
}
}
日志功能:
int LOG(const char *format, ...) {
va_list ap;
va_start(ap, format);
char buf[384];
size_t len = vsnprintf(buf, sizeof buf, format, ap);
va_end(ap);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
Serial.print((char*)buf);
return len;
}
新代码:
无法访问sendData(),但日志不处理32位值。
首先读取命令字节,然后从末尾读取其余字节并存储
void handleCommandAndResponde() {
t_command command;
byte CRC32_buf[4];
//LOG("Waiting for Serial...\r\n");
if ((unsigned)Serial.available() >= sizeof(t_command)) {
LOG("Serial data available: %d\r\n", Serial.available());
command.command = Serial.read();
LOG("incoming command:%02x\r\n", command.command);
for (uint8_t i=0; i<sizeof(uint32_t); i++) {
//read and store from the end
CRC32_buf[sizeof(uint32_t)-1-i] = (byte)Serial.read();
LOG("incoming CRC32: i=%d, data:%02x\r\n", i, CRC32_buf[i]);
}
memcpy(&command.CRC32, CRC32_buf, sizeof(uint32_t));
LOG("Serial command: 0x%02x\r\n", command.command);
LOG("Serial CRC32: 0x%08x\r\n", command.CRC32);
if (command.command == CMD_READ_ALL && command.CRC32 == CMD_READ_ALL_CRC32) {
LOG("Command 0x01 OK\r\n");
sendData(BME280_readAll());
}
}
}
void handleCommandAndResponde(){
t_指挥部,;
字节CRC32_buf[4];
//日志(“正在等待串行…\r\n”);
if((无符号)Serial.available()>=sizeof(t_命令)){
日志(“可用串行数据:%d\r\n”,Serial.available());
command.command=Serial.read();
日志(“传入命令:%02x\r\n”,command.command);
用于(uint8_t i=0;i8)&0xFF00FF);
返回值(val>16);
}
void handleCommandAndResponde(){
联合指挥变压器;
字节CRC32_buf[4];
//日志(“正在等待串行…\r\n”);
if((无符号)Serial.available()>=sizeof(t_命令)){
日志(“可用串行数据:%d\r\n”,Serial.available());
对于(UTI8AYT i=0),由于使用C++,IU不能使用Adulio中的类型进行双关,而不是C++。这是C++不特别适合硬件相关编程的几个原因之一。然后,您就有了网络接口和CPU接口。AVR应该是小EnDIN:ISIIrC(至于DeNANSESS适用于8个比特)。。因此,如果您的网络端号是big-endian,并且您发送了值0xa505df1b,那么您不能像您一样将其存储在uint32\u t
中,从最低地址字节到最高地址字节,假设您的系统使用小端号。什么是LOG()
?它是如何定义的?它能处理任意数据类型,如字节数组和32位值吗?您提供的格式字符串用于简单整数。@thebusybee The post已更新以显示日志函数。您可能希望尝试“0x%08lX”<代码> UTI32 32 t < /代码>。在不同的系统之间通信总是需要考虑的。确保您的理解正确。
void handleCommandAndResponde() {
t_command command;
byte CRC32_buf[4];
//LOG("Waiting for Serial...\r\n");
if ((unsigned)Serial.available() >= sizeof(t_command)) {
LOG("Serial data available: %d\r\n", Serial.available());
command.command = Serial.read();
LOG("incoming command:%02x\r\n", command.command);
for (uint8_t i=0; i<sizeof(uint32_t); i++) {
//read and store from the end
CRC32_buf[sizeof(uint32_t)-1-i] = (byte)Serial.read();
LOG("incoming CRC32: i=%d, data:%02x\r\n", i, CRC32_buf[i]);
}
memcpy(&command.CRC32, CRC32_buf, sizeof(uint32_t));
LOG("Serial command: 0x%02x\r\n", command.command);
LOG("Serial CRC32: 0x%08x\r\n", command.CRC32);
if (command.command == CMD_READ_ALL && command.CRC32 == CMD_READ_ALL_CRC32) {
LOG("Command 0x01 OK\r\n");
sendData(BME280_readAll());
}
}
}
#define CMD_READ_ALL 0x01
#define CMD_READ_ALL_CRC32 0xA505DF1B
typedef struct s_command {
uint8_t command;
uint32_t CRC32;
} t_command;
union command_transformer {
t_command command;
byte bytes[sizeof(t_command)];
};
uint32_t swap_uint32( uint32_t val )
{
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}
void handleCommandAndResponde() {
union command_transformer transformer;
byte CRC32_buf[4];
//LOG("Waiting for Serial...\r\n");
if ((unsigned)Serial.available() >= sizeof(t_command)) {
LOG("Serial data available: %d\r\n", Serial.available());
for (uint8_t i=0; i<sizeof(t_command); i++) {
transformer.bytes[i] = (byte)Serial.read();
LOG("incoming byte: i=%d, data:%02x\r\n", i, transformer.bytes[i]);
}
transformer.command.CRC32 = swap_uint32(transformer.command.CRC32);
LOG("Serial command: 0x%02x\r\n", transformer.command.command);
LOG("Serial CRC32: 0x%08lx\r\n", transformer.command.CRC32);
if (transformer.command.command == CMD_READ_ALL && transformer.command.CRC32 == CMD_READ_ALL_CRC32) {
LOG("Command 0x01 OK\r\n");
sendData(BME280_readAll());
}
}
}