簡體   English   中英

我想使用 I2C 從 arduino 接收多個數據到樹莓派

[英]I want to receive multiple data from arduino to raspberry pi using I2C

感謝任何有禮貌地調查這個問題的人。

我想使用 I2C 從 arduino 接收多個數據到樹莓派。

我可以從 arduino 獲得 1 個數據,但是一旦我移動到多個數據,它就無法這樣做。

到目前為止,我已經嘗試了多種方法,我發現這種方法最適合從 Arduino 獲取數據。

我之前從 arduino 獲取數據的嘗試如下: 我想使用 Raspberry Pi 使用 I2C 從 Arduino 讀取Raspberry Pi 的終端響應有無法識別的奇怪字體

現在都解決了。 從下面的鏈接獲得大量幫助https://area-51.blog/2014/02/15/connecting-an-arduino-to-a-raspberry-pi-using-i2c/

Arduino 代碼

#include <Wire.h>

#define echoPin 7
#define trigPin 8

int number=0;
long duration;
long distance;

void setup()
{
  //Join I2C bus as slave with address 8
  Wire.begin(8);

  //Call SendData & Receive Data
  Wire.onRequest(SendData);
  //Setup pins as output and input to operate ultrasonic sensor
  Serial.begin(9600);
  pinMode(echoPin,INPUT);
  pinMode(trigPin,OUTPUT);
}

void loop ()
{
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin,HIGH);
  delayMicroseconds(2);

  digitalWrite(trigPin,LOW);

  duration=pulseIn(echoPin,HIGH);

  distance=duration/58.2;

  Serial.print(distance);
  Serial.println(" cm");
}

void SendData()
{
  Wire.write(distance);
  Wire.write("Why No Work?");
  Wire.write(distance);
}

C++代碼

//Declare and Include the necessary header files
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>

//Define Address of Slave Address
#define ADDRESS 0x08

//Eliminate the Used of std in the future
using namespace std;

static const char *devName="/dev/i2c-1";

int main(int argc, char **argv)
{
    //Check to see if C++ works
    cout<<"Hello, World!\n";
    cout<<"I2C: Connecting"<<endl;
    int file;

    if ((file = open(devName, O_RDWR))<0)
    {
        fprintf(stderr, "I2C: Failed to access");
        exit(1);
    }

    if (ioctl(file, I2C_SLAVE, ADDRESS)<0)
    {
        cout<<"Failed to Access"<<endl;
    }
    char buf[0];
    char dd;
    for (int i=0; i<100;i++)
    {
        read(file,buf, 3);
        float distance= (int) buf[0];
        dd= buf[1];
        float dist=(int) buf[2];

        cout<<distance<<endl;
        usleep(10000);
        cout<<"doh"<<endl;
        cout<<dd<<endl;
        cout<<dist<<endl;
    }
    return 0;
}

我對 c++ 代碼的期望如下

15
doh
Why No Work?
15

但我明白了

15
doh
weird font can't be recognized
255
Wire.write(distance);

想在 I2C 總線上寫一個long 對於 Arduino 這是 32 位 4 字節的數據。 我不確定wire.write到底做了什么,因為我能找到的文檔不合標准,甚至可以說是垃圾,但文檔看起來會發送你想要發送的4 個字節中的1 個。 為了發送多個字節,看起來您需要使用數組版本: Wire.write(&distance, sizeof (distance)); ,但即使這樣可能還不夠。 我稍后再談。

Wire.write("Why No Work?");

將一個以 null 結尾的字符串(特別是const char[13] )寫入 I2C 總線。 我不知道 arduino 是否也發送終止 null。

所以

Wire.write(distance);
Wire.write("Why No Work?");
Wire.write(distance);

需要將至少 4 + 12 + 4 個字節寫入 I2C 總線。 並且可能只寫了 1 + 12 + 1。

在 Pi 方面,

read(file,buf, 3);

讀出 3 個字節。 這還不足以得到完整的distance ,更不用說字符數組和distance的第二次寫入了。 您需要讀取您編寫的所有數據,至少 20 個字節。

此外,

char buf[0];

定義一個長度為 0 的數組。 您無能為力,因為這里沒有存儲任何東西的空間。 它不能容納 3 個字符,更不用說必要的 20 或 21 個字符了。 read 3 個字節寫入無效 memory 並且程序不能再指望理智的結果

這意味着充其量

    float distance= (int) buf[0];
    dd= buf[1];
    float dist=(int) buf[2];

只得到了四個字節distance中的一個字節,結果與預期的一樣真是太幸運了。 dd只得到一個字符,而不是整個字符串,由於前面的錯誤之一,這被證明是無稽之談。 dist同樣是垃圾。

要成功地將數據從一台機器移動到另一台機器,您需要建立通信協議。 你不能只在電線上寫一個long字。 long在所有平台上的大小都不相同,也不總是具有相同的 encoding 您必須絕對確定雙方就long的寫入方式(大小和字節順序)和讀取方式達成一致。

您將如何執行此操作取決於您,但這里有一些提示和搜索詞serialization ,可幫助您進行進一步的研究。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM