我想在我的房间手册中远程控制插座,而无需在树莓派上添加额外的库。我想使用C语言的UART接口。插座有433 Mhz接收器,我使用433 Mhz发射器。在其他库中输入如下内容:send 11111 1 1.(套接字代码,套接字号,条件)。但是如何在C中用write()函数格式化这个命令? 10是用于字符的数量。我使用下面的代码。我通过minicom测试了输出,效果很好。但是,接收器现在如何知道它已经被满足了?C RbPi UART远程控制插座
#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;
}
我猜想:该协议提供了一个你必须把奴隶地址的地方。或者通讯是1-> 1(如RS232),只允许1个从站。 – LPs
那么我会假设每个套接字都有不同的代码,对吧? – immibis
是的主代码是11111和套接字代码从A到E,所以A是1我认为。然后条件1为“开”。 – Sven