我想在我的房間手冊中遠程控制插座,而無需在樹莓派上添加額外的庫。我想使用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