diff --git a/global.h b/global.h index bb0ec11..99cd826 100644 --- a/global.h +++ b/global.h @@ -33,7 +33,7 @@ #define PROGRAM_NAME "DSRemote" -#define PROGRAM_VERSION "0.30_1508291645" +#define PROGRAM_VERSION "0.30_1508312029" #define MAX_PATHLEN 4096 diff --git a/tmc_dev.c b/tmc_dev.c index a671ad5..85cce87 100644 --- a/tmc_dev.c +++ b/tmc_dev.c @@ -96,7 +96,7 @@ void tmcdev_close(struct tmcdev *dev) int tmcdev_write(struct tmcdev *dev, const char *cmd) { - int i, size, qry=0; + int i, n, len, qry=0; char buf[MAX_CMD_LEN + 16], str[256]; @@ -106,21 +106,23 @@ int tmcdev_write(struct tmcdev *dev, const char *cmd) return -1; } - if(strlen(cmd) > MAX_CMD_LEN) + len = strlen(cmd); + + if(len > MAX_CMD_LEN) { printf("tmcdev error: command too long\n"); return -1; } - if(strlen(cmd) < 2) + if(len < 2) { printf("tmcdev error: command too short\n"); return -1; } - if(cmd[strlen(cmd) - 1] == '?') + if(cmd[len - 1] == '?') { qry = 1; } @@ -145,22 +147,40 @@ int tmcdev_write(struct tmcdev *dev, const char *cmd) printf("tmc_dev write: %s", buf); } - size = write(dev->fd, buf, strlen(buf)); + n = write(dev->fd, buf, strlen(buf)); - if(size < 0) + if(n != (len + 1)) { printf("tmcdev error: device write error"); + + return -1; } if(!qry) { for(i=0; i<20; i++) { - usleep(50000); + usleep(25000); - write(dev->fd, "*OPC?\n", 6); + n = write(dev->fd, "*OPC?\n", 6); - if(read(dev->fd, str, 128) == 2) + if(n < 0) + { + printf("tmcdev error: device write error"); + + return -1; + } + + n = read(dev->fd, str, 128); + + if(n < 0) + { + printf("tmcdev error: device read error"); + + return -1; + } + + if(n == 2) { if(str[0] == '1') { @@ -170,7 +190,7 @@ int tmcdev_write(struct tmcdev *dev, const char *cmd) } } - return size - 1; + return len; } @@ -211,15 +231,7 @@ int tmcdev_read(struct tmcdev *dev) return -1; } - if(size >= 0) - { - dev->hdrbuf[size] = 0; - } - - if(size == 0) - { - return 0; - } + dev->hdrbuf[size] = 0; if(dev->hdrbuf[0] != '#') { diff --git a/tmc_lan.cpp b/tmc_lan.cpp index 950fe61..fe73293 100644 --- a/tmc_lan.cpp +++ b/tmc_lan.cpp @@ -38,38 +38,40 @@ int sockfd; struct sockaddr_in inet_address; -struct timeval timeout, temp_timeout; +struct timeval timeout; -fd_set tcp_fds, temp_tcp_fds; /* filedescriptor pool */ +fd_set tcp_fds; /* filedescriptor pool */ static int tmclan_send(const char *str) { - int n, len; + int len; + + fd_set temp_tcp_fds = tcp_fds; /* because select overwrites the arguments */ + struct timeval temp_timeout = timeout; len = strlen(str); - n = send(sockfd, str, len, MSG_NOSIGNAL); + if(select(sockfd + 1, 0, &temp_tcp_fds, 0, &temp_timeout) != -1) + { + if(FD_ISSET(sockfd, &temp_tcp_fds)) /* check if our file descriptor is set */ + { + return send(sockfd, str, len, MSG_NOSIGNAL); + } + } - if(n == len) - { - return n; - } - else - { - return -1; - } + return -1; } static int tmclan_recv(char *buf, int sz) { - temp_tcp_fds = tcp_fds; /* because select overwrites the arguments */ - temp_timeout = timeout; + fd_set temp_tcp_fds = tcp_fds; /* because select overwrites the arguments */ + struct timeval temp_timeout = timeout; - if(select(sockfd + 1, &temp_tcp_fds, 0, 0, &temp_timeout)) + if(select(sockfd + 1, &temp_tcp_fds, 0, 0, &temp_timeout) != -1) { if(FD_ISSET(sockfd, &temp_tcp_fds)) /* check if our file descriptor is set */ { @@ -217,14 +219,25 @@ int tmclan_write(struct tmcdev *tmc_device __attribute__ ((unused)), const char { for(int i=0; i<20; i++) { - usleep(50000); + usleep(25000); if(tmclan_send("*OPC?\n") != 6) { + printf("tmcdev error: device write error"); + return -1; } - if(tmclan_recv(str, 128) == 2) + n = tmclan_recv(str, 128); + + if(n < 0) + { + printf("tmcdev error: device read error"); + + return -1; + } + + if(n == 2) { if(str[0] == '1') { @@ -290,15 +303,7 @@ int tmclan_read(struct tmcdev *tmc_device) return -3; } - if(size >= 0) - { - tmc_device->hdrbuf[size] = 0; - } - - if(size == 0) - { - return 0; - } + tmc_device->hdrbuf[size] = 0; if(tmc_device->hdrbuf[0] != '#') {