【问题标题】:Raspberry error write in i2c socket覆盆子错误写入 i2c 套接字
【发布时间】:2016-06-07 20:38:35
【问题描述】:

我正在尝试通过智能手机控制我的家庭住宅,智能手机通过 Internet(Wi-Fi)向 Raspberry 发送 3-4 个字节,而 Raspberry 通过 I2C 总线将所有这些字节发送到相应的 Arduino(我有两个阿杜诺斯)。 当我向 Raspberry 发送命令时,它显示“无法写入 i2c 总线” 谁能帮帮我?

  int i2csend(msg_t *pmsg)
  {
    int fd;
    /* Open I2C device */
    if ((fd = open(device, O_RDWR)) < 0) error ("Can't open I2C device");
    if (ioctl(fd, I2C_SLAVE, arduino_addr) < 0) error ("Can't talk to slave");
    if (write(fd, (char *)pmsg, n) < n ) printf ("Failed to write to the i2c bus [1]\n");
    else
    {
      read(fd, (char *)pmsg, n);
      printf("Ricevuto il messaggio: %c%c %d %d\n", pmsg->tipo, pmsg->gruppo, pmsg->dato[0], pmsg->dato[1]);
    }
    close(fd);
    return 0;
  }

【问题讨论】:

    标签: sockets raspbian raspberry-pi2 i2c


    【解决方案1】:

    当我在 raspi 上使用 I2C 时,我从未在 if 语句中使用过“open”函数(就像您在 i2csend() 函数中使用的那样)。这是我的一个(工作)项目的示例:

    //open file for i2c interface
    int fh=open("/dev/i2c-1",O_RDWR);
    if (ioctl(fh, I2C_SLAVE, UIBC_ADDR) < 0){
        printf("Couldn't establish contact with the UIBC\n");
    }
    

    【讨论】:

    • 谢谢,但我已经解决了。我还没有将 rasp 和 arduino 接地连接在一起。我没有这样做,因为我第一次使用 USB 总线接地并且它已经工作了。这次我用的是独立电源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2020-11-15
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多