#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "string.h"
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <sys/un.h>
#include <sys/ipc.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/msg.h>
#include <unistd.h>
// close a0 01 00 a1
// open a0 01 01 a2
static int mcu_fd;
static int relay_fd;
bool get_camera = false;
bool get_lan = false;
bool ubuntu_start = false;
bool get_wlan0 = false;
static int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
static int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
pthread_mutex_t log_mutex;
void Log(char* buf)
{
char date[25] = {0};
time_t now;
struct tm *timenow;
time(&now);
timenow = localtime(&now);
sprintf(date,"%04d-%02d-%02d %02d:%02d:%02d",
timenow->tm_year + 1900,timenow->tm_mon + 1,timenow->tm_mday,
timenow->tm_hour,timenow->tm_min,timenow->tm_sec);
char save_buf[4096] = {0};
char file_name[32] = {0};
sprintf(file_name,"./log/tbox_%04d-%02d-%02d.log",timenow->tm_year + 1900,timenow->tm_mon + 1,timenow->tm_mday);
sprintf(save_buf,"[%s]:%s\n",date,buf);
printf("%s",save_buf);
pthread_mutex_lock(&log_mutex);
FILE *fd = fopen(file_name,"a");
if(fd == NULL)
{
pthread_mutex_unlock(&log_mutex);
return;
}
fwrite(save_buf,strlen(save_buf),1,fd);
fclose(fd);
pthread_mutex_unlock(&log_mutex);
}
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR | O_NOCTTY ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(false);
}
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
options.c_cflag &= ~CSIZE;
options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size/n"); return (false);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity/n");
return (false);
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits/n");
return (false);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; // 500ms time out
options.c_cc[VMIN] = 0; //Update the options and do it NOW
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (false);
}
return (true);
}
ssize_t tread(int fd, void *buf, size_t nbytes)
{
int nfds;
fd_set readfds;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000;
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
nfds = select(fd+1, &readfds, NULL, NULL, &tv);
if (nfds <= 0) {
if (nfds == 0)
errno = ETIME;
return(-1);
}
return(read(fd, buf, nbytes));
}
int mcu_send(unsigned char* buf)
{
int nread;
Log(buf);
nread = write(mcu_fd, buf ,strlen(buf)+1);
return nread;
}
int relay_open()
{
int nread;
Log("power on");
unsigned char buf[4] = {0xa0,0x01,0x01,0xa2};
nread = write(relay_fd, buf ,4);
return nread;
}
int relay_close()
{
int nread;
Log("power off\n\n\n");
unsigned char buf[4] = {0xa0,0x01,0x00,0xa1};
nread = write(relay_fd, buf ,4);
return nread;
}
void uart_thread(void)
{
char buff[4096] = {0};
char pbuf[1024] = {0};
int nread = 0;
while (1)
{
memset(buff,0,sizeof(buff));
memset(pbuf,0,sizeof(pbuf));
memcpy(pbuf,buff,sizeof(pbuf)-1);
if((nread = tread(mcu_fd, buff, 512))>0)
{
fflush(stdout);
if(strstr(buff,"flags="))
{
//Log(buff);
Log("get wlan0");
get_wlan0 = true;
}
}
}
close(mcu_fd);
return;
}
int main()
{
int ret;
pthread_t uart_thread_t;
char *dev = "/dev/ttyUSB0";
if(pthread_mutex_init(&log_mutex,NULL) != 0 )
{
Log("Init metux error.");
}
mcu_fd = OpenDev(dev);
if(mcu_fd < 0)
{
return 0;
}
set_speed(mcu_fd,115200);
if (set_Parity(mcu_fd,8,1,'N') == false)
{
printf("Set Parity Error/n");
}
relay_fd = OpenDev("/dev/ttyUSB1");
if(relay_fd < 0)
{
return 0;
}
set_speed(relay_fd,9600);
if (set_Parity(relay_fd,8,1,'N') == false)
{
printf("Set Parity Error/n");
}
ret = pthread_create(&uart_thread_t,NULL,(void*)uart_thread,NULL);
if(ret != 0)
{
printf("Uart thread create failed!\n");
}
while(1)
{
relay_open();
int count = 0;
sleep(40);
int j = 0;
for(;j<3;j++){
mcu_send("kuka\r");
sleep(1);
mcu_send("123\r");
mcu_send("\r\r");
sleep(5);
}
while(1)
{
sleep(1);
mcu_send("ifconfig | grep wlan0\r");
mcu_send("ifconfig | grep wlan0\r");
sleep(3);
if(get_wlan0 == true)
{
//mcu_send("echo \"123\" | sudo -S reboot\r");
get_wlan0 = false;
relay_close();
usleep(100000);
ubuntu_start = false;
break;
}else{
Log("Get wlan failed!");
}
}
}
return 0;
}
Linux C语言串口输出
于 2022-02-15 16:16:44 首次发布