diff --git a/detect.h b/detect.h index 68806b6..4ca028c 100644 --- a/detect.h +++ b/detect.h @@ -65,6 +65,8 @@ class PosCtl { int OutputClose(); int OutputOpen(); + int WriteTTY(char *); + int ReadTTY(char *, int); public: PosCtl(); ~PosCtl() {}; diff --git a/posctl.cc b/posctl.cc index b6f9902..90e7ba2 100644 --- a/posctl.cc +++ b/posctl.cc @@ -664,10 +664,58 @@ int PosCtl::OutputOpen() { return 0; }; +int PosCtl::WriteTTY (char * outbuf) { + ssize_t len; + + printf ("%s:%d %s send: '%s'\n", __FILE__, __LINE__, __FUNCTION__, outbuf); + + len = write (device_fd, outbuf, strlen(outbuf)); + if ((size_t) len != strlen(outbuf) || len < 0) { + printf ("%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, + device.c_str(), strerror(errno)); + errormessage_display ((char *)"window-posctl", (char *)"WriteTTY", + (char *) "%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, + device.c_str(), strerror(errno)); + } + + return 0; +} + +int PosCtl::ReadTTY (char * inbuf, int length) { + ssize_t len; + + // make device non-blocking + fcntl(device_fd, F_SETFL, fcntl(device_fd, F_GETFL) | O_NONBLOCK); + + len = read (device_fd, inbuf, length); + + // somehow the first read sometimes fails, no idea why + if (len < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { + usleep(100000); + len = read (device_fd, inbuf, length); + } + + if (len < 0) { + printf ("%s:%d could not read data from device:%s Error:%s\n", __FILE__, __LINE__, + device.c_str(), strerror(errno)); + errormessage_display ((char *)"window-posctl", (char *)"ReadTTY", + (char *) "%s:%d could not read data from device:%s Error:%s\n", __FILE__, __LINE__, + device.c_str(), strerror(errno)); + inbuf[0] = 0; + } + else { + inbuf[len] = 0; + printf ("%s:%d %s receive: '%s'\n", __FILE__, __LINE__, __FUNCTION__, inbuf); + } + + // make device blocking + fcntl(device_fd, F_SETFL, fcntl(device_fd, F_GETFL) & ~O_NONBLOCK); + + return 0; +} int PosCtl::OutputWriteValue (int axis, double value) { char outbuf[255]; - ssize_t len; printf ("%s:%d %s Axis %d Value:%f\n", __FILE__, __LINE__, __FUNCTION__, axis, value); if (device_fd <= 0) if (OutputOpen() != 0) return -1; @@ -677,46 +725,26 @@ int PosCtl::OutputWriteValue (int axis, double value) { std::string s = setlocale(LC_ALL, NULL); setlocale (LC_ALL, "C"); - snprintf(outbuf, 254, ":R%c%c%08.4f#\n", (axis == 0 ? 'D' : 'R'), - (value < 0 ? '-' : '+'), - value); + snprintf(outbuf, 254, ":R%c%+09.4f#", (axis == 0 ? 'D' : 'R'), value); outbuf[254] = 0; // reset language setting to default setlocale (LC_ALL, s.c_str()); - printf ("%s:%d %s send: '%s'\n", __FILE__, __LINE__, __FUNCTION__, outbuf); - - len = write (device_fd, outbuf, strlen(outbuf)); - if ((size_t) len != strlen(outbuf) || len < 0) { - printf ("%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, - device.c_str(), strerror(errno)); - errormessage_display ((char *)"window-posctl", (char *)"OutputWriteValue", - (char *) "%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, - device.c_str(), strerror(errno)); - } + WriteTTY(outbuf); + ReadTTY(outbuf, sizeof(outbuf) - 1); return 0; }; int PosCtl::OutputWriteStop () { char outbuf[255]; - ssize_t len; printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__); - snprintf (outbuf, 255, ":Q#, RT9#\n"); + snprintf (outbuf, 255, ":Q#"); if (device_fd <= 0) if (OutputOpen() != 0) return -1; - - len = write (device_fd, outbuf, strlen(outbuf)); - if ((size_t) len != strlen(outbuf) || len < 0) { - printf ("%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, - device.c_str(), strerror(errno)); - errormessage_display ((char *)"window-posctl", (char *)"OutputWriteValue", - (char *) "%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, - device.c_str(), strerror(errno)); - } - + WriteTTY(outbuf); return 0; } @@ -724,8 +752,8 @@ int PosCtl::OutputWriteStop () { int PosCtl::OutputWriteStart () { printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__); - OutputWriteValue(0, 1.0); - OutputWriteValue(1, 1.0); + OutputWriteValue(0, 0.0); + OutputWriteValue(1, 0.0); return 0; }