生产实习(普特最后一节课)__2018.07.13

硬件代码:

test .c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>

#define SZ 1024
#define PORT 2020
#define SER_ADDR "192.168.1.117"


int key1_fd=-1;
int key2_fd=-1;
int key3_fd=-1;
int key4_fd=-1;
int led_fd=-1;
int beep_fd=-1;
int jqc_fd=-1;
int dht_fd=-1;	// /dev

int sock_fd=0;

int init_socket()
{
	int sock_fd=socket(AF_INET,SOCK_STREAM,0);
	if(sock_fd==-1)
	{
		perror("socket error:\n");
		exit(EXIT_FAILURE);
	}
	struct sockaddr_in sa;
	memset(&sa,0,sizeof(sa));

	sa.sin_family=AF_INET;
	sa.sin_port=htons(PORT);
	sa.sin_addr.s_addr=inet_addr(SER_ADDR);

	int ret=connect(sock_fd,(struct sockaddr*)&sa,sizeof(sa));
	if(ret==-1)
	{
		perror("connect error \n");
		exit(EXIT_FAILURE);
	}
	return sock_fd;
}

void open_all_dev()
{
	//char s2;	//key1  a
	key1_fd=open("/dev/s2",O_RDWR);
	if(key1_fd<0)
	{
		perror("open key1 error:");
		exit(-1);
	}

	//char s3;	//key2   b
	key2_fd=open("/dev/s3",O_RDWR);
	if(key2_fd<0)
	{
		perror("open key2 error:");
		exit(-1);
	}

	//char s4;	//key3   c
	key3_fd=open("/dev/s4",O_RDWR);
	if(key3_fd<0)
	{
		perror("open key3 error:");
		exit(-1);
	}

	//char s5;	//key4   d
	key4_fd=open("/dev/s5",O_RDWR);
	if(key4_fd<0)
	{
		perror("open key4 error:");
		exit(-1);
	}

	//char led;	//1 2
	led_fd=open("/dev/myled",O_RDWR);
	if(led_fd<0)
	{
		perror("open led error:");
		exit(1);
	}
	
	//char beep;
	beep_fd = open("/dev/mybeep", O_RDWR);
	if (beep_fd < 0) 
	{
		perror("open beep error:");
		exit(1);
	}

	//int jqc;
	jqc_fd=open("/dev/myjqc",O_RDWR);
	if(jqc_fd<0)
	{
		perror("open myjqc error:");
		exit(1);
	}

	dht_fd=open("/dev/mydht",O_RDWR);
	if(dht_fd<0)
	{
		perror("open dht11 error:");
		exit(1);
	}


	sock_fd=init_socket();
	if(sock_fd<0)
	{
		printf("init_socket failed \n");
		exit(1);
		//return -1;
	}
	else if(sock_fd>=0)
	{
		sleep(1);
		printf("DHT11 starting success...\n");
	}

}

#if 0

int dht11()
{
	char buf[5]={0};

	while(1)
	{
		read(dht_fd,buf,sizeof(buf));
		//printf("temperature: %d\n",buf[2]);
		//printf("humidity: %d\n",buf[4]);
		int ret=send(sock_fd,buf,sizeof(buf),0);
		if(ret==-1)
		{
			perror("send error \n");
			exit(EXIT_FAILURE);
		}
		sleep(1);
	}
	return 0;
}

#endif

