目录
一、代码
主函数Main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "controlDevice.h"
#include "InputCommand.h"
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct Device *pDeviceHead = NULL;
struct inputcommander *pCommandHead = NULL;
struct inputcommander *socketHandler = NULL;
int c_fd;
struct Device *findDeviceByName(char *name,struct Device *phead)
{
struct Device* tmp = phead;
if(phead == NULL){
return NULL;
}else{
while(tmp != NULL){
if(strcmp(tmp->DeviceName,name) == 0){
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
}
struct inputcommander *FindcommandByName(char *name,struct inputcommander*phead)
{
struct inputcommander *tmp = phead;
if(phead == NULL){
return NULL;
}else{
while(tmp != NULL){
if(strcmp(tmp->commandName,name) == 0){
return tmp;
}
tmp = tmp->next;
}
}
};
void *voiceThread(void *datas)
{
struct inputcommander *voiceHandler;
int nread = 0;
voiceHandler = FindcommandByName("voice",pCommandHead);
if(voiceHandler == NULL){
printf("find voiceHandler error\n");
pthread_exit(NULL);
}else{
if( voiceHandler->Init(voiceHandler) < 0){
printf("voice init error\n");
pthread_exit(NULL);
}else{
printf("%s init success\n",voiceHandler->commandName);
}
while(1){
nread = voiceHandler->getcommand(voiceHandler);
if(nread == 0){
printf("voice nothing\n");
}else{
printf("control:%s\n",voiceHandler->command);
memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
}
}
}
}
void *read_t (void *datas)
{
int nread = 0;
memset(socketHandler->command,'\0',sizeof(socketHandler->command));
nread = read(c_fd,socketHandler->command,sizeof(socketHandler->command));
if(nread == -1){
perror("read");
}else if(nread > 0){
printf("get byte:%d,message:%s\n",nread,socketHandler->command);
}else{
printf("client quit\n");
}
}
void* socketThread(void *datas)
{
int nread = 0;
pthread_t readt;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int len = sizeof(struct sockaddr_in);
socketHandler = FindcommandByName("socket",pCommandHead);
printf("%s init success\n",socketHandler->commandName);
if(socketHandler == NULL){
printf("find socketHandler error\n");
pthread_exit(NULL);
}
socketHandler->Init(socketHandler);
while(1){
c_fd = accept(socketHandler->sfd,(struct sockaddr *)&c_addr,&len);
pthread_create(&readt,NULL,read_t,NULL);
}
}
int main()
{
if(wiringPiSetup() == -1){
printf("set up error!\n");
return -1;
}
struct Device *tmp = NULL;
struct Device *tmp1 = NULL;
int frieStatus;
pthread_t voice_thread;
pthread_t socket_thread;
//1.设备控制工厂初始化
pDeviceHead = addrBathroomLight(pDeviceHead);
pDeviceHead = addrlivingRoomLight(pDeviceHead);
pDeviceHead = addrrestaurantLight(pDeviceHead);
pDeviceHead = addrsecondfloorLight(pDeviceHead);
pDeviceHead = addrFireBell(pDeviceHead);
pDeviceHead = addrBee(pDeviceHead);
//2.控制工厂初始化
pCommandHead = addrVoiceControl(pCommandHead);
pCommandHead = addrSocketControl(pCommandHead);
//火警
/*
while(1){
tmp = findDeviceByName("Frie",pDeviceHead);
if(tmp != NULL){
tmp->DeviceInit(tmp->pinNum);
frieStatus = tmp->readStatus(tmp->pinNum);
tmp1 = findDeviceByName("Bee",pDeviceHead);
if(tmp1 != NULL){
tmp1->DeviceInit(tmp1->pinNum);
if(frieStatus == 0){
tmp1->DeviceInit(tmp1->pinNum);
tmp1->open(tmp1->pinNum);
sleep(3);
printf("fire\n");
}else{
tmp1->DeviceInit(tmp1->pinNum);
tmp1->close(tmp1->pinNum);
}
}
}
}
*/
//3.线程池开辟
//3.1语音线程
pthread_create(&voice_thread,NULL,voiceThread,NULL);
//3.2socket线程
pthread_create(&socket_thread,NULL,socketThread,NULL);
pthread_join(voice_thread,NULL);
pthread_join(socket_thread,NULL);
return 0;
}
语音Control-voice.c
#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int VoiceInit(struct inputcommander *commander)
{
int fd;
if((fd = serialOpen(commander->deviceName,115200))== -1){
exit(-1);
}else{
printf("serial open success\n");
}
commander->fd = fd;
return fd;
}
int VoiceGetCommand(struct inputcommander *commander)
{
int nread = 0;
nread = read(commander->fd,commander->command,sizeof(commander->command));
return nread;
}
struct inputcommander VoiceControl={
.commandName = "voice",
.deviceName = "/dev/ttyS5",
.command = {'\0'},
.Init = VoiceInit,
.getcommand = VoiceGetCommand,
.log = {'\0'},
.next = NULL
};
struct inputcommander *addrVoiceControl(struct inputcommander *phead)
{
if(phead == NULL){
return &VoiceControl;
}else{
VoiceControl.next = phead;
phead = &VoiceControl;
return phead;
}
};
网络Control-socket.c
#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
int SocketInit(struct inputcommander *commander)
{
int s_fd;
struct sockaddr_in s_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socket");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(commander->port));
inet_aton(commander->ipaddr,&s_addr.sin_addr);
//2.bind
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
printf("Socket server listening ........\n");
commander->sfd = s_fd;
return s_fd;
}
int SocketGetCommand(struct inputcommander *commander)
{
int c_fd;
int nread = 0;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int len = sizeof(struct sockaddr_in);
c_fd = accept(commander->sfd,(struct sockaddr *)&c_addr,&len);
memset(commander->command,0,sizeof(commander->command));
nread = read(c_fd,commander->command,sizeof(commander->command));
if(nread == -1){
perror("read");
}else if(nread > 0){
printf("get message:%d\n",nread);
}else{
printf("client quit\n");
}
return nread;
}
struct inputcommander SocketControl={
.commandName = "socket",
.command = {'\0'},
.Init = SocketInit,
.getcommand = SocketGetCommand,
.port = "8088",
.ipaddr = "192.168.0.107",
.log = {'\0'},
.next = NULL
};
struct inputcommander *addrSocketControl(struct inputcommander *phead)
{
if(phead == NULL){
return &SocketControl;
}else{
SocketControl.next = phead;
phead = &SocketControl;
return phead;
}
};
二、线程
Mainpro主函数的语音线程
void *voiceThread(void *datas)
{
struct inputcommander *voiceHandler;
int nread = 0;
voiceHandler = FindcommandByName("voice",pCommandHead);
if(voiceHandler == NULL){
printf("find voiceHandler error\n");
pthread_exit(NULL);
}else{
if( voiceHandler->Init(voiceHandler) < 0){
printf("voice init error\n");
pthread_exit(NULL);
}else{
printf("%s init success\n",voiceHandler->commandName);
}
while(1){
nread = voiceHandler->getcommand(voiceHandler);
if(nread == 0){
printf("voice nothing\n");
}else{
printf("control:%s\n",voiceHandler->command);
memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
}
}
}
}
int main()
{
//3.1语音线程
pthread_create(&voice_thread,NULL,voiceThread,NULL);
pthread_join(voice_thread,NULL);
}
Mainpro主函数的网络线程
void *read_t (void *datas)
{
int nread = 0;
memset(socketHandler->command,'\0',sizeof(socketHandler->command));
nread = read(c_fd,socketHandler->command,sizeof(socketHandler->command));
if(nread == -1){
perror("read");
}else if(nread > 0){
printf("get byte:%d,message:%s\n",nread,socketHandler->command);
}else{
printf("client quit\n");
}
}
void* socketThread(void *datas)
{
int nread = 0;
pthread_t readt;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int len = sizeof(struct sockaddr_in);
socketHandler = FindcommandByName("socket",pCommandHead);
printf("%s init success\n",socketHandler->commandName);
if(socketHandler == NULL){
printf("find socketHandler error\n");
pthread_exit(NULL);
}
socketHandler->Init(socketHandler);
while(1){
c_fd = accept(socketHandler->sfd,(struct sockaddr *)&c_addr,&len);
pthread_create(&readt,NULL,read_t,NULL);
}
}
int main()
{
//3.2socket线程
pthread_create(&socket_thread,NULL,socketThread,NULL);
pthread_join(socket_thread,NULL);
}
三、编译运行
网络测试
语音串口接收
(接收有误差,猜测可能是线程互相竞争导致的)