我想遥控插座在我的房间手册没有额外的图书馆对我的树莓皮。我想用C语言的UART接口。这个插座有433MHz的接收器,我用433MHz的发射器。在其他图书馆,你可以这样输入:send1111111。(套接字代码、套接字编号、条件)。但是如何用write()函数在C中格式化这个命令呢?10代表字符数。我使用下面的代码。我通过minicom测试了输出,效果很好。但现在接受者怎么知道它是地址?

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main(int argc, char ** argv) {
  int fd;
  // Open the Port. We want read/write, no "controlling tty" status, and open i$
  fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("open_port: Unable to open /dev/ttyAMA0 - ");
    return(-1);
  }

  // Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
  fcntl(fd, F_SETFL, 0);

// Write to the port
  int n = write(fd,"11111 1 1",10);
  if (n < 0) {
    perror("Write failed - ");
    return -1;
  }

  // Don't forget to clean up
  close(fd);
  return 0;
}

最佳答案

我做对了吗:你通过UART发送一个命令,通过无线电波发送器发送一个信息。嗅探UART输出可以证明您的代码是正确的,因为TX line发送您希望它在软件中发送的内容,而实际的问题是“rx模块如何获得该消息/如何使rx模块获得该消息?”
如果是的话,主要的问题是你实际使用的是什么类型的无线收发或收发(我指的是芯片/模块代码,如CC1120、NRF2401等)?有很多433MHz的无线模块可用,我想首先查阅他们的数据表,或者至少在这里张贴零件号是正确的方法。

关于c - C RbPi UART远程控制 socket ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30231982/

10-12 02:04