zoukankan      html  css  js  c++  java
  • 树莓派小车By 树莓派爱好者ITJoker(通过C socket通信实现树莓派视频小车)(二)新博客:https://www.itjoker.cn

    前面的方法可以参照(一)来配置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!
    ");
        while(listen(fd, 10) == -1);
        printf("Listening....
    ");
        printf("Ready for Accept,Waitting...
    ");
        new_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
        printf("Get the Client.
    ");
        // numbytes = send(new_fd,"Welcome to my server
    ",21,0);
        while((numbytes = recv(new_fd, buff, BUFSIZ, 0)) > 0)
        {
            buff[numbytes] = '';
            printf("%s
    ",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;
    }
    View Code

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

    sudo nano makefile
    

    编写内容如下:

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

    3.最后  输入指令:

    make
    

      

    内容太长无法显示。
  • 相关阅读:
    K8S之traefik高级特性
    docker nginx-php容器镜像瘦身优化
    Dubbo-服务消费者初始化
    ThreadLocal 源码分析
    JVM 对象分配规则
    JVM GC 机制
    LockSupport
    自旋锁
    CAS
    Synchronized 监视器锁
  • 原文地址:https://www.cnblogs.com/ITJoker/p/8423150.html
Copyright © 2011-2022 走看看