#include "spherebot.h" spherebot::spherebot(QObject *parent) : QObject(parent) { port = new QSerialPort(); bot_connected = false; // lineCounter = 0; lastLineTransmitted = true; timeout_timer = new QTimer(); timeout_timer->setInterval(5000); retry_timer = new QTimer(); retry_timer->setInterval(50); connect(timeout_timer, SIGNAL(timeout()), this, SLOT(resendLine())); connect(retry_timer, SIGNAL(timeout()), this, SLOT(trySendBufferLine())); resetState(); } bool spherebot::connectWithBot() { port->open(QIODevice::ReadWrite); // we open the port if(!port->isOpen()) { //QMessageBox::warning(this, "port error", "Impossible to open the port!"); qDebug()<< "Cannot open the port!"<< endl; return 0; } // Set the port properties //port->setBaudRate(9600); port->setBaudRate(115200); //port->setBaudRate(256000); port->setFlowControl(QSerialPort::NoFlowControl); port->setParity(QSerialPort::NoParity); port->setDataBits(QSerialPort::Data8); port->setStopBits(QSerialPort::OneStop); // lineCounter = 0; // sentLineBuffer.clear(); return 1; } bool spherebot::connectWithBot(QString portName) { port->setPortName(portName); lastLineTransmitted = true; return connectWithBot(); } bool spherebot::disconnectWithBot() { qDebug("port closed"); if(port->isOpen()) port->close(); bot_connected = false; this->resetState(); return 1; } void spherebot::processAnswer(QString answer) { // int linenumber = getOption(line,"N"); //qDebug()<< "process answer: "<< answer<< endl; //qDebug()<< "buffer: " << toSendBuffer<< endl; if(answer.contains("rs")) { resendLine(); } else if (answer.contains("ok")) { //qDebug()<< "answer.contains(ok): "<< endl; lastLineTransmitted = true; timeout_timer->stop(); if(toSendBuffer.size() > 0) { trySendBufferLine(); } else if(sendingFile) { if(toSendBuffer.size() == 0 && lineCounter > lineMax) { emit fileTransmitted(); } sendNext(); } else { //everything sent retry_timer->stop(); } emit dataSent(lastLine); } else if(answer.contains("Spherebot")) { //qDebug()<< "received version"<< endl; bot_connected = true; } else { qDebug()<< "answer did not contain rs or ok or Spherebot"<< endl; qDebug()<< "answer was:"<< answer<< endl; } } QString spherebot::generateChecksumString(QString msg) { QString result = "*"; //"*" is the startcharacter of the checksum std::string msgString = msg.toStdString(); int cs = 0; for(int i=0;iisOpen()) { while(lastLine.endsWith('\n') || lastLine.endsWith('\r')) lastLine.chop(1); cmd.append(" "); cmd.append(generateChecksumString(cmd)); cmd.append('\n'); qDebug()<< "cmd: "<< cmd; if(bot_connected) { if(lastLineTransmitted) { qDebug()<< "Sending: "<< cmd; int er = port->write((const char*)cmd.toUtf8(), cmd.length()); if (er == -1) qDebug()<< "an error occured while writing to the port!!"<< endl; //qDebug()<< "flush result: "<< port->flush(); lastLine = cmd; lastLineTransmitted = false; timeout_timer->start(); } else { toSendBuffer.append(cmd); } } else { toSendBuffer.append(cmd); qDebug()<< "Buffer: "<< toSendBuffer<< endl; retry_timer->start(); } } else { qDebug()<< port->errorString()<< " port is not open"<< endl; return false; } return true; } void spherebot::resendLine() { if(lastLine == "") { timeout_timer->stop(); } else if( bot_connected) { port->write((const char*)lastLine.toUtf8(),lastLine.length()); //while( lastLine.endsWith('\n')) lastLine.chop(1); qDebug()<<"Resending: " << lastLine << endl; } } void spherebot::trySendBufferLine() { qDebug()<< "trySendBufferLine: "<< toSendBuffer.front()<< endl; if(lastLineTransmitted && bot_connected) { if(toSendBuffer.size() > 0) { QString cmd = toSendBuffer.front(); toSendBuffer.pop_front(); port->write((const char*)cmd.toUtf8(),cmd.length()); lastLine = cmd; lastLineTransmitted = false; timeout_timer->start(); } retry_timer->stop(); } } bool spherebot::isConnected() { return bot_connected; } void spherebot::resetState() { lineCounter = 0; ignoreFirstM01 = true; sendingFile = false; toSendBuffer.clear(); lastLine = ""; lineCounter = 0; retry_timer->stop(); timeout_timer->stop(); } QString removeComments(QString intext) { // remove comments QString outTmp1,outTmp2; bool state = true; // true=send, false=ignore for(int i=0;i