shun-iwasawa 31accf
#include "stopmotionserial.h"
shun-iwasawa 31accf
#include "stopmotion.h"
shun-iwasawa 31accf
#include <qserialport></qserialport>
shun-iwasawa 31accf
#include <qserialportinfo></qserialportinfo>
shun-iwasawa 31accf
//=============================================================================
shun-iwasawa 31accf
//=============================================================================
shun-iwasawa 31accf
shun-iwasawa 31accf
StopMotionSerial::StopMotionSerial() { m_serialPort = new QSerialPort(this); }
shun-iwasawa 31accf
StopMotionSerial::~StopMotionSerial() {}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
QStringList StopMotionSerial::getAvailableSerialPorts() {
shun-iwasawa 31accf
  QList<qserialportinfo> list = QSerialPortInfo::availablePorts();</qserialportinfo>
shun-iwasawa 31accf
  QStringList ports;
shun-iwasawa 31accf
  ports.push_back(tr("No Device"));
shun-iwasawa 31accf
  int size = list.size();
shun-iwasawa 31accf
  for (int i = 0; i < size; i++) {
shun-iwasawa 31accf
    std::string desc  = list.at(i).description().toStdString();
shun-iwasawa 31accf
    std::string port  = list.at(i).portName().toStdString();
shun-iwasawa 31accf
    std::string maker = list.at(i).manufacturer().toStdString();
shun-iwasawa 31accf
    ports.push_back(list.at(i).portName());
shun-iwasawa 31accf
    if (list.at(i).hasProductIdentifier()) {
shun-iwasawa 31accf
      std::string id =
shun-iwasawa 31accf
          QString::number(list.at(i).productIdentifier()).toStdString();
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
    if (list.at(i).hasVendorIdentifier()) {
shun-iwasawa 31accf
      std::string id =
shun-iwasawa 31accf
          QString::number(list.at(i).vendorIdentifier()).toStdString();
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return ports;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
bool StopMotionSerial::setSerialPort(QString port) {
shun-iwasawa 31accf
  if (m_serialPort->isOpen()) m_serialPort->close();
shun-iwasawa 31accf
  QList<qserialportinfo> list = QSerialPortInfo::availablePorts();</qserialportinfo>
shun-iwasawa 31accf
  QStringList ports;
shun-iwasawa 31accf
  bool success = false;
shun-iwasawa 31accf
  int size     = list.size();
shun-iwasawa 31accf
  for (int i = 0; i < size; i++) {
shun-iwasawa 31accf
    if (port == list.at(i).portName()) {
shun-iwasawa 31accf
      m_serialPort->setPort(list.at(i));
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  // m_serialPort->setBaudRate(9600);
shun-iwasawa 31accf
  bool connected = m_serialPort->open(QIODevice::ReadWrite);
shun-iwasawa 31accf
  if (connected) {
shun-iwasawa 31accf
    success = m_serialPort->setBaudRate(QSerialPort::Baud9600) &
shun-iwasawa 31accf
              m_serialPort->setParity(QSerialPort::NoParity) &
shun-iwasawa 31accf
              m_serialPort->setDataBits(QSerialPort::Data8) &
shun-iwasawa 31accf
              m_serialPort->setStopBits(QSerialPort::OneStop) &
shun-iwasawa 31accf
              m_serialPort->setFlowControl(QSerialPort::NoFlowControl);
shun-iwasawa 31accf
    std::string fNumber =
shun-iwasawa 31accf
        std::to_string(StopMotion::instance()->getXSheetFrameNumber());
shun-iwasawa 31accf
    char const* fNumberChar = fNumber.c_str();
shun-iwasawa 31accf
    m_serialPort->write(fNumberChar);
shun-iwasawa 31accf
    m_serialPort->waitForBytesWritten(10);
shun-iwasawa 31accf
    QByteArray DataReceive = m_serialPort->readLine();
shun-iwasawa 31accf
    while (m_serialPort->waitForReadyRead(30)) {
shun-iwasawa 31accf
      DataReceive += m_serialPort->readLine();
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
    QByteArray response = DataReceive;
shun-iwasawa 31accf
    QString s(response);
shun-iwasawa 31accf
    std::string text = response.toStdString();
shun-iwasawa 31accf
    //}
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return success;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void StopMotionSerial::sendSerialData() {
shun-iwasawa 31accf
  if (m_serialPort->isOpen()) {
shun-iwasawa 31accf
    std::string fNumber =
shun-iwasawa 31accf
        std::to_string(StopMotion::instance()->getXSheetFrameNumber());
shun-iwasawa 31accf
    char const* fNumberChar = fNumber.c_str();
shun-iwasawa 31accf
    m_serialPort->write(fNumberChar);
shun-iwasawa 31accf
    m_serialPort->waitForBytesWritten(10);
shun-iwasawa 31accf
    QByteArray DataReceive = m_serialPort->readLine();
shun-iwasawa 31accf
    while (m_serialPort->waitForReadyRead(30)) {
shun-iwasawa 31accf
      DataReceive += m_serialPort->readLine();
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
    QByteArray response = DataReceive;
shun-iwasawa 31accf
    QString s(response);
shun-iwasawa 31accf
    std::string text = response.toStdString();
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
shun-iwasawa 31accf
  // for not data sending is not implemented yet, just the frame number.
shun-iwasawa 31accf
  // These lines are here to be a reference for using column data as movement.
shun-iwasawa 31accf
  // TDoubleParam *param =
shun-iwasawa 31accf
  // TApp::instance()->getCurrentScene()->getScene()->getXsheet()->getStageObjectTree()->getStageObject(0)->getParam(TStageObject::T_X);
shun-iwasawa 31accf
  // double value =
shun-iwasawa 31accf
  // TApp::instance()->getCurrentScene()->getScene()->getXsheet()->getStageObjectTree()->getStageObject(0)->getParam(TStageObject::T_X,
shun-iwasawa 31accf
  // m_xSheetFrameNumber - 1);
shun-iwasawa 31accf
  // QString isCam =
shun-iwasawa 31accf
  // TApp::instance()->getCurrentScene()->getScene()->getXsheet()->getStageObjectTree()->getStageObject(0)->getId().isCamera()
shun-iwasawa 31accf
  // ? "yep" : "nope";
shun-iwasawa 31accf
  // std::string name =
shun-iwasawa 31accf
  // TApp::instance()->getCurrentScene()->getScene()->getXsheet()->getStageObjectTree()->getStageObject(0)->getName();
shun-iwasawa 31accf
  // TMeasure *measure =param->getMeasure();
shun-iwasawa 31accf
  // const TUnit *unit = measure->getCurrentUnit();
shun-iwasawa 31accf
  // double newValue = unit->convertTo(value);
shun-iwasawa 31accf
}