📄 main.c
字号:
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include <stdio.h> /*fputs()*/
#include <stdlib.h> /*exit()*/
#include <string.h> /*memset(),atoi()*/
#include <errno.h> /*strerror()*/
#include <netinet/in.h> /*struct sockaddr_in,htons(),inet_addr()*/
#include <sys/types.h> /*socket()*/
#include <sys/socket.h> /*socket(),inet_addr()*/
#include <fcntl.h>
#include "main.h"
int main()
{
fd_set readfds;
struct timeval tv; //tv是记时器,可以精确到微秒
int maxfd,nfds=0;
int pid0 = -1,pid1 = -1;
int fdkeypipe = -1;
int ikeycode = 0;
int combuf[512]={0};
int iReadNum = 0;
//打开键盘管道,作为main和keyfunc交换数据的通道
fdkeypipe = open("/dev/keypipe",O_RDWR);
if (fdkeypipe<0)
{
printf("open keypipe fail!\n");
exit(1);
}
gfdcom = open("/dev/uart0",O_RDWR | O_NOCTTY | O_NDELAY);
if (gfdcom < 0)
{
close(fdkeypipe);
printf("open com fail!\n");
exit(1);
}
//创建进程1执行读键盘程序
pid1 = vfork();
if(pid1==0){
printf("ok\n");
execl("/bin/keyfunc","/bin/keyfunc",(char *)0);
perror("fork call err\n");
}
else if (pid1 < 0)
{
printf("Creat fork1 fail!\n");
exit(1);
}
printf("Start ...\n");
for(;;)
{
tv.tv_usec = 0;
tv.tv_sec=5;
FD_ZERO(&readfds);
maxfd = 0;
FD_SET(fdkeypipe,&readfds);
//FD_SET(gfdsock_sr, &readfds); //fd1是句柄,可以是串口,网口,管道等
FD_SET(gfdcom, &readfds); //fd1是句柄,可以是串口,网口,管道等
//maxfd = max(gfdsock_sr, maxfd);
maxfd = max(fdkeypipe, maxfd);
maxfd = max(gfdcom,maxfd);
nfds = select(maxfd+1, &readfds, NULL, NULL, &tv);
if(FD_ISSET(fdkeypipe, &readfds))
{
read(fdkeypipe,&ikeycode,sizeof(ikeycode));
printf("main ikeycode = %d\n",ikeycode);
}
if(FD_ISSET(gfdcom, &readfds))
{
printf("receive com\n");
iReadNum = read(gfdcom,combuf,sizeof(combuf));
write(gfdcom,combuf,iReadNum);
}
}
close(fdkeypipe);
close(gfdcom);
return 0;
}
int max(int num0,int num1)
{
if (num0 > num1 )
{
return num0;
}
else
{
return num1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -