首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在i2c套接字中写入Raspberry错误

在i2c套接字中写入Raspberry错误
EN

Stack Overflow用户
提问于 2016-06-08 04:38:36
回答 1查看 369关注 0票数 0

我试图通过智能手机控制我的住宅,智能手机通过互联网(Wi-Fi)发送3-4个字节到树莓,树莓通过I2C总线发送所有这些字节到相应的Arduino (我有两个Arduinos)。当我向树莓发送命令时,它显示“写入i2c总线失败”,有人能帮我吗?

代码语言:javascript
运行
复制
  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;
  }
EN

回答 1

Stack Overflow用户

发布于 2016-06-10 04:06:23

当我在raspi上使用I2C时,我从未在if语句中使用过'open‘函数(就像在i2csend()函数中一样)。下面是我的一个(正在工作的)项目的示例:

代码语言:javascript
运行
复制
//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");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37688908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档