用于 LoRa 的 Arduino SAMD 整数到字节的转换
Arduino SAMD Integer to Byte conversion for LoRa
我正在为 MKRWAN LoRa arduino 开发板编写一段代码。
使用 MKRWAN 库,该库没有很好的文档记录。
此代码的目标是接受一些整数和 return 字节,以便它们可以存储在字节数组中,我应该能够通过 LoRa 网络发送这些字节。
在 arduino 文档中描述了带有 SAMD 芯片的 arduino 以 4 个字节存储整数。
On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).
来源:https://www.arduino.cc/reference/en/language/variables/data-types/int/
MKRWAN 1310开发板采用SAMD21芯片
The MKR WAN 1310, brings in a series of improvements when compared to its predecessor, the MKR WAN 1300. While still based on the Microchip® SAMD21 low power processor, the Murata CMWX1ZZABZ LoRa® module, and the MKR family’s characteristic crypto chip (the ECC508), the MKR WAN 1310 includes a new battery charger, a 2MByte SPI Flash, and improved control of the board’s power consumption.
来源:https://store.arduino.cc/mkr-wan-1310
然而,当我 运行 我的代码时,它吐出一些整数为 4 个字节,一些则更少。
-9001 in BINARY = 11010111 11011100 11111111 11111111
newInt = -9001
13 in BINARY = 1101 0 0 0
newInt = 13
-1 in BINARY = 11111111 11111111 11111111 11111111
newInt = -1
250 in BINARY = 11111010 0 0 0
newInt = 250
-1950 in BINARY = 1100010 11111000 11111111 11111111
newInt = -1950
13450 in BINARY = 10001010 110100 0 0
newInt = 13450
我正在使用联合将整数转换为字节。
SAMD 开发板是否动态存储整数?
我的代码有错吗?
#define ArraySize 4
byte ATA[ArraySize]; // Air temperature array
byte AHA[ArraySize]; // Air humidity array
byte GT1A[ArraySize]; // Ground temperature sensor 1 array
byte GT2A[ArraySize]; // Ground temperature sensor 2 array
byte GT3A[ArraySize]; // Ground temperature sensor 3 array
byte GHA[ArraySize]; // Ground humidty sensor array
byte master[24]; // message array
union unionForm { // werkt naar behoren
byte myBytes[ArraySize];
int myInt;
} myUnion;
void setup() {
// put your setup code here, to run once:
int int1 = -9001;
int int2 = 13;
int int3 = -1;
int int4 = float_to_int(2.5);
int int5 = float_to_int(-19.5);
int int6 = float_to_int(134.5);
Serial.begin(115200);
while(!Serial);
int_to_bytes(int1, ATA);
int_to_bytes(int2, AHA);
int_to_bytes(int3, GT1A);
int_to_bytes(int4, GT2A);
int_to_bytes(int5, GT3A);
int_to_bytes(int6, GHA);
Combine_Arrays();
for (int i = 0; i < 24; i++){
Serial.print(master[i], BIN);
Serial.print(" ");
}
Serial.println();
}
void loop(){
}
int float_to_int(float floatyboi){
Serial.print("Float:\t");
Serial.println(floatyboi);
int newInt = floatyboi * 100; // convert 2 decimal float to int by multiplying by 100 and saving as int
Serial.print("New Int:\t");
Serial.println(newInt);
return newInt;
}
void int_to_bytes(int ConvertInt, byte (& MyArray)[ArraySize]){
myUnion.myInt = ConvertInt; // Save int into union
Serial.print(ConvertInt);
Serial.print(" in BINARY = ");
for (int i = 0; i < sizeof(int); i++) {
Serial.print(myUnion.myBytes[i], BIN);
Serial.print(" ");
MyArray[i] = myUnion.myBytes[i]; // save bits out of union in corresponding array
}
Serial.println();
int newInt = myUnion.myInt;
Serial.print("newInt = ");
Serial.println(newInt);
}
void Combine_Arrays(){
Serial.println("Combining Arrays");
master[0] = ATA[0];
master[1] = ATA[1];
master[2] = ATA[2];
master[3] = ATA[3];
master[4] = AHA[0];
master[5] = AHA[1];
master[6] = AHA[2];
master[7] = AHA[3];
master[8] = GT1A[0];
master[9] = GT1A[1];
master[10] = GT1A[2];
master[11] = GT1A[3];
master[12] = GT2A[0];
master[13] = GT2A[1];
master[14] = GT2A[2];
master[15] = GT2A[3];
master[16] = GT3A[0];
master[17] = GT3A[1];
master[18] = GT3A[2];
master[19] = GT3A[3];
master[20] = GHA[0];
master[21] = GHA[1];
master[22] = GHA[2];
master[23] = GHA[3];
}
亲切问候您的帮助
以这种方式使用联合并不是最好的方式。它会让你接触到未定义的行为。这是一种双关语形式。
最好将值移出并移入 32 位字
byte values[4] = {0xff, 0xff, 0xff, 0xff};
int output = values[3] << 24 | values[2] << 16 | values[1] << 8 | values[0];
values[0] = output;
values[1] = output >> 8;
values[2] = output >> 8;
values[3] = output >> 8;
您还应该查看值的符号性,因为它们可能会导致问题。
我正在为 MKRWAN LoRa arduino 开发板编写一段代码。 使用 MKRWAN 库,该库没有很好的文档记录。
此代码的目标是接受一些整数和 return 字节,以便它们可以存储在字节数组中,我应该能够通过 LoRa 网络发送这些字节。
在 arduino 文档中描述了带有 SAMD 芯片的 arduino 以 4 个字节存储整数。
On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).
来源:https://www.arduino.cc/reference/en/language/variables/data-types/int/
MKRWAN 1310开发板采用SAMD21芯片
The MKR WAN 1310, brings in a series of improvements when compared to its predecessor, the MKR WAN 1300. While still based on the Microchip® SAMD21 low power processor, the Murata CMWX1ZZABZ LoRa® module, and the MKR family’s characteristic crypto chip (the ECC508), the MKR WAN 1310 includes a new battery charger, a 2MByte SPI Flash, and improved control of the board’s power consumption.
来源:https://store.arduino.cc/mkr-wan-1310
然而,当我 运行 我的代码时,它吐出一些整数为 4 个字节,一些则更少。
-9001 in BINARY = 11010111 11011100 11111111 11111111
newInt = -9001
13 in BINARY = 1101 0 0 0
newInt = 13
-1 in BINARY = 11111111 11111111 11111111 11111111
newInt = -1
250 in BINARY = 11111010 0 0 0
newInt = 250
-1950 in BINARY = 1100010 11111000 11111111 11111111
newInt = -1950
13450 in BINARY = 10001010 110100 0 0
newInt = 13450
我正在使用联合将整数转换为字节。
SAMD 开发板是否动态存储整数? 我的代码有错吗?
#define ArraySize 4
byte ATA[ArraySize]; // Air temperature array
byte AHA[ArraySize]; // Air humidity array
byte GT1A[ArraySize]; // Ground temperature sensor 1 array
byte GT2A[ArraySize]; // Ground temperature sensor 2 array
byte GT3A[ArraySize]; // Ground temperature sensor 3 array
byte GHA[ArraySize]; // Ground humidty sensor array
byte master[24]; // message array
union unionForm { // werkt naar behoren
byte myBytes[ArraySize];
int myInt;
} myUnion;
void setup() {
// put your setup code here, to run once:
int int1 = -9001;
int int2 = 13;
int int3 = -1;
int int4 = float_to_int(2.5);
int int5 = float_to_int(-19.5);
int int6 = float_to_int(134.5);
Serial.begin(115200);
while(!Serial);
int_to_bytes(int1, ATA);
int_to_bytes(int2, AHA);
int_to_bytes(int3, GT1A);
int_to_bytes(int4, GT2A);
int_to_bytes(int5, GT3A);
int_to_bytes(int6, GHA);
Combine_Arrays();
for (int i = 0; i < 24; i++){
Serial.print(master[i], BIN);
Serial.print(" ");
}
Serial.println();
}
void loop(){
}
int float_to_int(float floatyboi){
Serial.print("Float:\t");
Serial.println(floatyboi);
int newInt = floatyboi * 100; // convert 2 decimal float to int by multiplying by 100 and saving as int
Serial.print("New Int:\t");
Serial.println(newInt);
return newInt;
}
void int_to_bytes(int ConvertInt, byte (& MyArray)[ArraySize]){
myUnion.myInt = ConvertInt; // Save int into union
Serial.print(ConvertInt);
Serial.print(" in BINARY = ");
for (int i = 0; i < sizeof(int); i++) {
Serial.print(myUnion.myBytes[i], BIN);
Serial.print(" ");
MyArray[i] = myUnion.myBytes[i]; // save bits out of union in corresponding array
}
Serial.println();
int newInt = myUnion.myInt;
Serial.print("newInt = ");
Serial.println(newInt);
}
void Combine_Arrays(){
Serial.println("Combining Arrays");
master[0] = ATA[0];
master[1] = ATA[1];
master[2] = ATA[2];
master[3] = ATA[3];
master[4] = AHA[0];
master[5] = AHA[1];
master[6] = AHA[2];
master[7] = AHA[3];
master[8] = GT1A[0];
master[9] = GT1A[1];
master[10] = GT1A[2];
master[11] = GT1A[3];
master[12] = GT2A[0];
master[13] = GT2A[1];
master[14] = GT2A[2];
master[15] = GT2A[3];
master[16] = GT3A[0];
master[17] = GT3A[1];
master[18] = GT3A[2];
master[19] = GT3A[3];
master[20] = GHA[0];
master[21] = GHA[1];
master[22] = GHA[2];
master[23] = GHA[3];
}
亲切问候您的帮助
以这种方式使用联合并不是最好的方式。它会让你接触到未定义的行为。这是一种双关语形式。
最好将值移出并移入 32 位字
byte values[4] = {0xff, 0xff, 0xff, 0xff};
int output = values[3] << 24 | values[2] << 16 | values[1] << 8 | values[0];
values[0] = output;
values[1] = output >> 8;
values[2] = output >> 8;
values[3] = output >> 8;
您还应该查看值的符号性,因为它们可能会导致问题。