通过PC控制开发板的LED 和按键,PC每次点亮了LED或者按了按键之后,PC上会显示出所有LED和按键的状态
烧写到开发板的C程序要交叉编译
服务端的程序server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <fcntl.h>
#define SERVPORT 3333
#define BACKLOG 10 //请求队列中的最大请求数
#define MAX_CONNECTED_NO 10 //最大是128台电脑连接
#define MAXDATASIZE 100
void pthread_recv(void *arg);
void pthread_send(void *arg);
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sockfd,client_fd,sin_size,ret;
pthread_t id1,id2;
sin_size =sizeof(struct sockaddr);
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
printf("\033[2J\033[0;0H");
printf("socket success!,sockfd=%d\n",sockfd);
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVPORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY; //htonl(INADDR_ANY)
bzero(&(server_sockaddr.sin_zero),8);
//把本端的信息绑定
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1)
{
perror("bind");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen");
exit(1);
}
printf("listening....\n");
if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1)
{
perror("accept");
exit(1);
}
printf("client_fd=%d\n",client_fd);
ret = pthread_create(&id1,NULL,(void *)pthread_recv,(void *)&client_fd);
if(ret != 0)
perror("pthread_recv creat");
ret = pthread_create(&id2,NULL,(void *)pthread_send,(void*)&client_fd);
if(ret != 0)
perror("Pthread_send creat");
pthread_join(id1,NULL);
pthread_join(id2,NULL);
close(sockfd);
}
//服务器发送线程
void pthread_send(void *arg)
{
int buf_len, client_fd, sendbytes;
char buf[MAXDATASIZE];
client_fd = *((int *)arg);
buf_len = sizeof(buf);
while(1)
{
memset(buf,0,buf_len);
printf("I say to client:...........................\n");
fgets(buf,MAXDATASIZE,stdin);
if((sendbytes = send(client_fd,buf,buf_len,0))==-1)
{
perror("send");
exit(1);
}
if(!strncmp(buf,"end",3)) //只比较字符串的前三个字符
break;
}
}
//服务器接收数据线程
void pthread_recv(void *arg)
{
int buf_len,client_fd,recvbytes,fd;
char buf[MAXDATASIZE];
fd = open("/opt/hiphon/s_data",O_RDWR);
client_fd = *((int *)arg);
buf_len = sizeof(buf);
while(1)
{
memset(buf,0,buf_len);
if((recvbytes = recv(client_fd,buf,MAXDATASIZE,0))==-1)
{
perror("recv");
exit(1);
}
else
{
buf[recvbytes]='\0';
printf("Received data from client is :%s\n",buf);
write(fd,buf,recvbytes+1);
if(!strncmp(buf,"end",3)) //只比较字符串前三个字符
break;
}
}
}
客户端的程序
client.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#define SERVPORT 3333
#define MAXDATASIZE 100
void pthread_recv(void *arg,int argc,char **argv);
void pthread_send(void *arg);
void pthread_buttonled(void *arg);
int main(int argc,char *argv[])
{
int sockfd,sin_size,ret;
struct hostent *host;
pthread_t id1,id2,id3;
struct sockaddr_in serv_addr; //ÌלÓ×ֵĵØÖ·œá¹¹
struct sockaddr_in server_sockaddr;
printf("\033[2J\033[0;0H");
if(argc < 2)
{
fprintf(stderr,"Please enter the server's hostname!\n");
exit(1);
}
//AF_INET:ʹÓõÄÊÇIPV4
//SOCK_STREAM:Á÷ÊœÌלÓ×Ö
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit(1);
}
printf("sockfd=%d\n",sockfd);
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
bzero(&(serv_addr.sin_zero),8);
//Á¬œÓ
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
perror("connect");
exit(1);
}
ret = pthread_create(&id1,NULL,(void *)pthread_send,(void *)&sockfd);
if(ret != 0)
perror("Pthread_send creat");
ret = pthread_create(&id2,NULL,(void *) pthread_recv,(void *)&sockfd);
if(ret != 0)
perror("pthread_recv creat");
ret = pthread_create(&id3,NULL,(void *) pthread_buttonled,(void *)&sockfd);
if(ret != 0)
perror("pthread_recv creat");
pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_join(id3,NULL);
close(sockfd);
}
//¿Í»§¶Ë·¢ËÍÊýŸÝÏß³Ì
void pthread_send(void *arg)
{
int buf_len,sockfd,sendbytes;
char buf[MAXDATASIZE];
sockfd = *((int *)arg);
buf_len = sizeof(buf);
while(1)
{
memset(buf,0,buf_len);
printf("I say to server:............................\n");
fgets(buf,MAXDATASIZE,stdin);
if((sendbytes = send(sockfd,buf,buf_len,0))==-1)
{
perror("send");
exit(1);
}
if(!strncmp(buf,"end",3)) //Ö»±ÈœÏ×Ö·ûŽ®µÄÇ°Èýžö×Ö·û
break;
}
}
//¿Í»§¶ËœÓÊÕÊýŸÝÏß³Ì
void pthread_recv(void *arg,int argc,char **argv)
{
int buf_len,sockfd,recvbytes,fd;
char buf[MAXDATASIZE];
sockfd = *((int *)arg);
buf_len = sizeof(buf);
fd = open("/opt/hiphon/c_data",O_RDWR);
struct ctrl
{
char *1_o,*1_f,*2_o,*2_f;
char *3_o,*3_f,*4_o,*4_f;
};
struct ctrl cmd;
cmd.1_o="led 0 state is on !";
cmd.1_f="led 0 state is off !";
cmd.2_o="led 1 state is on !";
cmd.2_f="led 1 state is off !";
cmd.3_o="led 2 state is on !";
cmd.3_f="led 2 state is off !";
cmd.4_o="led 3 state is on !";
cmd.4_f="led 3 state is off !";
int on=0;
int led_no=0;
int fd1;
int i;
int result;
fd1 = open("/dev/hiphon_leds", 0);
if (fd1 < 0) {
fd1 = open("/sys/devices/virtual/misc/hiphon_leds", 0);
}
if (fd1 < 0) {
perror("open device leds");
exit(1);
}
//œÓÊÕ·þÎñ¶Ë·¢ÀŽµÄÐÅÏ¢
while(1)
{
memset(buf,0,buf_len);
if((recvbytes = recv(sockfd,buf,MAXDATASIZE,0))==-1)
{
perror("recv");
exit(1);
}
else
{
buf[recvbytes]='\0';
printf("client received data from server is :%s\n",buf);
switch(buf[0])
{
case '0':
on=0;break;
case '1':
on=1;break;
}
switch(buf[1])
{
case '0':
led_no=0;break;
case '1':
led_no=1;break;
case '2':
led_no=2;break;
case '3':
led_no=3;break;
}
ioctl(fd1, on, led_no);
if(led_no==0 && on==1)
printf("led0 is on \n");
if(led_no==0 && on==0)
printf("led0 is off\n");
if(led_no==1 && on==1)
printf("led1 is on \n");
if(led_no==2 && on==0)
printf("led1 is off \n");
if(led_no==2 && on==1)
printf("led2 is on \n");
if(led_no==2 && on==0)
printf("led3 is off \n");
if(led_no==3 && on==1)
printf("led3 is on \n");
if(led_no==3 && on==0)
printf("led3 is off \n");
if((sendbytes = send(sockfd,buf,2,0))==-1)
{
perror("send");
exit(1);
}
result=ioctl(fd1, on, led_no);
if((result&0x01)==0) {
printf("%s\n",cmd.1_o);
send(sockfd,cmd.1_o,19,0); }
else {
printf("%s\n",cmd.1_f);
send(sockfd,cmd.1_f,20,0); }
if((result&0x02)==0) {
printf("%s\n",cmd.2_o);
send(sockfd,cmd.2_o,19,0); }
else {
printf("%s\n",cmd.2_f);
send(sockfd,cmd.2_f,20,0); }
if((result&0x04)==0) {
printf("%s\n",cmd.3_o);
send(sockfd,cmd.3_o,19,0); }
else {
printf("%s\n",cmd.3_f);
send(sockfd,cmd.3_f,20,0); }
if((result&0x08)==0) {
printf("%s\n",cmd.4_o);
send(sockfd,cmd.4_o,19,0); }
else {
printf("%s\n",cmd.4_f);
send(sockfd,cmd.4_f,20,0); }
write(fd,buf,recvbytes+1);
if(!strncmp(buf,"end",3)) //Ö»±ÈœÏ×Ö·ûŽ®µÄÇ°Èýžö×Ö·û
break;
}
}
}
void pthread_buttonled(void *arg)
{
int i=0;
int ret=0;
int fd=-1;
int buf_len,sockfd,sendbytes;
char buf[MAXDATASIZE];
sockfd = *((int *)arg);
static char buff[8]={'0','0','0','0','0','0','0','0'};
int on;
int led_no;
int fd1;
int result;
struct ctrl
{
char *1_o,*1_f,*2_o,*2_f;
char *3_o,*3_f,*4_o,*4_f;
};
struct ctrl cmd;
cmd.1_o="led 0 state is on !";
cmd.1_f="led 0 state is off !";
cmd.2_o="led 1 state is on !";
cmd.2_f="led 1 state is off !";
cmd.3_o="led 2 state is on !";
cmd.3_f="led 2 state is off !";
cmd.4_o="led 3 state is on !";
cmd.4_f="led 3 state is off !";
fd1 = open("/dev/hiphon_leds", 0);
if (fd1 < 0) {
fd = open("/sys/devices/virtual/misc/hiphon_leds", 0);
}
if (fd1 < 0) {
perror("open device leds");
exit(1);
}
fd=open("/dev/hiphon_buttons",0);
if (fd<0)
{
printf("can't open /dev/hiphon_buttons\n");
}
while(1) {
ret = read(fd, &buff, sizeof(buff));
for(i=0; i<8; i++) {
if (buff == '1')
{
printf("Key[%d]press\n",i);
switch(i)
{
case 0: buf[0]='A'; break;
case 1: buf[0]='B'; break;
case 2: buf[0]='C'; break;
case 3: buf[0]='D'; break;
case 4: buf[0]='E'; break;
case 5: buf[0]='F'; break;
case 6: buf[0]='G'; break;
case 7: buf[0]='H'; break;
}
buf[1]='\0';
if(buf[0]=='A')
{
led_no=0;on=1;
printf("led0 is on\n");
}
if(buf[0]=='B')
{
led_no=1;on=1;
printf("led1 is on\n");
}
if(buf[0]=='C')
{
led_no=2;on=1;
printf("led2 is on\n");
}
if(buf[0]=='D')
{
led_no=3;on=1;
printf("led3 is on\n");
}
if(buf[0]=='E')
{
led_no=0;on=0;
printf("led0 is on\n");
}
if(buf[0]=='F')
{
led_no=1;on=0;
printf("led1 is on\n");
}
if(buf[0]=='G')
{
led_no=2;on=0;
printf("led2 is on\n");
}
if(buf[0]=='H')
{
led_no=3;on=0;
printf("led3 is on\n");
}
ioctl(fd1,on,led_no);
if((sendbytes = send(sockfd,buf,2,0))==-1)
{
perror("send");
exit(1);
}
result=ioctl(fd1, on, led_no);
if((result&0x01)==0) {
printf("%s\n",cmd.1_o);
send(sockfd,cmd.1_o,19,0); }
else {
printf("%s\n",cmd.1_f);
send(sockfd,cmd.1_f,20,0); }
if((result&0x02)==0) {
printf("%s\n",cmd.2_o);
send(sockfd,cmd.2_o,19,0); }
else {
printf("%s\n",cmd.2_f);
send(sockfd,cmd.2_f,20,0); }
if((result&0x04)==0) {
printf("%s\n",cmd.3_o);
send(sockfd,cmd.3_o,19,0); }
else {
printf("%s\n",cmd.3_f);
send(sockfd,cmd.3_f,20,0); }
if((result&0x08)==0) {
printf("%s\n",cmd.4_o);
send(sockfd,cmd.4_o,19,0); }
else {
printf("%s\n",cmd.4_f);
send(sockfd,cmd.4_f,20,0); }
if(!strncmp(buf,"end",3)) //Ö»±ÈœÏ×Ö·ûŽ®µÄÇ°Èýžö×Ö·û
break;
}
}
}
close(fd);
}