📄 pm511at9200.c
字号:
/*此版本完成了基本功能,包括DA输出,AD定时采集060821:开始添加扩展通道功能*/#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <sys/shm.h>#include <sys/ipc.h>#include <sys/io.h>#include <time.h>#include <sys/time.h>#include <fcntl.h>#include <errno.h>//#define debugint zt_io_fd;void delaytimes(int fd,int times){ while(times-->0)inport(fd,10);}int adproc(char* shmcommand,struct sockaddr_in *client_address){ int fd; int address_len; struct sockaddr_in address; unsigned short io=0; fd_set fdset; int i=0;long odata,idata;short base=0x100; struct timeval timeout; unsigned long uldata[3]; unsigned long max,min,v; long retv; char line[256];// void* sendtodata=malloc(4200*sizeof(unsigned short));// unsigned long* times=sendtodata; unsigned short* addata=malloc(409608*sizeof(unsigned short)); unsigned char* adch,*triger,*eendch;unsigned short *g0; unsigned long* adfreq; unsigned long ctrlw=0; int zerotimes=0; fd = socket(AF_INET, SOCK_STREAM, 0); //SOCK_DGRAM bzero(&address, sizeof(address)); address.sin_family = AF_INET; address.sin_addr.s_addr = client_address->sin_addr.s_addr; address.sin_port = htons(1234); address_len = sizeof(address); while(connect(fd,(void*)&address,address_len)){ printf("connect err:%s\n",strerror(errno)); }; adch=(char*)(shmcommand+20);adfreq=(long*)(shmcommand+24); g0=(short*)(shmcommand+28); triger=(char*)(shmcommand+36); eendch=shmcommand+37; printf("\n\nadch,%x,g0,%x,adfreq:%ld,fd:%d\n\n",*adch,*g0,*adfreq,zt_io_fd);// exit(0);//////////////////////////////////////////////////////////////////////////// outportb(1000,1); outportb(base+1,0); outport(base,*adch); outport(base+2,*adfreq); printf("adch:%x,freq:%x,fifo:%x\n",*adch,*adfreq,inport(base+6)); outportb(base+1,0xc2); shmcommand[1]=0;zerotimes=0; while(shmcommand[0]){ if((retv=read(zt_io_fd,addata,819200))<0){ printf("read 8192 false!%ld\n",retv); printf("AI\n"); break; } if(retv>0){ if(send(fd, addata, retv*2, 0)<retv*2){ printf("send AI error,%s\n",strerror(errno)); break; } max=0;min=65536;v=0; for(i=0;i<(retv<4096?retv:4096);i++){ v+=addata[i]; max=addata[i]<max?max:addata[i]; min=addata[i]>min?min:addata[i]; } v/=4096; printf("%6x,%6x,%6x,%6x,%7ld\n",v,(max-min),max,min,retv); }else if(retv<0){ printf("adproc:%d\n",retv); break; } } outport(base+1,0); shmcommand[1]=1; outport(1000,0); printf("adproc end\n"); return 0;}void singlead(char* line){ short base=0x100,v;char* q[1]; int adch=0,adfreq=79,i=0; adch=strtol(line+12,q,16); outportb(base+1,0); outportb(base,adch); outport(base+2,adfreq); outportb(base+1,0x82); while((inport(base+6)&2)); for(i=0;i<adch+1;i++){ v=inport(base+4)&0xfff;// printf("%x\n",v); sprintf(line+11+i*4,"%3xq",v); } printf("adch:%x,freq:%x,fifo:%x,v:%x\n",adch,adfreq,inport(base+6),v); printf("singlead:%s\n",line); outportb(base+1,0x0);}int main(){ long ps2d=0,i; int fd,n; struct sockaddr_in address; struct sockaddr_in* client_address; int address_len; char* shmcommand,line[4096]; unsigned char* adch,*triger;unsigned short*g;long *adfreq; unsigned char* eendch; int shm_id; key_t shmkey; FILE* npf;uint16_t np[2]; int len; unsigned long *addata1num,*addata2num; int* zt_io_fdp; long odata,idata;int base=0x100; shmkey=ftok(".",'a'); shm_id=shmget(shmkey,40,IPC_CREAT); shmcommand=shmat(shm_id,0,0); bzero(shmcommand,40); adch=(char*)(shmcommand+20);adfreq=(long*)(shmcommand+24); g=(unsigned short*)(shmcommand+28);zt_io_fdp=(int*)(shmcommand+32); triger=(char*)(shmcommand+36);eendch=shmcommand+37; shmkey=ftok(".",'b'); shm_id=shmget(shmkey,sizeof(struct sockaddr_in),IPC_CREAT); client_address=shmat(shm_id,0,0);/*get net port*/ np[0]=1234; np[1]=1235; /*init net*/ fd = socket(AF_INET, SOCK_DGRAM, 0); //SOCK_DGRAM// memcpy(shmcommand+16,&fd,2); bzero(&address, sizeof(address)); address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(INADDR_ANY); printf("%d\n",np); address.sin_port = htons(np[1]); address_len = sizeof(address); bind(fd, (struct sockaddr *)&address, address_len); printf("%x,%x,%x\n",fd,shmcommand[16],ntohs(address.sin_port)); zt_io_fd=open("/dev/zt_io-0",O_RDWR); printf("zt_io_fd:%d\n",zt_io_fd); if(zt_io_fd<0){printf("open device error\n");return 0;} outportb(0x400,30000); while(1) { socklen_t len = sizeof(*client_address); int n; struct timeval timeout; fd_set fdset; //喂狗 outportb(0x404,1); timeout.tv_sec = 1; timeout.tv_usec = 0; FD_ZERO(&fdset); FD_CLR(fd, &fdset); FD_SET(fd, &fdset); if((select(fd+1, &fdset, NULL, NULL, &timeout))< 0){ printf("cannot select.\n"); fflush(stdout); } if(FD_ISSET(fd, &fdset)){/**************************************************************/ n = recvfrom(fd, line, 4096, 0, (struct sockaddr *)client_address, &len); if(!strncmp(line,"CMDSTARTAD",10)){ int retnum=0;char* q[1]; *adch=strtol(line+10,q,16); *adfreq=strtoll(line+13,q,16); printf("\n\nadch,adfreq,%10x,%10ld\n\n\n",*adch,*adfreq); shmcommand[0]=1; if(fork()==0){ adproc(shmcommand,client_address); } else { sprintf(line,"CMDSTARTAD"); if((retnum=sendto(fd, line, strlen(line)+1, 0, (struct sockaddr *)client_address, len))<0) printf("%s\n",line); } }else if(!strncmp(line,"CMDSTOPAD",9)){ shmcommand[0]=0; while(!shmcommand[1]); sendto(fd, line, 9, 0, (struct sockaddr *)client_address, len); }else if(!strncmp(line,"CMDSINGLEAD",11)){ if(shmcommand[0]){ sprintf(line,"CMDADSTART"); sendto(fd, line, strlen(line), 0, (struct sockaddr *)client_address, len); }else{ singlead(line); sendto(fd, line, strlen(line), 0, (struct sockaddr *)client_address, len); } }else if(!strncmp(line,"CMDWRITEIO",10)){ line[14]='\0'; odata=strtol(line+10,NULL,16); ztpm511write55(base,(odata>>8)&0xf,odata&0xff); sendto(fd, line, 14, 0, (struct sockaddr *)client_address, len); }else if(!strncmp(line,"CMDREADIO",9)){ line[10]='\0'; odata=strtol(line+9,NULL,16); idata=ztpm511read55(base,odata&0xf); snprintf(line+10,3,"%2x",idata); sendto(fd, line, 13, 0, (struct sockaddr *)client_address, len); }else if(!strncmp(line,"CMDSETIO",8)){ line[11]='\0'; odata=strtol(line+8,NULL,16); idata=ztpm511init55(base,odata&0xff); sendto(fd, line, 11, 0, (struct sockaddr *)client_address, len); }else if(!strncmp(line,"CMDSETC",7)){ line[9]='\0'; odata=strtol(line+7,NULL,16); ztpm511initc(base,odata&0xff); sendto(fd, line, 10, 0, (struct sockaddr *)client_address, len); }else if(!strncmp(line,"CMDWRITEC",9)){ line[14]='\0'; odata=strtol(line+9,NULL,16); ztpm511writec(base,(odata>>16)&0xf,odata&0xffff); sendto(fd, line, 15, 0, (struct sockaddr *)client_address, len); }else if(!strncmp(line,"CMDREADC",8)){ line[9]='\0';// printf("%s\n",line); odata=strtol(line+8,NULL,16); idata=ztpm511readc(base,odata&0xf); snprintf(line+9,5,"%4x",idata); sendto(fd, line, 14, 0, (struct sockaddr *)client_address, len); }else if(!strncmp(line,"CMDDA",5)){ line[9]='\0'; odata=strtol(line+5,NULL,16); ztpm511da(base,(odata>>12)&0xf,odata&0xfff); sendto(fd, line, 11, 0, (struct sockaddr *)client_address, len);// printf("CMDDA:%x\n",odata); }else if(!strncmp(line,"reboot",6)){ shmcommand[1]|=2; sync(); sendto(fd, line, 6, 0, (struct sockaddr *)&client_address, len); outportb(0x400,30); } } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -