diff --git a/QnetITAP.cpp b/QnetITAP.cpp index d4646b3..6816e51 100644 --- a/QnetITAP.cpp +++ b/QnetITAP.cpp @@ -130,7 +130,8 @@ REPLY_TYPE CQnetITAP::GetITAPData(unsigned char *buf) { // Shamelessly adapted from Jonathan G4KLX's CIcomController::GetResponse() // and CSerialController::read() - // Get the start of the frame or nothing at all + + // Get the buffer size or nothing at all int ret = ::read(serfd, buf, 1U); if (ret < 0) { printf("Error when reading first byte from the Icom radio %d: %s", errno, strerror(errno)); @@ -153,17 +154,9 @@ REPLY_TYPE CQnetITAP::GetITAPData(unsigned char *buf) unsigned int offset = 1U; while (offset < length) { - fd_set fds; - FD_ZERO(&fds); - FD_SET(serfd, &fds); - int n = ::select(serfd+1, &fds, NULL, NULL, NULL); // wait untill it's ready. won't return a zero. - if (n < 0) { - printf("ERROR: GetITAPData: select returned error %d: %s\n", errno, strerror(errno)); - return RT_ERROR; - } ret = ::read(serfd, buf + offset, length - offset); - if (ret < 0 && errno!=EAGAIN) { + if (ret<0 && errno!=EAGAIN) { printf("Error when reading buffer from the Icom radio %d: %s\n", errno, strerror(errno)); return RT_ERROR; } @@ -205,7 +198,6 @@ void CQnetITAP::Run(const char *cfgfile) bool is_alive = false; std::chrono::steady_clock::time_point lastdata = std::chrono::steady_clock::now(); -// bool wasone = false; while (keep_running) { fd_set readfds; FD_ZERO(&readfds); @@ -249,51 +241,28 @@ void CQnetITAP::Run(const char *cfgfile) unsigned char buf[100]; ssize_t len; - if (FD_ISSET(serfd, &readfds)) { - REPLY_TYPE rt = GetITAPData(buf); - - if (rt == RT_ERROR) - break; - - if (rt == RT_TIMEOUT) - continue; - + if (keep_running && FD_ISSET(serfd, &readfds)) { lastdata = std::chrono::steady_clock::now(); - //printf("read %d bytes from ITAP\n", (int)buf[0]); - if (RT_DATA==rt || RT_HEADER==rt) { - if (ProcessITAP(buf)) + switch(GetITAPData(buf) { + case RT_ERROR: + keep_running = false; + break; + case RT_DATA: + case RT_HEADER: + if (ProcessITAP(buf)) + keep_running = false; + break; + case RT_PONG: + if (! is_alive) { + printf("Icom Radio is connected.\n"); + is_alive = true; + } break; - } else { - switch (rt) { - case RT_HEADER_ACK: - //printf("HEADER_ACK"); - //for (int i=0; i