1.76传奇私服

PROT_READ|PROT_WRITE

源程序下载         此程序包含了共享内存区、多进程程序设计,是初学linux程序设计者的比较好的一个例程。         使用方法:将此com.c文件拷贝到根目录,执行gcc -o com com.c,然后执行./com,死一般的沉寂里,就能看到相应的输出信息了。 /**************com.c************/ #include <stdio.h>
#include <unistd.h> #include <stdlib.h> #include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/ioctl.h> #include <sys/mman.h>
#include <sys/time.h> #define DEBUG 1   //1:打印调事信息  0:不打印 typedef struct

 int share_file_fd;
 int option;
 char cmd[20];
}Share_Memory; int Com_Inital(int Port,int Speed);   //串口初始化函数
int Com_Exit(int Fd,int Port);  //串口退出函数 int main()
{
 //定义共享内存区 
 Share_Memory *share_zone = (Share_Memory*)mmap(NULL,sizeof(Share_Memory),PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,由教师自己设定目标、规则、方法、玩中学,0);      
 //在创建子进程之前打开串口,这样父子进程都可疑使用COM0_fd对串口进行操作
 int COM0_fd = Com_Inital(0,96);
 char *com_buffer = (char *)malloc(sizeof(char)*100);
 pid_t pid = fork();
 //子进程
 if(pid == 0)  
 {
  //延时1s,等待父进程先执行
  sleep(1);  
  while(1)
  {
   switch((*share_zone).option)
           {
            case 0:
             printf("COM has no data ");
             int num=1;
             sprintf(com_buffer,"hello,please send data,i am waiting! %d",num);
             write(COM0_fd,com_buffer,strlen(com_buffer));
             usleep(500000);     //延时0.5s
             break;
            case 1:
             printf("COM has data ");
             read(COM0_fd,com_buffer,100);
             (*share_zone).option = 0;  //显示完串口有数据后将标志位清零
             usleep(500000);
             break;
            default:
             break;
           }
  }
 }
 //父进程:不断检查串口有无数据输入,如果有则(*share_zone).option=1;否则(*share_zone).option=0
 else if(pid > 0)
 {
  int ret;
  struct timeval timeout;
  fd_set readfd;
  while(1)
  {
   //定时1s
   timeout.tv_sec=1;   //秒
         timeout.tv_usec=0;  //毫秒
         FD_ZERO(&readfd);
         FD_SET(COM0_fd,&readfd);
         // 查询串口
         ret=select(COM0_fd+1,&readfd,NULL,NULL,&timeout);  
         switch(ret)
           {
              case 0:      //等待1s的时间但串口还是没有数据
                (*share_zone).option=0;
                 break;
                default:      //串口有数据
                 (*share_zone).option=1;
                 break;
            } 
        }
 }
 Com_Exit(COM0_fd,0);  //永远执行不到,可以删除
 return 0;
} int Com_Inital(int Port,int Speed)
{
 int Fd;
 struct termios newtio,oldtio;
 char Dev_Driver[3][20]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
 Fd=open(Dev_Driver[Port],O_RDWR | O_NOCTTY);
 if(Fd==-1)
 {
  #if DEBUG
   printf("**Error : Can't Open COM%d !** ",Port);
  #endif
  return -1;
 }
 if(fcntl(Fd,F_SETFL,0)<0)   //设置读写的阻塞状态
 {
  #if DEBUG
   printf("**Fcntl failed !** ");
  #endif
  return -1;
 }
 if(isatty(STDIN_FILENO)==0)  //检查串口是否已被占
 {
  printf("Standard input is not a terminal device ");
  #if DEBUG
   printf("**Standard input COM%d is not a terminal device!** ",Port);
  #endif 
 }
 #if DEBUG
  printf("**Open COM%d successfull!** ",Port);
 #endif  if ( tcgetattr(Fd, &oldtio) ,传世私服!= 0)
 {
  #if DEBUG
   printf("**Setup COM%d error !** ",Port);
  #endif
  return -1;
 }
 bzero(&newtio, sizeof(newtio));
 newtio.c_cflag |= CLOCAL | CREAD;
 newtio.c_cflag &= ~CSIZE;
 newtio.c_cflag |= CS8;  
 newtio.c_cflag &= ~PARENB;
 newtio.c_cflag &= ~CSTOPB;
 switch(Speed)
 {
  case 24:
   cfsetispeed(&newtio, B2400);
   cfsetospeed(&newtio,相守着你爱的那个谁, B2400);
   break;
  case 48:
   cfsetispeed(&newtio, B4800);
   cfsetospeed(&newtio, B4800);
   break;
  case 96:
   cfsetispeed(&newtio, B9600);
   cfsetospeed(&newtio,传奇世界私服, B9600);
   break;
  case 1152:
   cfsetispeed(&newtio, B115200);
   cfsetospeed(&newtio, B115200);
   break;
  default:
   break;
 }
 newtio.c_cc[VTIME] = 0;
 newtio.c_cc[VMIN] = 0;
 tcflush(Fd,TCIFLUSH);
 if((tcsetattr(Fd, TCSANOW, &newtio)) != 0)
 {
  #if DEBUG
   printf("**Setup COM%d error !** ",Port);
  #endif
  return -1;
 }
 #if DEBUG
  printf("**COM%d Configure Successfull!** ",Port);
 #endif
 return Fd;
} int Com_Exit(int Fd,int Port)
{
 if(close(Fd)!=0)
 { 
  #if DEBUG
   printf("**COM%d Exit Error!** ",Port);
  #endif 
  return -1;           
 }
 #if DEBUG
  printf("**COM%d Exit Successfull!** ",Port);
 #endif
 return 0;
}

作者:admin 日期:11/10/18 22:35 人气:

上一篇:有 组织地在管理疏松的低档商业城
下一篇:每个人都有属于自己的方向