源码
#include <wiringSerial.h> #include <wiringPi.h> #include <stdio.h> #include <string.h> #include <unistd.h> int main () { int fd; char cmd[128]={'\0'}; int nread; wiringPiSetup(); fd = serialOpen("/dev/ttyAMA0",9600); while(1){ nread = read (fd, cmd, sizeof(cmd)); if(strlen(cmd)==0){ printf("chaoShi\n"); continue; } if (strstr(cmd,"open")!=NULL){ printf("open light\n"); } if(strstr(cmd,"close")!=NULL){ printf("close light\n"); } memset(cmd,'\0',sizeof(cmd)/sizeof(char)); } return 0; }
接线
运行