00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "V24.h"
00011 #include "V24_strings.h"
00012
00013 #define SERIAL_BAUDRATE BAUD19200
00014 #define SERIAL_DEVICE "/dev/ttyUSB0"
00015
00020 bool V24Control::setInterface(QString _interface) {
00021 this->sDeviceInterface = _interface;
00022 printf ("Serial Interface set to: %s\n", _interface.ascii());
00023 if (port->isOpen()) this->closeDevice();
00024
00025 port = new Posix_QextSerialPort(this->sDeviceInterface.ascii());
00026
00027 return this->openDevice();
00028 }
00029
00035 V24Control::V24Control(QMutex *_guiMutex, QMainWindow *_parent) {
00036
00037 sDeviceInterface = "/dev/ttyUSB0";
00038
00039 port = new Posix_QextSerialPort(sDeviceInterface);
00040 this->guiMutex = _guiMutex;
00041 this->conMutex = new QMutex();
00042 this->receiving = this->transmitting = false;
00043 this->parent = _parent;
00044
00045 rxtx = true;
00046 }
00047
00053 bool V24Control::transmitAndReceive(unsigned char tData, unsigned char rData) {
00054
00055 int retransmitLoop=0, rxLoop=0, back;
00056 unsigned char _data;
00057
00058 retransmitLoop=0;
00059 rxLoop=0;
00060 do {
00061 transmit(tData);
00062
00063 rxLoop = 0;
00064 do {
00065 back = port->readBlock((char*)&_data, 1);
00066 printf (".");
00067
00068 if (back >= 1) {
00069 printf ("###RX=%d###", _data);
00070 if (rData != _data) {
00071 rxLoop++;
00072 } else {
00073 usleep(10000);
00074 rxLoop = 501;
00075 retransmitLoop = 11;
00076 }
00077 } else {
00078 fflush(stdout);
00079 usleep(10000);
00080 rxLoop++;
00081 }
00082
00083 } while (rxLoop < 500);
00084
00085 printf ("\n");
00086 retransmitLoop++;
00087 } while (retransmitLoop < 10 && rData != _data);
00088 if (retransmitLoop== 10) return false;
00089
00090 return true;
00091
00092 }
00093
00101 bool V24Control::transmit (unsigned char data) {
00102
00103
00104 printf(" TX=%u", data);
00105 port->writeBlock((char*)&data, 1);
00106
00107 return true;
00108 }
00109
00116 bool V24Control::receive (unsigned char *data) {
00117
00118 int errorLoop = 0;
00119 int back;
00120
00121 do {
00122 back = port->readBlock((char*)data, 1);
00123 printf (".");
00124
00125 if (back >= 1) {
00126 printf ("#RX=%d#", *data);
00127 errorLoop = 501;
00128 } else {
00129 usleep(10000);
00130 errorLoop++;
00131 fflush(stdout);
00132 }
00133 } while (errorLoop <500);
00134
00135 if (errorLoop == 500) {
00136 this->showErrorCode(E_READ_FAILED);
00137 return false;
00138 }
00139
00140 return true;
00141 }
00142
00151 bool V24Control::transmitCommand (V24COMMAND *c) {
00152
00153
00154 this->openDevice();
00155
00156
00157 printf("\nBegin transmitCommand():\n");
00158
00159
00160
00161
00162 if (!this->transmitAndReceive(V24_COMMAND_SYNC_COMMAND, V24_COMMAND_SYNC_OK_COMMAND)) return false;
00163 if (!this->transmitAndReceive(c->command, c->command));
00164 if (!this->transmitAndReceive(c->dataLength, c->dataLength));
00165
00166 for (int i=0; i < c->dataLength; i++) {
00167
00168 if (!this->transmitAndReceive(c->data[i], c->data[i])) return false;
00169 printf ("\n");
00170 }
00171
00172 printf("End transmitCommand():\n");
00173
00174 return true;
00175 }
00176
00183 bool V24Control::receiveCommand (V24COMMAND *c) {
00184
00185 this->openDevice();
00186
00187 printf("\nBegin receiveCommand():\n");
00188
00189
00190 if (!receive(&c->command)) {
00191 return false;
00192 }
00193 if (rxtx) {
00194 }
00195 printf ("\n");
00196
00197
00198 if (!receive(&c->dataLength)) {
00199 return false;
00200 }
00201 if (rxtx) {
00202 }
00203 printf ("\n");
00204
00205 for (int i=0; i< c->dataLength; i++) {
00206
00207 if (!receive(&c->data[i])) {
00208 return false;
00209 }
00210 if (rxtx) {
00211 }
00212 printf ("\n");
00213 }
00214 printf("End receiveCommand():\n");
00215
00216
00217 return true;
00218 }
00219
00223 void V24Control::closeDevice() {
00224 port->close();
00225 }
00226
00230 bool V24Control::openDevice() {
00231
00232
00233 if (!port->isOpen()) {
00234
00235
00236 if (!port->open()) {
00237 DBG();
00238 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "Could not open Interface: " + sDeviceInterface);
00239
00240 return false;
00241 } else {
00242
00243 port->setFlowControl(FLOW_OFF);
00244 port->setParity(PAR_NONE);
00245 port->setDataBits(DATA_8);
00246 port->setStopBits(STOP_1);
00247 port->setBaudRate(SERIAL_BAUDRATE);
00248 port->setTimeout(1,0);
00249 }
00250 }
00251
00252 return true;
00253 }
00254
00255
00262 void V24Control::parseFloatData(unsigned char *c, float *_data) {
00263
00264 unsigned char* _cpfValue = (unsigned char*)_data;
00265
00266 for (int i=0; i<(int)sizeof(float); i++) {
00267 _cpfValue[i] = c[i];
00268 }
00269 }
00270
00271
00278 void V24Control::parseIntegerData(unsigned char* c, unsigned int *_data) {
00279
00280 unsigned char* _cpiValue = (unsigned char*)_data;
00281
00282 for (int i=0; i<(int)sizeof(unsigned int); i++) {
00283 *(_cpiValue+i) = c[i];
00284 }
00285 }
00286
00293 bool V24Control::receiveTransmitOKCommand() {
00294 return true;
00295 }
00296
00297
00302 void V24Control::showErrorCode(unsigned long _errorCode) {
00303
00304
00305 if (_errorCode == E_NO_ERROR) {
00306 return;
00307 }
00308
00309
00310 if (_errorCode == E_INVALID_FD) {
00311 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "Invalid file descriptor (port was not opened correctly)");
00312 printf ("Invalid file descriptor (port was not opened correctly)\n");
00313 } else if (_errorCode == E_NO_MEMORY) {
00314 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "Unable to allocate memory tables");
00315 printf ("Unable to allocate memory tables\n");
00316 } else if (_errorCode == E_CAUGHT_NON_BLOCKED_SIGNAL) {
00317 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "Caught a non-blocked signal");
00318 printf ("Caught a non-blocked signal\n");
00319 } else if (_errorCode == E_PORT_TIMEOUT) {
00320 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "Operation timed out");
00321 printf ("Operation timed out\n");
00322 } else if (_errorCode == E_INVALID_DEVICE) {
00323 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "The file opened by the port is not a character device");
00324 printf ("The file opened by the port is not a character device\n");
00325 } else if (_errorCode == E_BREAK_CONDITION) {
00326 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "The port detected a break condition");
00327 printf ("The port detected a break condition\n");
00328 } else if (_errorCode == E_FRAMING_ERROR) {
00329 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "The port detected a framing error");
00330 printf ("The port detected a framing error\n");
00331 } else if (_errorCode == E_IO_ERROR) {
00332 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "There was an I/O error while communicating with the port");
00333 printf ("There was an I/O error while communicating with the port\n");
00334 } else if (_errorCode == E_BUFFER_OVERRUN) {
00335 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "Character buffer overrun");
00336 printf ("Character buffer overrun\n");
00337 } else if (_errorCode == E_RECEIVE_OVERFLOW) {
00338 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "Receive buffer overflow");
00339 printf ("Receive buffer overflow\n");
00340 } else if (_errorCode == E_RECEIVE_PARITY_ERROR) {
00341 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "The port detected a parity error in the received data");
00342 printf ("The port detected a parity error in the received data\n");
00343 } else if (_errorCode == E_TRANSMIT_OVERFLOW) {
00344 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "Transmit buffer overflow");
00345 printf ("Transmit buffer overflow\n");
00346 } else if (_errorCode == E_READ_FAILED) {
00347 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "General read operation failure. Please check robot connection.");
00348 printf ("General read operation failure. Please check robot connection.\n");
00349 } else if (_errorCode == E_WRITE_FAILED) {
00350 QMessageBox::critical(this->parent, "Serial Communcation ERROR:", "General write operation failure");
00351 printf ("General write operation failure\n");
00352 }
00353 }
00354
00355
00356