前面的方法可以参照(一)来配置GPIO口。
下面是代码。。。仅供参考,转载请注明出处。
1. 先输入以下指令,再复制代码,Ctrl+X 输入Y 回车 保存。
sudo nano start.c
#include "car.h" #include <stdio.h> #include <stdlib.h> #include <wiringPi.h> #include <sys/time.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <string.h> #include <errno.h> #include <unistd.h> #define IN1 0 // wiringPi GPIO0(pin11) car.GPIO #define IN2 1 #define IN3 2 #define IN4 3 #define IN5 4 //Ultrasound module.GPIO send #define IN6 5 // accept /* Write By ITJoker Time:2.18.2.4 Version:1.0 */ int up() { wiringPiSetup() ; pinMode (IN1, OUTPUT) ; pinMode (IN2, OUTPUT) ; pinMode (IN3, OUTPUT) ; pinMode (IN4, OUTPUT) ; digitalWrite(IN1, HIGH) ; delay (500) ; digitalWrite(IN2, LOW) ; delay (500) ; digitalWrite(IN3, HIGH) ; delay (500) ; digitalWrite(IN4, LOW) ; delay (500) ; } int down() { wiringPiSetup() ; pinMode (IN1, OUTPUT) ; pinMode (IN2, OUTPUT) ; pinMode (IN3, OUTPUT) ; pinMode (IN4, OUTPUT) ; digitalWrite(IN1, LOW) ; delay (500) ; digitalWrite(IN2, HIGH) ; delay (500) ; digitalWrite(IN3, LOW) ; delay (500) ; digitalWrite(IN4, HIGH) ; delay (500) ; } int right() { wiringPiSetup() ; pinMode (IN1, OUTPUT) ; pinMode (IN2, OUTPUT) ; pinMode (IN3, OUTPUT) ; pinMode (IN4, OUTPUT) ; digitalWrite(IN1, LOW) ; delay (500) ; digitalWrite(IN2, HIGH) ; delay (500) ; digitalWrite(IN3, HIGH) ; delay (500) ; digitalWrite(IN4, LOW) ; delay (500) ; } int left() { wiringPiSetup() ; pinMode (IN1, OUTPUT) ; pinMode (IN2, OUTPUT) ; pinMode (IN3, OUTPUT) ; pinMode (IN4, OUTPUT) ; digitalWrite(IN1, HIGH) ; delay (500) ; digitalWrite(IN2, LOW) ; delay (500) ; digitalWrite(IN3, LOW) ; delay (500) ; digitalWrite(IN4, HIGH) ; delay (500) ; } int stop() { wiringPiSetup() ; pinMode (IN1, OUTPUT) ; pinMode (IN2, OUTPUT) ; pinMode (IN3, OUTPUT) ; pinMode (IN4, OUTPUT) ; digitalWrite(IN1, LOW) ; delay (500) ; digitalWrite(IN2, LOW) ; delay (500) ; digitalWrite(IN3, LOW) ; delay (500) ; digitalWrite(IN4, LOW) ; delay (500) ; } int main(int argc, char *argv[]) { int fd, new_fd, struct_len, numbytes,i; struct sockaddr_in server_addr; struct sockaddr_in client_addr; char buff[BUFSIZ]; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8888);//port 8888 server_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server_addr.sin_zero), 8); struct_len = sizeof(struct sockaddr_in); fd = socket(AF_INET, SOCK_STREAM, 0); while(bind(fd, (struct sockaddr *)&server_addr, struct_len) == -1); printf("Bind Success!\n"); while(listen(fd, 10) == -1); printf("Listening....\n"); printf("Ready for Accept,Waitting...\n"); new_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len); printf("Get the Client.\n"); // numbytes = send(new_fd,"Welcome to my server\n",21,0); while((numbytes = recv(new_fd, buff, BUFSIZ, 0)) > 0) { buff[numbytes] = ‘\0‘; printf("%s\n",buff); if(send(new_fd,buff,numbytes,0)<0) { perror("write"); return 1; } //txtz=find(buff,sizeof(buff),‘u‘) //strcmp(buff,‘u‘)==0 if(buff[0]==‘u‘) up(); else if(buff[0]==‘d‘) down(); else if(buff[0]==‘r‘) right(); else if(buff[0]==‘l‘) left(); else if(buff[0]==‘s‘) stop(); } close(new_fd); close(fd); return 0; }
2.为了编写方便,建立makedile。
sudo nano makefile
编写内容如下:
start:start.o gcc start.c -o start -lwiringPi clean: rm -f start start.o
3.最后 输入指令:
make