/* Restore the app's timer */ (void)setitimer(ITIMER_REAL, &apptimer, NULL); timerflag = FALSE; } /* stop_read_timer */ #endif /* __linux__ */ int cygDeviceInputStream_readDeviceOneByteNC (JNIEnv *jenv, jobject jobj) { jclass jc; jclass ec; jfieldID jf; jint fd = -1; int rc; int dc; char buf[1]; jfieldID tmof; int tmo; int serrno; // Get the exception class. ec = (*jenv)->FindClass(jenv, "java/io/IOException"); assert(ec); // Get the file descriptor. jc = (*jenv)->GetObjectClass(jenv, jobj); assertexc(jc); jf = (*jenv)->GetFieldID(jenv, jc, "fd", "I"); assertexc(jf); fd = (*jenv)->GetIntField(jenv, jobj, jf); if (fd == -1) { (*jenv)->ThrowNew(jenv, ec, ""); } tmof = (*jenv)->GetFieldID(jenv, jc, "tmo", "I"); assert(tmof); tmo = (*jenv)->GetIntField(jenv, jobj, tmof); #ifdef QNX // Read data - QNX with timeout if (tmo <100 & tmo>0) { tmo=100; } dc=readcond(fd, buf, 1, 1, 0, tmo/100); //10th of a second instead of microSecs if (dc<1) { //dc=-1; //return fake error } #endif /* QNX */ #if defined(__linux__) || defined(__osx__) // Start the timer. timeoutOccurred = FALSE; if (tmo > 0) start_read_timer(tmo); // Read data. buf[0] = 0; dc = read(fd, buf, 1); serrno = errno; // Stop the timer. if (tmo > 0) stop_read_timer(); if (dc < 0 && !(serrno == EINTR || serrno == EAGAIN)) { (*jenv)->ThrowNew(jenv, ec, ""); } #endif /* __linux__ */ // If timeout had occurred, or if nil data was received, return -1. if (dc <= 0) return -1; return (int)(unsigned char)buf[0]; } /* cygDeviceInputStream_readDeviceOneByteNC */
void SerielleSchnittstelle::empfangeDaten(void* buf, ssize_t nbyte) { int fileDescriptor; // pthread_mutex_lock(&mutex); fileDescriptor = open(DEVICE, O_RDWR | O_NOCTTY); readcond(fileDescriptor, buf, nbyte, nbyte, 1, 0); close(fileDescriptor); // pthread_mutex_unlock(&mutex); }
int RS232_1::readMsg(char* rbuf) { int rc = readcond(fd, rbuf, sizeof(rbuf), 1, 0, 10); if(rc <= 0) { if(errno == EAGAIN || rc == 0){ #ifdef DEBUG_RS232 printf("Debug RS232_1: Timeout or EAGAIN\n"); #endif return 0; } else { printf("recieving from devfile1 failed\n"); return -1; } } return rc; }
/* Restore the app's timer */ (void)setitimer(ITIMER_REAL, &apptimer, NULL); timerflag = FALSE; } /* stop_read_timer */ #endif /* __linux__ */ int cygDeviceInputStream_readDeviceOneByteNC (JNIEnv *jenv, jobject jobj) { jclass jc; jclass ec; jfieldID jf; jint fd = -1; int rc; int dc; char buf[1]; jfieldID tmof; int tmo; int serrno; // Get the exception class. ec = (*jenv)->FindClass(jenv, "java/io/IOException"); assert(ec); // Get the file descriptor. jc = (*jenv)->GetObjectClass(jenv, jobj); assertexc(jc); jf = (*jenv)->GetFieldID(jenv, jc, "fd", "I"); assertexc(jf); fd = (*jenv)->GetIntField(jenv, jobj, jf); if (fd == -1) { (*jenv)->ThrowNew(jenv, ec, ""); } tmof = (*jenv)->GetFieldID(jenv, jc, "tmo", "I"); assert(tmof); tmo = (*jenv)->GetIntField(jenv, jobj, tmof); #ifdef QNX // Read data - QNX with timeout if (tmo <100 & tmo>0) { tmo=100; } dc=readcond(fd, buf, 1, 1, 0, tmo/100); //10th of a second instead of microSecs if (dc<1) { //dc=-1; //return fake error } #endif /* QNX */ #if defined(__linux__) || defined(__osx__) // Start the timer. timeoutOccurred = FALSE; if (tmo > 0) start_read_timer(tmo); // Read data. buf[0] = 0; dc = read(fd, buf, 1); serrno = errno; // Stop the timer. if (tmo > 0) stop_read_timer(); if (dc < 0 && !(serrno == EINTR || serrno == EAGAIN)) { (*jenv)->ThrowNew(jenv, ec, ""); } #endif /* __linux__ */ // If timeout had occurred, or if nil data was received, return -1. if (dc <= 0) return -1; return (int)(unsigned char)buf[0]; } /* cygDeviceInputStream_readDeviceOneByteNC */ int cygDeviceInputStream_readDeviceNC (JNIEnv *jenv, jobject jobj, jbyteArray jba, jint off, jint len) { jclass jc; jfieldID jf; jint fd = -1; int rc; int dc = 0; char *cbuf; jfieldID tmof; int tmo; jfieldID tmoDonef; cbuf = malloc(len); assert(cbuf); // Get the file descriptor. jc = (*jenv)->GetObjectClass(jenv, jobj); assert(jc); jf = (*jenv)->GetFieldID(jenv, jc, "fd", "I"); assert(jf); fd = (*jenv)->GetIntField(jenv, jobj, jf); if (fd == -1) { return -1; } tmof = (*jenv)->GetFieldID(jenv, jc, "tmo", "I"); assert(tmof); tmoDonef = (*jenv)->GetFieldID(jenv, jc, "tmoDone", "Z"); assert(tmoDonef); tmo = (*jenv)->GetIntField(jenv, jobj, tmof); #ifdef QNX // Read data - QNX with timeout if (tmo <100 & tmo>0) { tmo=100; } dc=readcond(fd, cbuf, len, len, tmo/100, tmo/100); //10th of a second instead of microSecs if (dc<len) { (*jenv)->SetBooleanField(jenv, jobj, tmoDonef, (jboolean)JNI_TRUE); //dc=-1; //return fake error } #endif /* QNX */ #if defined(__linux__) || defined(__osx__) // Start the timer. timeoutOccurred = FALSE; if (tmo > 0) start_read_timer(tmo); //Read data dc = read(fd, cbuf, len); // If the timer had already expired, set the field tmoDone. // Else, stop the timer. if (timeoutOccurred) { dc = 0; // Bug fix for PR#117959 (*jenv)->SetBooleanField(jenv, jobj, tmoDonef, (jboolean)JNI_TRUE); } if (tmo > 0) stop_read_timer(); #endif /*__linux__*/ // Copy back the data into the java buffer. if (dc > 0) (*jenv)->SetByteArrayRegion(jenv, jba, off, dc, (jbyte*)cbuf); free(cbuf); return dc; } /* cygDeviceInputStream_readDeviceNC */