From 8ceda24536565ccb216c560b7c6de1655fcf57a8 Mon Sep 17 00:00:00 2001 From: Mjolnir Date: Wed, 2 Sep 2015 15:48:51 +0200 Subject: [PATCH] com port discovery --- ArduinoScreen/ArduinoScreen.ino | 9 ++--- GCodeParser/GCodeParser.ino | 43 +++++++++++++++++++--- GCodeParser/process_string.ino | 11 +++++- Library/LivingDevice.cpp | 64 ++++++++++++++++++++++++--------- Library/LivingDevice.h | 19 +++++----- Library/LivingKeyboard.cpp | 6 +++- Library/LivingKeyboard.h | 2 ++ Library/LivingScreen.cpp | 7 +++- Library/LivingScreen.h | 2 ++ Library/SerialLinux.cpp | 7 ++-- Library/XYPlotter.cpp | 7 +++- Library/XYPlotter.h | 2 ++ Test/mainwindow.cpp | 6 +++- 13 files changed, 145 insertions(+), 40 deletions(-) diff --git a/ArduinoScreen/ArduinoScreen.ino b/ArduinoScreen/ArduinoScreen.ino index d3772f0..e85c873 100644 --- a/ArduinoScreen/ArduinoScreen.ino +++ b/ArduinoScreen/ArduinoScreen.ino @@ -24,8 +24,8 @@ void eval(int command, int value) switch(command) { case 'I': - Serial.print(F("LivingScreen\0\n")); - Serial.flush(); + Serial.println(F("LivingScreen")); + //Serial.flush(); break; case 'T': Serial.print("Translate "); @@ -47,7 +47,8 @@ void eval(int command, int value) break; default: Serial.print("Unknown command "); - Serial.println(command); + Serial.write(command); + Serial.write('\n'); } } @@ -61,7 +62,7 @@ void setup() init_motors(); //Calibration - calibrate(); + //calibrate(); } void loop() diff --git a/GCodeParser/GCodeParser.ino b/GCodeParser/GCodeParser.ino index 77c801c..7eaad32 100644 --- a/GCodeParser/GCodeParser.ino +++ b/GCodeParser/GCodeParser.ino @@ -95,11 +95,39 @@ void setup() } //other initialization. init_process_string(); - init_steppers(); - process_string("G90",3);//Absolute Position + //init_steppers(); + //process_string("G90",3);//Absolute Position //Serial.println("start"); } +void loop() +{ + + char c; + //read in characters if we got them. + if (Serial.available() > 0) + { + c = Serial.read(); + + if (c == '\n' || c == '\r') + { + //process our command! + process_string(commands, serial_count); + //clear command. + init_process_string(); + } + else if (serial_count < COMMAND_SIZE) + { + commands[serial_count] = c; + serial_count++; + } + } + else + delayMicroseconds(100); +} + + +/* void loop() { @@ -112,8 +140,13 @@ void loop() //newlines are ends of commands. if (c != '\n') { - if(c=='I') - Serial.print(F("XYPlotter\0\n")); + if (c == 'I') + Serial.println(F("XYPlotter")); + else if (c == 'C') + { + init_steppers(); + process_string("G90",3);//Absolute Position + } else { if (c == '(') @@ -160,5 +193,5 @@ void loop() // } -} +}*/ diff --git a/GCodeParser/process_string.ino b/GCodeParser/process_string.ino index 0240f02..9033296 100644 --- a/GCodeParser/process_string.ino +++ b/GCodeParser/process_string.ino @@ -65,7 +65,16 @@ void process_string(char instruction[], int size) //did we get a gcode? - if (!has_command('$', instruction, size)&&( + if (has_command('I', instruction, size)) + { + Serial.println(F("XYPlotter")); + } + else if (has_command('C', instruction, size)) + { + init_steppers(); + process_string("G90",3);//Absolute Position + } + else if (!has_command('$', instruction, size)&&( has_command('G', instruction, size) || has_command('X', instruction, size) || has_command('Y', instruction, size) || diff --git a/Library/LivingDevice.cpp b/Library/LivingDevice.cpp index c136796..5bccf37 100644 --- a/Library/LivingDevice.cpp +++ b/Library/LivingDevice.cpp @@ -8,6 +8,7 @@ //#define QIODEVICE_DEBUG char const *LivingDevice::serialports[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3", "/dev/ttyUSB4"}; +bool LivingDevice::usedports[] = {false, false, false, false, false}; LivingDevice::LivingDevice(QString name, int timeout) : serialPort(NULL) @@ -25,18 +26,33 @@ void LivingDevice::discover(QString name, int timeout) qDebug() << "Discovering" << name; for (int i = 0 ; i < 5 && !serialPort ; i++) { - try{ - serialPort = new SerialLinux(serialports[i], 9600); - + if (usedports[i]) + continue; + try { qDebug() << "Trying port" << serialports[i]; + serialPort = new SerialLinux(serialports[i], 9600); - //ask fo his name - sendCommand('I'); - QThread::msleep(500); - QString answer = readAnswer(); + int tries = 0; + QString answer; + qDebug() << "Identify"; + do + { + qDebug() << "Try" << tries; + //ask fo his name + if (!sendCommand('I')) + { + qDebug() << "Cannot speak with port"; + closeSerialPort(); + continue; + } + answer = readAnswer(timeout); + } while (answer == "" && tries++ < 5); if (answer == name) - qDebug() << "Found LivingKeyboard on port" << serialports[i]; + { + qDebug() << "Found " << name << " on port" << serialports[i]; + usedports[i] = true; + } else { qDebug() << "Not" << name; @@ -65,25 +81,27 @@ void LivingDevice::closeSerialPort() serialPort = NULL; } -void LivingDevice::sendCommand(char command) +bool LivingDevice::sendCommand(char command) { if (serialPort) { char buffer[] = {command, '\n'}; - serialPort->WriteData(buffer, 2); + return serialPort->WriteData(buffer, 2); } + return false; } -void LivingDevice::sendCommand(char command, int value) +bool LivingDevice::sendCommand(char command, int value) { if (serialPort) { QString buffer = command + QString::number(value) + "\n"; - serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data())); + return serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data())); } + return false; } -void LivingDevice::sendCommand(char *command, int *value, int nb) +bool LivingDevice::sendCommand(char *command, int *value, int nb) { QString buffer; if (serialPort) @@ -91,18 +109,32 @@ void LivingDevice::sendCommand(char *command, int *value, int nb) for (int i = 0 ; i < nb ; i++) buffer += command[i] + QString::number(value[i]); buffer += '\n'; - serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data())); + return serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data())); } + return false; } -QString LivingDevice::readAnswer() +QString LivingDevice::readAnswer(int timeout) { char buffer[128]; if (serialPort) { - int nb = serialPort->ReadData(buffer, 127); + int tries = 0; + int nb = 0; + do + { + QThread::msleep(100); + nb = serialPort->ReadData(buffer, 127); + } while (nb == 0 && tries++ < timeout / 100); + if (nb <= 0) + { + qDebug() << "No response"; + return ""; //TODO: do something else + } buffer[nb] = '\0'; + while(buffer[strlen(buffer)-1] == '\n' || buffer[strlen(buffer)-1] == '\r') + buffer[strlen(buffer)-1] = '\0'; qDebug() << "Read" << buffer; return QString(buffer); } diff --git a/Library/LivingDevice.h b/Library/LivingDevice.h index c7d0ace..a820cfe 100644 --- a/Library/LivingDevice.h +++ b/Library/LivingDevice.h @@ -11,20 +11,23 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingDevice LivingDevice(QString name, int timeout=5000); virtual ~LivingDevice(); - void discover(QString name, int timeout=5000); - - void closeSerialPort(); + virtual void calibrate() = 0; - void sendCommand(char command); - void sendCommand(char command, int value); - void sendCommand(char *command, int *value, int nb); - - QString readAnswer(); + QString readAnswer(int timeout); protected: Serial *serialPort; static char const *serialports[]; + static bool usedports[]; + + void discover(QString name, int timeout=5000); + void closeSerialPort(); + + bool sendCommand(char command); + bool sendCommand(char command, int value); + bool sendCommand(char *command, int *value, int nb); + }; #endif // LIVINGDEVICE_H diff --git a/Library/LivingKeyboard.cpp b/Library/LivingKeyboard.cpp index cb733b3..eabbf66 100644 --- a/Library/LivingKeyboard.cpp +++ b/Library/LivingKeyboard.cpp @@ -3,7 +3,7 @@ #include LivingKeyboard::LivingKeyboard() -: LivingDevice("LivingKeyboard", 500) +: LivingDevice("LivingKeyboard", 5000) { } @@ -26,3 +26,7 @@ void LivingKeyboard::translation(LivingKeyboard::direction d, unsigned int dista dis *= -1; sendCommand('T', dis); } + +void LivingKeyboard::calibrate() +{ +} diff --git a/Library/LivingKeyboard.h b/Library/LivingKeyboard.h index 7d7ff8b..71137c5 100644 --- a/Library/LivingKeyboard.h +++ b/Library/LivingKeyboard.h @@ -15,6 +15,8 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingKeyboard : public LivingDevice void rotation(tilt t, unsigned int angle); void translation(direction d, unsigned int distance); + + void calibrate(); }; #endif // LIVINGKEYBOARD_H diff --git a/Library/LivingScreen.cpp b/Library/LivingScreen.cpp index 1042bfa..06fb71c 100644 --- a/Library/LivingScreen.cpp +++ b/Library/LivingScreen.cpp @@ -3,7 +3,7 @@ #include LivingScreen::LivingScreen() -: LivingDevice("LivingScreen", 30000) +: LivingDevice("LivingScreen", 5000) { } @@ -26,3 +26,8 @@ void LivingScreen::translation(LivingScreen::direction d, unsigned int distance) dis *= -1; sendCommand('T', dis); } + +void LivingScreen::calibrate() +{ + sendCommand('C'); +} diff --git a/Library/LivingScreen.h b/Library/LivingScreen.h index 55def11..ac468f1 100644 --- a/Library/LivingScreen.h +++ b/Library/LivingScreen.h @@ -14,6 +14,8 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingScreen : public LivingDevice void rotation(direction t, unsigned int angle); void translation(direction d, unsigned int distance); + + void calibrate(); }; #endif // LIVINGSCREEN_H diff --git a/Library/SerialLinux.cpp b/Library/SerialLinux.cpp index 2ce9a18..95410cd 100644 --- a/Library/SerialLinux.cpp +++ b/Library/SerialLinux.cpp @@ -1,5 +1,7 @@ #include "SerialLinux.h" +#include + #include #include #include @@ -17,14 +19,15 @@ SerialLinux::SerialLinux(const char *portName, int baudrate) { _arduinowaittime = 1; //Try to connect to the given port throuh CreateFile - _hSerial = open(portName, O_RDWR | O_NOCTTY);// | O_NDELAY); + //_hSerial = open(portName, O_RDWR | O_NOCTTY | O_NDELAY); + _hSerial = open(portName, O_RDWR | O_NDELAY); //Check if the connection was successfull if(_hSerial < 0) { char buffer[256]; sprintf(buffer, "Open error on port %s %d %s\n", portName, errno, strerror(errno)); - throw buffer; + throw ErrorMessage(buffer); } struct termios serialParams; diff --git a/Library/XYPlotter.cpp b/Library/XYPlotter.cpp index f0c90e9..134067e 100644 --- a/Library/XYPlotter.cpp +++ b/Library/XYPlotter.cpp @@ -5,7 +5,7 @@ const int XYPlotter::toolDistance = 70; XYPlotter::XYPlotter() -: LivingDevice("XYPlotter", 13000) +: LivingDevice("XYPlotter", 5000) { } @@ -19,3 +19,8 @@ void XYPlotter::moveTo(int x, int y, bool toolup=true) int values[] = {0, x, y, toolup * toolDistance}; sendCommand(commands, values, 4); } + +void XYPlotter::calibrate() +{ + sendCommand('C'); +} diff --git a/Library/XYPlotter.h b/Library/XYPlotter.h index af8f331..a3903c0 100644 --- a/Library/XYPlotter.h +++ b/Library/XYPlotter.h @@ -11,6 +11,8 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT XYPlotter : public LivingDevice void moveTo(int x, int y, bool toolup); + void calibrate(); + private: unsigned int posX, posY; bool toolUp; diff --git a/Test/mainwindow.cpp b/Test/mainwindow.cpp index b7d653c..eda2def 100644 --- a/Test/mainwindow.cpp +++ b/Test/mainwindow.cpp @@ -2,6 +2,7 @@ #include "ui_mainwindow.h" #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) @@ -15,6 +16,7 @@ MainWindow::MainWindow(QWidget *parent) connect(ui->keyboardrotatebutton, SIGNAL(clicked()), this, SLOT(rotateKeyboard())); ui->keyboardmovebutton->setEnabled(true); ui->keyboardrotatebutton->setEnabled(true); + keyboard->calibrate(); } catch (ErrorMessage e) { @@ -30,10 +32,11 @@ MainWindow::MainWindow(QWidget *parent) connect(ui->screenrotatebutton, SIGNAL(clicked()), this, SLOT(rotateScreen())); ui->screenmovebutton->setEnabled(true); ui->screenrotatebutton->setEnabled(true); + screen->calibrate(); } catch (ErrorMessage e) { - mouse = NULL; + screen = NULL; qDebug() << QString(e.what()); } qDebug("Connect mouse"); @@ -43,6 +46,7 @@ MainWindow::MainWindow(QWidget *parent) mouse = new XYPlotter(); connect(ui->mousebutton, SIGNAL(clicked()), this, SLOT(moveMouse())); ui->mousebutton->setEnabled(true); + mouse->calibrate(); } catch (ErrorMessage e) { -- 2.30.2