基于ARM的智能灯光控制系统(10)网络节点
嵌入式开发培训(阶段2)底层系统开发
智能灯光控制系统
节点设备
络通信协议
//--head-- cmd data crc
//0x5a 0x15 0x 0x 0x
协议头部(2字节) | 命令位(1字节) | 数据位(1字节) | 校验位(1字节) | 功能 |
---|---|---|---|---|
0x5a 0x15 | 0x10 | 0x11 | 前四位求和 | 查询设备在线 |
0x5a 0x15 | 0x20 | 0x21 | 前四位求和 | 在线 |
0x5a 0x15 | 0x30 | 0x31 | 前四位求和 | 设备开 |
0x5a 0x15 | 0x30 | 0x32 | 前四位求和 | 设备关 |
0x5a 0x15 | 0x30 | 0x33 | 前四位求和 | 设备自动开关 |
主控端网络程序(net_pro.c)
net_por.h
#ifndef __SL2000_NET_H_
#define __SL2000_NET_H_
#define DEF_PORT 6899
int socket_init(int port);
void* client_pro(void* arg);
void* net_pro(void* arg);
int send_cmd(char cmd, char cmd_da ,int sockfd);
int get_cmd(char *buf,char* cmd,int len);
#endif
#include
#include
#include
#include
#include
#include "config.h"
#include "net_pro.h"
int net_user;
unsigned char net_cmd;
unsigned char net_flag;
extern struct sys_all g_dev[1];
extern struct dev_fd g_dev_fd;//系统设备文件
int socket_init(int port)
{
int sockfd;
struct sockaddr_in server_addr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
exit(1);
}
printf("socket...\n");
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(port);
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
printf("bind...\n");
if(listen(sockfd,10)==-1)
{
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
printf("listen...\n");
return sockfd;
}
//protocol
//--head-- cmd data crc
//0x5a 0x15 0x 0x 0x
int get_cmd(char *buf,char* cmd,int len)
{
char crc=0;
int i;
if(buf==NULL || cmd==NULL || len !=5 )
return -1;
if(buf[0] != 0x5a || buf[1] != 0x15)
return -1;
for(i=0;i
节点端网络程序(net_light.c)
#include
#include
#include
#include
#include
#include
#include
#include
#define DEV_RELAY "/dev/4418_relay"
#define DEV_PIR "/dev/4418_pir"
#define J1_OFF 0x01
#define J1_ON 0x00
#define BUF_SIZE 32
int fd_relay=-1,fd_pir = -1;
int dev_init()
{
printf("nanoPi driver init...\n");
fd_relay = open(DEV_RELAY,O_RDWR);
if(fd_relay<0){
printf("open device err\n");
return -1;
}
fd_pir = open(DEV_PIR,O_RDWR);
if(fd_pir<0){
printf("open device err\n");
return -1;
}
}
int net_init(char* ip ,char* port_str)
{
int sockfd,port;
struct sockaddr_in server_addr;
port = atoi(port_str);
if(port<1024){
printf("port error\n");
return -1;
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
return -1;
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(port);
server_addr.sin_addr.s_addr=inet_addr(ip);
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1){
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
return -1;
}
return sockfd;
}
void dev_sw(int sw)
{
char da[2];
if(sw == 3){
read(fd_pir,da,1);
write(fd_relay,da,1);
}else{
if(sw == 1)
da[0] = J1_ON;
if(sw == 2)
da[0] = J1_OFF;
write(fd_relay,da,1);
}
printf("==sw=%d==\n",sw);
}
//protocol
//--head-- cmd data crc
//0x5a 0x15 0x 0x 0x
int get_cmd(char *buf,char* cmd,int len)
{
char crc=0;
int i;
if(buf==NULL || cmd==NULL || len !=5 )
return -1;
if(buf[0] != 0x5a || buf[1] != 0x15)
return -1;
for(i=0;i= 5){
if(get_cmd(recv_buf,cmd,nbytes)==0)
cmd_proc(cmd,sockfd);
printf("\nbuf=%x,%x,%x,%x,%x=cmd=%x,%x=\n",
recv_buf[0],recv_buf[1],recv_buf[2],recv_buf[3],
recv_buf[4],cmd[0],cmd[1]);
}
// sleep(1);
}
return 0;
}