Я работаю над приложением, передающим данные с Arduino (ведомого) на Raspberry Pi (ведущего) по шине I²C. Я писал код, как вдруг передача перестала работать, и я не могу понять, почему. Я попробовал переустановить SMBus2, но проблема не устранена.
Вот тестовый код, пытающийся передать 7 байт из Arduino:
#! /usr/bin/python3
from smbus2 import SMBus
slaveAddress = 10
blockSize = 7
with SMBus(1) as bus:
# Read a block of blockSize bytes from address slaveAddres, offset 0
block = bus.read_i2c_block_data(slaveAddress, 0, blockSize)
# Returned value is a list of slaveAddress bytes
print(block)
Я попробовал несколько вариантов, но несмотря ни на что получил следующее:
[255, 255, 255, 255, 255, 255, 255]
Если я дам команду
i2ctransfer -y 1 r7@0x0a
Я получаю ожидаемый результат, похожий на:
0x32 0x01 0x00 0x00 0x00 0x01 0x00
Это означает, что код на Arduino и шине I²C ведет себя так, как ожидалось. Что может быть не так и как это исправить?
РЕДАКТИРОВАТЬ: Я пробовал читать по одному байту:
#! /usr/bin/python3
from smbus2 import SMBus
slaveAddress = 0x0a
blockSize = 7
block = []
with SMBus(1) as bus:
for x in range(blockSize):
block.append(bus.read_byte_data(slaveAddress, x))
print(block)
Я все равно получаю:
[255, 255, 255, 255, 255, 255, 255]
РЕДАКТИРОВАТЬ 2: Добавление соответствующего кода, работающего на Arduino
#include
#define Cool 0 // Peltier Cooler ON = HIGH
#define POK 1 // Power Failure IN
#define Fan 9 // Fan Relay ON = HIGH
#define PiCtl 14 // Raspberry Pi Power
#define ADDR1 15 // I2C Address 1
#define ADDR2 16 // I2C Address 2
#define SCL 23 // I2C Clock
#define SDA 24 // I2C Data
#define PTC 13 // Thermocouple Analog IN
int I2C;
bool True = 1;
bool False = 0;
float Temp;
bool FanSet;
bool PowerFail;
bool Responded;
unsigned long TimeStamp;
union Response {
struct {
int Temperature; // 2 bytes
unsigned long Living; // 4 bytes
uint8_t Command; // 1 byte
uint8_t FanValue; // 1 byte
uint8_t CoolStat; // 1 byte
uint8_t PowerStat; // 1 byte
uint8_t padding; // 1 byte
};
uint8_t bytes[11];
};
union Response Reply;
void setup()
{
pinMode(ADDR1, INPUT_PULLUP); // Address Offset 0 or 1
pinMode(ADDR2, INPUT_PULLUP); // Address Offset 0 or 2
I2C = (2 * digitalRead(ADDR2)) + digitalRead(ADDR1) + 8; // Set I2C address 8 - 11
Wire.begin(I2C); // Initialize I2C Communications
Wire.onRequest(requestEvent); // Call requestEvent when data requested
Wire.onReceive(receiveEvent); // Call receiveEvent when data received
Reply.PowerStat = 1; // Reset Power Status
Reply.FanValue = 1; // Set Fan Status On
Reply.CoolStat = 0; // Set Cool Status Off
}
void TempCheck()
{
Reply.Temperature = analogRead(PTC);
}
void PowerCheck()
{
bool Power = digitalRead(POK);
if (Power)
{
if (PowerFail)
{
PowerFail = False; // Reset Power Failure Variable
}
Reply.Command = 1; // Reset shutdown command
Reply.PowerStat = 1; // Reset Power Status
}
else
{
TimeStamp = millis(); // Reset Timer
int Iterations = 0; // Reset Time Counter
Reply.PowerStat = 0; // Set Power Status
Reply.Command = 1;
}
}
// Function that executes whenever data is requested from master
void requestEvent()
{
Wire.write(Reply.bytes, sizeof Reply); // respond with message as expected by master
Responded = True; // Set the responded variable
Reply.Living = millis(); // Set the responded time
}
void loop()
{
PowerCheck();
TempCheck();
delay (2000);
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... ddenly-qui
Чтение блока данных из I²C на Raspberry Pi с использованием Python SMBus2 внезапно перестало работать. Что может быть не ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение