📄 nonblockreaddemo.c
字号:
/* nonBlockReadDemo.c - This is an example of doing a nonBlocking read on a port in raw mode*/ /* modification history -------------------- 01a,23dec99,cmf written. */ /* DESCRIPTION: This library is provided by Wind River Systems Customer Support strictly as an educational example. It is unsupported and provided as-is. This is an example of doing a nonBlocking read on a port in raw mode. The first read blocks and waits for a character. It then checks to see if additional characters have been sent as a result of pressing a special key such as <- (left arrow) If so, a special case is invoked to handle this (printf in this case) What to do with the data is left up to the customer. */ /* includes */ #include <vxWorks.h> #include <ioLib.h> #include <fioLib.h> #include <stdio.h> #include <stdioLib.h> #include <logLib.h> #include <errnoLib.h> #include <taskLib.h> /* defines */ #define DELAY_BETWEEN_WRITES 100 #define DELAY_BETWEEN_READS 10 #define NUMBER_OF_WRITES 15 #define NUMBER_OF_READS 25 /*globals*/ int fd1, fd2=0; SEM_ID syncSem; /* standard string should be about */ #define READ_BUFFER_SIZE 4 void readPort () { int j, bytesRecvd, bytesRead=0; char * tPtr; char * inputString; char inputChar; inputString = (char *) malloc(READ_BUFFER_SIZE); /* clear inputString */ tPtr = inputString; for (j=0; j < READ_BUFFER_SIZE ;j++) { *tPtr = '\0'; tPtr++; } /* block waiting for input one character - standard string should * one charachter. READ_BUFFER_SIZE allocated in case of special * characters */ bytesRead = fioRdString (fd1,(char *) inputString,1); taskDelay(10); /* do any string processing here */ if (*inputString == 0x1b) { /* check if more than one char was sent */ ioctl(fd1, FIONREAD, &bytesRecvd); if (bytesRecvd == 0) printf("read: esc\n"); else { for (j=0;j<bytesRecvd;j++) { bytesRead = fioRdString (fd1,(char *) inputString,1); inputString ++; } printf("read: arrow\n"); } } else printf("read: %s\n", inputString); taskDelay (DELAY_BETWEEN_READS); /* if larger - flush buffer, discard extra bytes and clear string */ /* if (bytesRecvd !=0) { ioctl(fd1, FIORFLUSH, 0); tPtr = inputString; for (j=0; j < READ_BUFFER_SIZE;j++) { *tPtr = '\0'; tPtr++; } */ } void readLoop () { int i= 0; semTake (syncSem, WAIT_FOREVER); while (i < NUMBER_OF_READS) { readPort(); i++; } } /*readLoop*/ void writePort (int i) { char * escString = "\x1b"; char * arrowString = "\x1b\x5b\x44"; char * letterString = "a"; int nbytes = 0; if ((i % 7) == 0) { nbytes = write (fd2, arrowString, strlen (arrowString)); printf("write: arrow %s\n",arrowString); } else if ((i % 5) == 0) { nbytes = write (fd2, escString, strlen (escString)); printf("write: esc\n"); } else { nbytes = write (fd2, letterString, strlen (letterString)); printf("write: %s\n",letterString); (*letterString)++; /* pg 196 K&R */ } taskDelay(DELAY_BETWEEN_WRITES); } /* writePort */ void writeLoop () { int i= 0; syncSem = semBCreate (SEM_Q_PRIORITY, SEM_FULL); while (i < NUMBER_OF_WRITES) { writePort(i); if (i==0) semGive (syncSem); i++; } }/* writeLoop */ int nonBlockReadRawInit () { int options=0; int status=0; /* open descriptors on serial 2 and 3 */ printf ("Starting Read Test\n"); printf ("Opening File Descriptors\n"); fd1=open("/tyCo/1",O_RDWR,0); if (fd1==NULL) { printf("Opening tyCo 1 failed\n"); return (ERROR); } fd2=open("/tyCo/2",O_RDWR,0); if (fd2==NULL) { printf("Opening tyCo 2 failed\n"); return (ERROR); } printf ("Setting to raw mode\n"); status = ioctl (fd1, FIOSETOPTIONS, OPT_RAW); if (status==ERROR) { printf("OPT_RAW on tyCo 1 failed\n"); return (ERROR); } status = ioctl (fd2, FIOSETOPTIONS, OPT_RAW); if (status==ERROR) { printf("OPT_RAW on tyCo 2 failed\n"); return (ERROR); } printf ("Flushing read and write ports\n"); ioctl(fd1, FIORFLUSH, 0); ioctl(fd2, FIORFLUSH, 0); printf ("initialization done\n"); return (OK); }/*nonBlockReadRawInit*/ int nonBlockReadRawStart () { if (nonBlockReadRawInit()==ERROR) { printf ("initialization failed\n"); return (ERROR); } taskSpawn ("tWriteLoop", 100, 0, 10000, (FUNCPTR) writeLoop, 0,0,0,0,0,0,0,0,0,0); taskSpawn ("tReadLoop", 101, 0, 10000, (FUNCPTR) readLoop, 0,0,0,0,0,0,0,0,0,0); return (OK); }/*nonBlockReadRawStart*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -