📄 signal.c
字号:
//*************************************************************************// // Copyright (c) 2001 IAG Software Team, //// Beijing R&D Center // // Advantech Co., Ltd.//// User program for Linux comedi device driver//// This is An Unpublished Work Containing Confidential And Proprietary// Information Which Is The Property Of Advantech Automation Corp.//// Any Disclosure, Use, Or Reproduction, Without Written Authorization From// Advantech Automation Corp., Is Strictly Prohibit.//////*************************************************************************// Program : signal.c// Revision: 1.0 // Author : Edwin du // Date : 12/30/02 //// Description: Catch di interrupt signal of pci1761 card. // This is just an example for DI0 interrupt prompt,// you can also add your own interrupt handler.// //-------------------------------------------------------------------------#include <stdio.h>#include <stdlib.h>#include <comedilib.h>#include <fcntl.h>#include <unistd.h>#include <sys/ioctl.h>#include <errno.h>#include <getopt.h>#include <ctype.h>#include <math.h>#include <sys/time.h>#include <string.h>#include <signal.h>#include <sys/types.h>#define N_SAMPLE 2#define EVENT_DI0 40#define EVENT_DI1 41#define EVENT_DI2 42#define EVENT_DI3 43#define EVENT_DI4 44#define EVENT_DI5 45#define EVENT_DI6 46#define EVENT_DI7 47typedef unsigned short USHORT;typedef unsigned long ULONG;void DI0Handler(int signum);void DI1Handler(int signum);void DI2Handler(int signum);void DI3Handler(int signum);void DI4Handler(int signum);void DI5Handler(int signum);void DI6Handler(int signum);void DI7Handler(int signum);ULONG AddIntHandler(int Event, void *Handler); char *filename="/dev/comedi0";int verbose_flag;comedi_t *device;int channel =1;int range = 0;int subdev = 15; // PCI1761 FAKE DI deviceint main(void){ lsampl_t data[N_SAMPLE]; int save_errno; int ret,n_channels; comedi_insn insn; char nKey; pid_t sendpid[2]; // Step1. prompt case information: printf("************** signal.c****************\n"); printf("\n Comedi driver test program\n\n"); printf("****************************************\n"); // Step2. open device file device = comedi_open(filename); if(!device){ printf("E: comedi_open(\"%s\"): %s\n",filename,strerror(errno)); return 1; } // Step3. clear buffer, fill in structures... memset(&insn,0,sizeof(insn)); memset(&data,0,sizeof(data)); n_channels = comedi_get_n_channels(device, subdev); insn.subdev = subdev; insn.insn = INSN_BITS; insn.n = N_SAMPLE; insn.data = data; // Step4. Tell the drvier of user case's pid, using comedi insn. sendpid[0] = getpid(); // get pid of itself data[0] = (lsampl_t)sendpid[0]; sendpid[1] = 2; // clear signal list on driver?? data[1] = 2; ret = comedi_do_insn(device,&insn); //write to device save_errno = errno; if(ret<0) { comedi_perror(filename); exit(0); } sendpid[1] = 1; // set pid to driver data[1] = 1; ret = comedi_do_insn(device,&insn); //write to device save_errno = errno; if(ret<0) { comedi_perror(filename); exit(0); } // Step5. Add Interrupt Handler AddIntHandler(EVENT_DI0, DI0Handler); AddIntHandler(EVENT_DI1, DI1Handler); AddIntHandler(EVENT_DI2, DI2Handler); AddIntHandler(EVENT_DI3, DI3Handler); AddIntHandler(EVENT_DI4, DI4Handler); AddIntHandler(EVENT_DI5, DI5Handler); AddIntHandler(EVENT_DI6, DI6Handler); AddIntHandler(EVENT_DI7, DI7Handler); // Step6. exit while pressing any key printf("\nInput single into DI0-7, let the system generates interrupt !!!"); printf("\nPress any 'q' to exit....\n"); while(1) { nKey = getchar(); if(nKey == 'q') break; } // Step6. Close device comedi_close(device); printf("complete input!\n"); return 1;}// Add a Event handler to the driver// Arguments:// Event: which event to be handled// it could be: EVENT_DI0// EVENT_DI1// EVENT_DI*// EVENT_DI7// Handler: the handler interface// it's format is: void Handler(int Event);//ULONG AddIntHandler(int Event, void *Handler){ struct sigaction new_action; new_action.sa_handler = Handler; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; sigaction(Event, &new_action, NULL);/* sendpid[0] = getpid(); sendpid[1] = 2; write(fd, sendpid, 2*sizeof(sendpid)); sendpid[1] = 1; write(fd, sendpid, 2*sizeof(sendpid));*/ return 0;}// DI0 interrupt handlervoid DI0Handler(int signum){ printf("Get DI0 interrupt!\n");}void DI1Handler(int signum){ printf("Get DI1 interrupt!\n");}void DI2Handler(int signum){ printf("Get DI2 interrupt!\n");}void DI3Handler(int signum){ printf("Get DI3 interrupt!\n");}void DI4Handler(int signum){ printf("Get DI4 interrupt!\n");}void DI5Handler(int signum){ printf("Get DI5 interrupt!\n");}void DI6Handler(int signum){ printf("Get DI6 interrupt!\n");}void DI7Handler(int signum){ printf("Get DI7 interrupt!\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -