#include "prot.h"
extern pthread_mutex_t M0_Mutex;
extern pthread_t thread_M0_id;
extern tData Server_Msg;
char buf[BUFSIZE]={0x00};
char data[BUFSIZE]={0x00};
int m0_fd = 0;
int UART0_OPEN()
{
int fd;
fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd < 0)
{
perror("open error");
return -1;
}
printf("open ok\r\n");
if(fcntl(fd,F_SETFL,0)<0)
{
printf("fcntl faile \n");
return -1;
}
else
{
printf("fcntl=%d\r\n",fcntl(fd,F_SETFL,FNDELAY));
}
if(0 == isatty(STDIN_FILENO))
{
printf("input is not a terminal device\r\n");
return -1;
}
else
{
printf("ttyUSB0 success!\r\n");
}
printf("fd->open=%d\r\n",fd);
return fd;
}
int UART0_SET(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
int i;
int status;
int speed_arr[]={ B115200, B19200, B9600 ,B4800 , B2400 ,B1200 ,B300};
int name_arr[]={115200,19200,9600,4800,2400,1200,300};
struct termios options;
//得到与fd指向对象的相关参数,并将它们保存于options,该函数,还可以测试配置是否正确
if(tcgetattr(fd,&options) !=0 )
{
perror("setupserial 1");
return -1;
}
//speed
for(i=0;i<sizeof(speed_arr)/sizeof(int);i++)
{
if(speed==name_arr[i])
{
cfsetispeed(&options,speed_arr[i]);
cfsetospeed(&options,speed_arr[i]);
}
}
/* 修改控制模式,保证程序不会占用串口 */
options.c_cflag |= CLOCAL;
/* 修改控制模式,使得能够从串口中读取输入数据 */
options.c_cflag |= CREAD;
/* 设置数据流控制 */
switch(flow_ctrl)
{
case 0://不使用流控制
options.c_cflag &= ~CRTSCTS;
break;
case 1://使用硬件流控制
options.c_cflag |= CRTSCTS;
break;
case 2:
options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
options.c_cflag &= ~CSIZE; /* 设置数据位 */
switch (databits)
{
case 5:
options.c_cflag |= CS5;
break;
case 6:
options.c_cflag |= CS6;
break;
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size/n");
return -1;
}
//设置校验位
switch(parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD|PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 's':
case 'S':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return -1;
}
switch(stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"stop bitts error\n");
return -1;
}
//修改输出模式,原始数据输出
options.c_oflag &= ~OPOST;
/* 设置等待时间和最小接收字符 */
options.c_cc[VTIME] = 1; /* 读取一个字符等待1*(1/10)s */
options.c_cc[VMIN] = 1; /* 读取字符的最少个数为1 */
// 如果发生数据溢出,接收数据,但是不再读取
tcflush(fd,TCIFLUSH);
//自己设置(write写不进去 是因为我们要回车或者换行)
options.c_lflag &= ~(ICANON |ECHO |ECHOE |ISIG);
options.c_iflag &= ~(ICANON |ECHO |ISIG);
options.c_iflag &= ~(INLCR |ICRNL |IGNCR);
options.c_oflag &= ~(ONLCR |OCRNL);
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("com set error");
return -1;
}
printf("set ok\r\n");
return 0;
}
int UART0_INIT(int fd,int ret)
{
int d=fd;
if(ret == -1)
{
return -1;
}
else
{
printf("INIT OK\r\n");
return 0;
}
}
void UART0_Close(int fd )
{
close(fd);
}
int UART0_Recv(int fd, char *rcv_buf,int data_len)
{
int len,fs_sel;
fd_set fs_read;
struct timeval time;
FD_ZERO(&fs_read);
FD_SET(fd,&fs_read);
time.tv_sec = 10;
time.tv_usec = 0;
/* 使用select实现串口的多路通信 */
fs_sel = select(fd+1,&fs_read,NULL,NULL,&time);
if(fs_sel)
{
len = read(fd,rcv_buf,data_len);
return len;
}
else
{
return -1;
}
}
int UART0_tmp1_Recv(int fd)
{
//接受消息温度
int tmp1;
int ret =UART0_Recv(fd, data, BUFSIZE);
if(ret < 28)
{
UART0_tmp1_Recv(fd);
}
tmp1=data[5];
printf("tmp1->%d \r\n",tmp1);
return tmp1;
}
int UART0_tmp2_Recv(int fd)
{
//接受消息湿度
int tmp2;
int ret =UART0_Recv(fd, data, BUFSIZE);
if(ret < 28)
{
UART0_tmp1_Recv(fd);
}
tmp2=data[7];
printf("tmp2->%d \r\n",tmp2);
return tmp2;
}
int UART0_tmp3_Recv(int fd)
{
//接受消息光照
int tmp3;
int ret =UART0_Recv(fd, data, BUFSIZE);
if(ret < 28)
{
UART0_tmp1_Recv(fd);
}
tmp3=data[20];
printf("tmp3->%d \r\n",tmp3);
return tmp3;
}
//灯 亮
int UART0_tmp1_up(int fd)
{
int ret1 =0;
if(fd < 0)
{
perror("i have not temperature");
return -1;
}
buf[0]=0xdd;
buf[1]=0x05;
buf[2]=0x24;
buf[3]=0x04;
buf[4]=0x00;
for(;;)
{
ret1=write(fd,buf,BUFSIZE);
if(ret1 == BUFSIZE)
{
break;
}
}
if(ret1 <0 )
{
perror("write error");
return -1;
}
printf("tmp1 open ok\r\n");
return 0;
}
//灯灭
int UART0_tmp1_down(int fd)
{
int ret;
if (fd == 0)
{
perror("i have not temperature");
return -1;
}
buf[0]=0xdd;
buf[1]=0x05;
buf[2]=0x24;
buf[3]=0x04;
buf[4]=0x01;
for(;;)
{
ret=write(fd,buf,BUFSIZE);
if(ret == BUFSIZE)
{
break;
}
}
if(ret <0 )
{
perror("write error");
return -1;
}
printf("tmp1down ok\r\n");
return 0;
}
//蜂鸣器开
int UART0_tmp2_up(int fd)
{
int ret;
if(fd == 0)
{
perror("i have not temperature");
return -1;
}
buf[0]=0xdd;
buf[1]=0x05;
buf[2]=0x24;
buf[3]=0x04;
buf[4]=0x02;
for(;;)
{
ret=write(fd,buf,BUFSIZE);
if(ret == BUFSIZE)
{
break;
}
}
if(ret <0 )
{
perror("write error");
return -1;
}
printf("tmp2 open ok\r\n");
return 0;
}
//蜂鸣器关闭
int UART0_tmp2_down(int fd)
{
int ret;
if(fd == 0)
{
perror("i have not temperature");
return -1;
}
buf[0]=0xdd;
buf[1]=0x05;
buf[2]=0x24;
buf[3]=0x04;
buf[4]=0x03;
for(;;)
{
ret=write(fd,buf,BUFSIZE);
if(ret == BUFSIZE)
{
break;
}
}
if(ret <0 )
{
perror("write error");
return -1;
}
printf("tmp2 down ok\r\n");
return 0;
}
//风扇 打开
int UART0_tmp3_up(int fd)
{
int ret;
if(fd == 0)
{
perror("i have not temperature");
return -1;
}
buf[0]=0xdd;
buf[1]=0x05;
buf[2]=0x24;
buf[3]=0x04;
buf[4]=0x04;
for(;;)
{
ret=write(fd,buf,BUFSIZE);
if(ret == BUFSIZE)
{
break;
}
}
if(ret <0 )
{
perror("write error");
return -1;
}
printf("tmp3 open ok\r\n");
return 0;
}
int UART0_tmp3_down(int fd)
{
int ret;
if (fd == 0)
{
perror("i have not temperature");
return -1;
}
buf[0]=0xdd;
buf[1]=0x05;
buf[2]=0x24;
buf[3]=0x04;
buf[4]=0x08;
for(;;)
{
ret=write(fd,buf,BUFSIZE);
if(ret == BUFSIZE)
{
break;
}
}
if(ret < 0 )
{
perror("write error");
return -1;
}
printf("tmp3 down\r\n");
return 0;
}
void *M0_MAIN()
//int main()
{
//串口配置 及 初始化
int fd =UART0_OPEN();
int ret=UART0_SET(fd,115200,0,8,1,'N');
UART0_INIT(fd,ret);
m0_fd = fd;
while(1)
{
sleep(1);
pthread_mutex_lock(&M0_Mutex);
Server_Msg.tmp_1=UART0_tmp1_Recv(fd);
Server_Msg.tmp_2=UART0_tmp2_Recv(fd);
Server_Msg.tmp_3=UART0_tmp3_Recv(fd);
printf("---------%d %d %d\r\n",Server_Msg.tmp_1,Server_Msg.tmp_2,Server_Msg.tmp_3);
pthread_mutex_unlock(&M0_Mutex);
}
UART0_Close(fd);
return (void *)0;
}
void M0_SET(char *tmp,int Set_Buf_Size)
{
//判断服务器发来命令
if(NULL == tmp || Set_Buf_Size < 3)
{
printf("m0_set buf error\r\n");
return ;
}
char p0[3]={0}; //开灯
strcat(p0,"A2");
strcat(p0,"0");
char p1[3]={0}; //关灯
strcat(p1,"A2");
strcat(p1,"1");
char p2[3]={