diff --git a/connection.cpp b/connection.cpp index 8cc1a7f..9789e5a 100644 --- a/connection.cpp +++ b/connection.cpp @@ -29,9 +29,17 @@ #include "connection.h" +#define TMC_LAN_TIMEOUT 5000 + +#define MAX_CMD_LEN (255) +#define MAX_RESP_LEN (1024 * 1024 * 2) + + int tmc_connection_type; -struct tmcdev *usb_tmcdev; +struct tmcdev *tmc_device; + +QTcpSocket *sck; @@ -39,9 +47,9 @@ struct tmcdev * tmc_open_usb(const char *device) { tmc_connection_type = 0; - usb_tmcdev = tmcdev_open(device); + tmc_device = tmcdev_open(device); - return usb_tmcdev; + return tmc_device; } @@ -49,38 +57,292 @@ void tmc_close(void) { if(tmc_connection_type == 0) { - tmcdev_close(usb_tmcdev); + tmcdev_close(tmc_device); - usb_tmcdev = NULL; + tmc_device = NULL; + } + else + { + if(sck != NULL) + { + sck->abort(); + + delete sck; + + sck = NULL; + } + + if(tmc_device != NULL) + { + free(tmc_device->hdrbuf); + + free(tmc_device); + + tmc_device = NULL; + } } } -int tmc_write(const char *msg) +int tmc_write(const char *cmd) { + int qry=0; + + char buf[MAX_CMD_LEN + 16], + str[256]; + if(tmc_connection_type == 0) { - return tmcdev_write(usb_tmcdev, msg); + return tmcdev_write(tmc_device, cmd); + } + else + { + if(sck == NULL) + { + return -1; + } + + if(strlen(cmd) > MAX_CMD_LEN) + { + printf("tmc_lan error: command too long\n"); + + return -1; + } + + if(strlen(cmd) < 2) + { + printf("tmc_lan error: command too short\n"); + + return -1; + } + + if(cmd[strlen(cmd) - 1] == '?') + { + qry = 1; + } + + strncpy(buf, cmd, MAX_CMD_LEN); + + buf[MAX_CMD_LEN] = 0; + + strcat(buf, "\n"); + + if(!(!strncmp(buf, ":TRIG:STAT?", 11) || + !strncmp(buf, ":TRIG:SWE?", 10) || + !strncmp(buf, ":WAV:DATA?", 10) || + !strncmp(buf, ":WAV:MODE NORM", 14) || + !strncmp(buf, ":WAV:FORM BYTE", 14) || + !strncmp(buf, ":WAV:SOUR CHAN", 14) || + !strncmp(buf, ":TRIG:SWE?", 10) || + !strncmp(buf, ":ACQ:SRAT?", 10) || + !strncmp(buf, ":ACQ:MDEP?", 10) || + !strncmp(buf, ":MEAS:COUN:VAL?", 15))) + { + printf("tmc_lan write: %s", buf); + } + + sck->write(buf); + + if(sck->waitForBytesWritten(TMC_LAN_TIMEOUT) == false) + { + return -1; + } + + if(!qry) + { + for(int i=0; i<20; i++) + { + usleep(50000); + + sck->write("*OPC?\n"); + + if(sck->waitForBytesWritten(TMC_LAN_TIMEOUT) == false) + { + return -1; + } + + if(sck->waitForReadyRead(TMC_LAN_TIMEOUT) == false) + { + return -1; + } + + if(sck->read(str, 128) == 2) + { + if(str[0] == '1') + { + break; + } + } + } + } + + return strlen(cmd); } return -1; } +/* + * TMC Blockheader ::= #NXXXXXX: is used to describe + * the length of the data stream, wherein, # is the start denoter of + * the data stream; N is less than or equal to 9; the N figures + * followed N represents the length of the data stream in bytes. + * For example, #9001152054. Wherein, N is 9 and 001152054 + * represents that the data stream contains 1152054 bytes + * effective data. + * Reading from the file descriptor blocks, + * there is a timeout of 5000 milli-Sec. + */ int tmc_read(void) { + int size, size2, len; + + char blockhdr[32]; + if(tmc_connection_type == 0) { - return tmcdev_read(usb_tmcdev); + return tmcdev_read(tmc_device); + } + else + { + if(sck == NULL) + { + return -1; + } + + tmc_device->hdrbuf[0] = 0; + + tmc_device->sz = 0; + + size = 0; + + while(1) + { + if(sck->waitForReadyRead(TMC_LAN_TIMEOUT) == false) + { + return -1; + } + + size += sck->read(tmc_device->hdrbuf + size, MAX_RESP_LEN - size); + + if(tmc_device->hdrbuf[size - 1] == '\n') + { + break; + } + } + + if((size < 2) || (size > MAX_RESP_LEN)) + { + tmc_device->hdrbuf[0] = 0; + + tmc_device->buf[0] = 0; + + return -1; + } + + if(size >= 0) + { + tmc_device->hdrbuf[size] = 0; + } + + if(size == 0) + { + return 0; + } + + if(tmc_device->hdrbuf[0] != '#') + { + if(tmc_device->hdrbuf[size - 1] == '\n') + { + tmc_device->hdrbuf[--size] = 0; + } + + tmc_device->buf = tmc_device->hdrbuf; + + tmc_device->sz = size; + + return tmc_device->sz; + } + + strncpy(blockhdr, tmc_device->hdrbuf, 16); + + len = blockhdr[1] - '0'; + + if((len < 1) || (len > 9)) + { + blockhdr[31] = 0; + + return -1; + } + + blockhdr[len + 2] = 0; + + size2 = atoi(blockhdr + 2); + + size--; // remove the last character + + if(size < size2) + { + blockhdr[31] = 0; + + return -1; + } + + tmc_device->buf = tmc_device->hdrbuf + len + 2; + + tmc_device->sz = size2; + + return tmc_device->sz; } return -1; } +struct tmcdev * tmc_open_lan(const char *address) +{ + tmc_connection_type = 1; + + sck = new QTcpSocket; + + sck->connectToHost(address, 5555); + + if(sck->waitForConnected(TMC_LAN_TIMEOUT) == false) + { + sck->abort(); + delete sck; + sck = NULL; + return NULL; + } + + tmc_device = (struct tmcdev *)calloc(1, sizeof(struct tmcdev)); + if(tmc_device == NULL) + { + sck->abort(); + delete sck; + sck = NULL; + return NULL; + } + + tmc_device->hdrbuf = (char *)calloc(1, MAX_RESP_LEN + 1024); + if(tmc_device->hdrbuf == NULL) + { + free(tmc_device); + sck->abort(); + delete sck; + sck = NULL; + return NULL; + } + + tmc_device->buf = tmc_device->hdrbuf; + + return tmc_device; +} + + - diff --git a/connection.h b/connection.h index 657bfd3..9fb5f4b 100644 --- a/connection.h +++ b/connection.h @@ -29,16 +29,31 @@ #ifndef DSR_CONNECTION_H #define DSR_CONNECTION_H +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include #include "global.h" #include "tmc_dev.h" #include "utils.h" -struct tmcdev * tmc_open_usb(const char *device); + +struct tmcdev * tmc_open_usb(const char *); void tmc_close(void); int tmc_write(const char *); int tmc_read(void); +struct tmcdev * tmc_open_lan(const char *); diff --git a/dsremote.pro b/dsremote.pro index 6ee0f23..567d52c 100644 --- a/dsremote.pro +++ b/dsremote.pro @@ -11,6 +11,7 @@ CONFIG += static CONFIG += largefile QT += widgets +QT += network QMAKE_CXXFLAGS += -Wextra -Wshadow -Wformat-nonliteral -Wformat-security -Wtype-limits -Wfatal-errors diff --git a/global.h b/global.h index bad7b14..2a9906d 100644 --- a/global.h +++ b/global.h @@ -31,7 +31,7 @@ #define PROGRAM_NAME "DSRemote" -#define PROGRAM_VERSION "0.11_1506271103" +#define PROGRAM_VERSION "0.20_1506271641" #define MAX_PATHLEN 4096 diff --git a/interface.cpp b/interface.cpp index 1d108ce..dc0f5c2 100644 --- a/interface.cpp +++ b/interface.cpp @@ -987,6 +987,12 @@ void UI_Mainwindow::vertScaleDialChanged(int new_pos) tmc_write(str); + tmc_write(":TRIG:EDG:LEV?"); + + tmc_read(); + + devparms.triggeredgelevel[chn] = atof(device->buf); + old_pos = new_pos; waveForm->update(); diff --git a/mainwindow.cpp b/mainwindow.cpp index caa2bea..5d0c2a1 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -44,9 +44,9 @@ void UI_Mainwindow::open_connection() { int n; - char str[1024], - dev_str[128] = {""}, - resp_str[1024], + char str[1024] = {""}, + dev_str[256] = {""}, + resp_str[1024] = {""}, *ptr; QSettings settings; @@ -74,7 +74,7 @@ void UI_Mainwindow::open_connection() if(devparms.connectiontype == 0) // USB { - strcpy(dev_str, settings.value("connection/device").toString().toLocal8Bit().data()); + strcpy(dev_str, settings.value("connection/device").toString().toLatin1().data()); if(!strcmp(dev_str, "")) { @@ -93,6 +93,20 @@ void UI_Mainwindow::open_connection() if(devparms.connectiontype == 1) // LAN { + strcpy(dev_str, settings.value("connection/ip").toString().toLatin1().data()); + + if(!strcmp(dev_str, "")) + { + sprintf(str, "No IP address set"); + goto OUT_ERROR; + } + + device = tmc_open_lan(dev_str); + if(device == NULL) + { + sprintf(str, "Can not open connection to %s", dev_str); + goto OUT_ERROR; + } } if(tmc_write("*IDN?") != 5) @@ -2383,6 +2397,12 @@ void UI_Mainwindow::chan_scale_plus() tmc_write(str); + tmc_write(":TRIG:EDG:LEV?"); + + tmc_read(); + + devparms.triggeredgelevel[chn] = atof(device->buf); + waveForm->update(); } @@ -2447,6 +2467,12 @@ void UI_Mainwindow::chan_scale_minus() tmc_write(str); + tmc_write(":TRIG:EDG:LEV?"); + + tmc_read(); + + devparms.triggeredgelevel[chn] = atof(device->buf); + waveForm->update(); } diff --git a/mainwindow.h b/mainwindow.h index bddb0fa..9da53d4 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -32,6 +32,7 @@ #include #include +#include #include #include #include diff --git a/settings_dialog.cpp b/settings_dialog.cpp index 495b1ae..d331590 100644 --- a/settings_dialog.cpp +++ b/settings_dialog.cpp @@ -103,7 +103,19 @@ UI_settings_window::UI_settings_window(QWidget *parnt) comboBox1->setCurrentIndex(dev_str[11] - '0'); - QObject::connect(applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked())); + if(mainwindow->devparms.connected) + { + usbRadioButton->setEnabled(false); + lanRadioButton->setEnabled(false); + ipLineEdit->setEnabled(false); + comboBox1->setEnabled(false); + applyButton->setEnabled(false); + } + else + { + QObject::connect(applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked())); + } + QObject::connect(cancelButton, SIGNAL(clicked()), this, SLOT(close())); exec(); diff --git a/tmc_dev.c b/tmc_dev.c index 002e8e5..800dcee 100644 --- a/tmc_dev.c +++ b/tmc_dev.c @@ -54,7 +54,7 @@ struct tmcdev * tmcdev_open(const char *device) return NULL; } - dev->hdrbuf = calloc(1, MAX_RESP_LEN + 1024); + dev->hdrbuf = (char *)calloc(1, MAX_RESP_LEN + 1024); if(dev->hdrbuf == NULL) { free(dev);