我正在尝试使用Arduino nano 33 IOT和Jetson Nano 2gb之间的i2c总线。我正在使用i2c总线,我想向Jetson发送一个整数数组,但是当我在总线上接收数据时,这是胡言乱语,破坏了从Jetson发送到Arduino的数据。
Jetson纳米引脚: GND,27 (SDA),28 (SDL) Arduino Nano 33 IoT引脚: GND,A4 (SDA),A5 (SCL)
Arduino代码:
#include <Wire.h>
int data [4];
int x = 0;
void setup() {
Serial.begin(9600);
Wire.begin(0x6a);
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
}
void loop () {
//sendData();
delay(100);
}
void sendData() {
int arr[4] = { 0, 23, 41, 19 };
Serial.println("Sending Data to Jetson ...");
//sendI2C((byte*) arr, sizeof(arr));
Wire.write( (byte*)&arr, sizeof(arr));
Serial.print("Sent...");
Serial.println();
}
//void sendI2C(byte *data, int size) {
// Wire.beginTransmission(0x6a);
// for(int i = 0; i < size; i++) {
// Wire.write(data[i]);
// }
// Wire.endTransmission();
//}
void receiveData(int byteCount) {
while(Wire.available() && x < 4) { //Wire.available() returns the number of bytes available for retrieval with Wire.read(). Or it returns TRUE for values >0.
data[x]=Wire.read();
x++;
}
if(x == 4) { x = 0; }
Serial.println("----");
Serial.print(data[0]);
Serial.print("\t");
Serial.print(data[1]);
Serial.print("\t");
Serial.print(data[2]);
Serial.print("\t");
Serial.println(data[3]);
Serial.print("----");
//sendData();
}Jetson Python3代码:
import smbus
import time
bus = smbus.SMBus(0)
address = 0x6a
def writeArray(a, b, c, d):
bus.write_i2c_block_data(address, a, [b, c, d])
return -1
def readArray(bytes_nr):
values = bus.read_i2c_block_data(address, 0x00, bytes_nr)
return values
while True:
writeArray(14,42,95,0)
time.sleep(1)
values = readArray(8)
print(values)有两件事会发生:
当我只从jetson发送数据到arduino,在arduino的串行监视器上数据被正确地接收:[14, 42, 95, 0]
[0, 0, 0, 0, 0, 0, 42, 105, 0 , 0, 4, 0 , 0 ,0 ,0 , 56, 0 , 0 , 0 ,0 ,0, 187, 0 , 0 ,0, 0, 0 , 0]
-- And on the Arduino console the data shifts from left to right so instead of receiving `[14, 42, 95, 0]`, It prints
[8, 14, 42, 95] 我只想从双方发送一个由4个整数组成的数组。
有人能伸出援手吗?
谢谢!
发布于 2022-07-03 07:33:06
Arduino代码使用原始I2C协议,Jetson在I2C之上使用SMBus。SMBus块结构意味着要传输一个额外的字节长度。
https://stackoverflow.com/questions/72841620
复制相似问题