树莓派小车By 树莓派爱好者ITJoker(通过C socket通信实现树莓派视频小车)(二)

时间:2024-01-08 15:23:38

前面的方法可以参照(一)来配置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 () ;
digitalWrite(IN2, LOW) ; delay () ;
digitalWrite(IN3, HIGH) ; delay () ;
digitalWrite(IN4, LOW) ; delay () ;
}
int down()
{
wiringPiSetup() ;
pinMode (IN1, OUTPUT) ;
pinMode (IN2, OUTPUT) ;
pinMode (IN3, OUTPUT) ;
pinMode (IN4, OUTPUT) ;
digitalWrite(IN1, LOW) ; delay () ;
digitalWrite(IN2, HIGH) ; delay () ;
digitalWrite(IN3, LOW) ; delay () ;
digitalWrite(IN4, HIGH) ; delay () ;
}
int right()
{
wiringPiSetup() ;
pinMode (IN1, OUTPUT) ;
pinMode (IN2, OUTPUT) ;
pinMode (IN3, OUTPUT) ;
pinMode (IN4, OUTPUT) ;
digitalWrite(IN1, LOW) ; delay () ;
digitalWrite(IN2, HIGH) ; delay () ;
digitalWrite(IN3, HIGH) ; delay () ;
digitalWrite(IN4, LOW) ; delay () ;
}
int left()
{
wiringPiSetup() ;
pinMode (IN1, OUTPUT) ;
pinMode (IN2, OUTPUT) ;
pinMode (IN3, OUTPUT) ;
pinMode (IN4, OUTPUT) ;
digitalWrite(IN1, HIGH) ; delay () ;
digitalWrite(IN2, LOW) ; delay () ;
digitalWrite(IN3, LOW) ; delay () ;
digitalWrite(IN4, HIGH) ; delay () ;
}
int stop()
{
wiringPiSetup() ;
pinMode (IN1, OUTPUT) ;
pinMode (IN2, OUTPUT) ;
pinMode (IN3, OUTPUT) ;
pinMode (IN4, OUTPUT) ;
digitalWrite(IN1, LOW) ; delay () ;
digitalWrite(IN2, LOW) ; delay () ;
digitalWrite(IN3, LOW) ; delay () ;
digitalWrite(IN4, LOW) ; delay () ;
}
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();//port 8888
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero), );
struct_len = sizeof(struct sockaddr_in); fd = socket(AF_INET, SOCK_STREAM, );
while(bind(fd, (struct sockaddr *)&server_addr, struct_len) == -);
printf("Bind Success!\n");
while(listen(fd, ) == -);
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, )) > )
{
buff[numbytes] = '\0';
printf("%s\n",buff);
if(send(new_fd,buff,numbytes,)<)
{
perror("write");
return ;
}
//txtz=find(buff,sizeof(buff),'u')
//strcmp(buff,'u')==0
if(buff[]=='u')
up();
else if(buff[]=='d')
down();
else if(buff[]=='r')
right();
else if(buff[]=='l')
left();
else if(buff[]=='s')
stop();
}
close(new_fd);
close(fd);
return ;
}

2.为了编写方便,建立makedile。

sudo nano makefile

编写内容如下:

start:start.o
gcc start.c -o start -lwiringPi
clean:
rm -f start start.o

3.最后  输入指令:

make