int main(int argc, const char *argv[])
{
	open_all_dev();

	char beep='2';	//beep off
	write (beep_fd, &beep, 1);

	char led='1';	//led off
	write(led_fd,&led,1);
	
	int jqc=1;	//fan off
	write(jqc_fd,&jqc,1);

	char buf[5]={0};	//socket

	while(1)
	{
		read(dht_fd,buf,sizeof(buf));
		//printf("temperature: 	%d\n",buf[2]);
		//printf("humidity: 	%d%%\n",buf[4]);

		if(buf[2]>32&&buf[2]<34)
		{
			beep='1';
			led='2';
			jqc=0;
			write(beep_fd,&beep,1);	//beep on
			write(led_fd,&led,1);	//led on
			write(jqc_fd,&jqc,4);	//jqc on fan on
		}

		if(buf[2]<=30)
		{
			beep='2';
			led='1';
			jqc=1;
			write(beep_fd,&beep,1);	//beep off
			write(led_fd,&led,1);	//led off
			write(jqc_fd,&jqc,4);	//jqc on fan on
		}


		

		int ret=send(sock_fd,buf,sizeof(buf),0);
		if(ret==-1)
		{
			perror("send error\n");
			exit(EXIT_FAILURE);
		}

		sleep(1);
	}
#if 0
	//dht11();

	int ret;
	pthread_t id1;
	ret=pthread_create(&id1,NULL,(void *)dht11,NULL);
	if(ret)
	{
		printf("Create pthread error!\n");
		return 1;
	}

#endif

#if 0
	while(1)
	{
		char s2;	//key1 
		read(key1_fd, &s2, 1);
		printf("key1=%c\n",s2);
		if(s2 == 'a')
		{
			beep='1';
			write(beep_fd,&beep,1);	//beep on
			led='2';
			write(led_fd,&led,1);	//led on
		}

		char s3;	//key2
		read(key2_fd,&s3,1);
		printf("key2=%c\n",s3);
		if(s3=='b')
		{
			beep='2';	//beep off
			led='1';	//led off
			write(beep_fd,&beep,1);
			write(led_fd,&led,1);
		}

		char s4;	//key3
		read(key3_fd,&s4,1);
		printf("key3=%c\n",s4);
		if(s4=='c')
		{
			jqc=0;	//jqc on
			write(jqc_fd,&jqc,1);
		}
	}
#endif

	close(sock_fd);
	close(dht_fd);
	close(jqc_fd);
	close(beep_fd);
	close(led_fd);
	close(key4_fd);
	close(key3_fd);
	close(key2_fd);
	close(key1_fd);

	//pthread_join(id1,NULL);
	return 0;
}

server.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

#define	PORT	2020

int init_socket()
{
        int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
        if(sock_fd == -1){
                perror("socket err \n");
                exit(EXIT_FAILURE);
        }

        struct sockaddr_in sa;
        memset(&sa, 0, sizeof(sa));

        sa.sin_family = AF_INET;
        sa.sin_port = htons(PORT);
        sa.sin_addr.s_addr = INADDR_ANY;

        int on = 1;
        int ret = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
        if(ret < 0){
                perror("setsockopt err \n");
                exit(EXIT_FAILURE);
        }

        ret = bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa));
        if(ret == -1){
                perror("bind err \n");
                exit(EXIT_FAILURE);
        }

        listen(sock_fd, 10);

        return sock_fd;
}
int sock_fd = 0;
int init_server()
{
	sock_fd = init_socket();	
	if(sock_fd < 0){
		printf("init_socket failed \n");
		return -1;
	}
	sleep(1);
	printf("server initialization success ... \n");

	struct sockaddr_in sa;
	socklen_t len = sizeof(sa);
	int new_fd = accept(sock_fd, (struct sockaddr*)&sa, &len);	 
	if(new_fd < 0){
		perror("accept err \n");			
		exit(EXIT_FAILURE);
	}
	return new_fd;
}
int main(int argc, char* argv[])
{
	int new_fd = init_server();
	sleep(1);
	printf("The server receives the DHT11's request ... \n");

	char buf[5] = {0};
	while(1){
		int len = recv(new_fd, buf, sizeof(buf), 0);	
		if(len < 0){
			perror("recv err \n");	
			return -1;
		}
		printf("C: %d\n", buf[2]);
		printf("F: %d%%\n",(int)buf[4]);
		printf("-------------------------------\n");
		sleep(1);
	}
	close(new_fd);

	return 0;
}
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值