在i2c套接字中写入覆盆子错误

时间:2016-06-07 20:38:36

标签: sockets raspbian raspberry-pi2 i2c

我正试图通过智能手机控制我的家庭住宅,智能手机通过互联网(Wi-Fi)向Raspberry发送3-4个字节,Raspberry通过I2C总线将所有这些字节发送到相应的Arduino(我有两个Arduinos)。 当我将命令发送到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;
  }

1 个答案:

答案 0 :(得分:0)

当我在raspi上使用I2C时,我从来没有使用过&#39; open&#39;函数在if语句中(就像你在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");
}