Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 350 → Rev 351

/Misc Projects/PcMarlinInterface/GlobalDefines.h
3,5 → 3,7
 
#include <QtWidgets>
 
#define STATUS_TIMEOUT_MS 3000
 
#endif // GLOBALDEFINES
 
/Misc Projects/PcMarlinInterface/MacroController.cpp
206,6 → 206,17
btnRemoveMacro->setEnabled(false);
}
 
void MacroController::Macro_Clear()
{
for (int i = 0; i < macroCount; i++) {
macroNameList[i]->setText(QString("Macro %1").arg(i+1));
macroValueList[i]->clear();
macroBtnKeyList[i]->setText("Hotkey: None");
}
registeredKeyMacroIDs.clear();
registeredKeySequences.clear();
}
 
void MacroController::Macro_WriteToFile()
{
QString file = QFileDialog::getSaveFileName(this, "Settings XML File", lastKnownFilePath, "XML File (*.xml)");
332,17 → 343,6
}
}
 
void MacroController::Macro_Clear()
{
for (int i = 0; i < macroCount; i++) {
macroNameList[i]->setText(QString("Macro %1").arg(i+1));
macroValueList[i]->clear();
macroBtnKeyList[i]->setText("Hotkey: None");
}
registeredKeyMacroIDs.clear();
registeredKeySequences.clear();
}
 
bool MacroController::eventFilter(QObject *obj, QEvent *event)
{
// Only process keyboard events
/Misc Projects/PcMarlinInterface/MacroController.h
22,9 → 22,9
void Macro_KeybindPrompt(int id);
void Macro_AddEntry(void);
void Macro_RemoveEntry(void);
void Macro_Clear(void);
void Macro_WriteToFile(void);
void Macro_ReadFromFile(void);
void Macro_Clear(void);
 
signals:
void Macro_TransmitText(QString string);
/Misc Projects/PcMarlinInterface/MainWindow.cpp
7,49 → 7,6
centralWidget = new QWidget();
setCentralWidget(centralWidget);
 
// Serial connection UI
groupSerialInit = new QGroupBox("Initialization");
btnSerialConnect = new QPushButton("&Connect");
btnSerialRefresh = new QPushButton("&Refresh");
labelSerialPort = new QLabel("Serial Port:");
labelSerialSpeed = new QLabel("Baud Rate:");
labelSerialDataBits = new QLabel("Data:");
labelSerialStopBits = new QLabel("Stop:");
labelSerialParity = new QLabel("Parity Bit:");
labelSerialFlowControl = new QLabel("Flow Control:");
cboxSerialPort = new QComboBox();
cboxSerialPort->setMinimumWidth(80);
cboxSerialSpeed = new QComboBox();
cboxSerialSpeed->setEditable(true);
cboxSerialSpeed->setValidator(new QIntValidator(0, 100000000, this));
cboxSerialDataBits = new QComboBox();
cboxSerialDataBits->setMaximumWidth(60);
cboxSerialStopBits = new QComboBox();
cboxSerialStopBits->setMaximumWidth(60);
cboxSerialParity = new QComboBox();
cboxSerialFlowControl = new QComboBox();
cboxSerialFlowControl->setMinimumWidth(70);
 
QGridLayout *serialSettingsLayout = new QGridLayout();
serialSettingsLayout->addWidget(btnSerialConnect, 0, 0);
serialSettingsLayout->addWidget(labelSerialPort, 0, 1);
serialSettingsLayout->addWidget(cboxSerialPort, 0, 2);
serialSettingsLayout->addWidget(labelSerialDataBits, 0, 3);
serialSettingsLayout->addWidget(cboxSerialDataBits, 0, 4);
serialSettingsLayout->addWidget(labelSerialParity, 0, 5);
serialSettingsLayout->addWidget(cboxSerialParity, 0, 6);
 
serialSettingsLayout->addWidget(btnSerialRefresh, 1, 0);
serialSettingsLayout->addWidget(labelSerialSpeed, 1, 1);
serialSettingsLayout->addWidget(cboxSerialSpeed, 1, 2);
serialSettingsLayout->addWidget(labelSerialStopBits, 1, 3);
serialSettingsLayout->addWidget(cboxSerialStopBits, 1, 4);
serialSettingsLayout->addWidget(labelSerialFlowControl, 1, 5);
serialSettingsLayout->addWidget(cboxSerialFlowControl, 1, 6);
 
groupSerialInit->setLayout(serialSettingsLayout);
groupSerialInit->setFixedSize(groupSerialInit->sizeHint());
 
// Serial data UI
groupSerialData = new QGroupBox("Data");
textSerialData = new QTextEdit();
71,7 → 28,6
serialDataLayout->addLayout(serialTransmitLayout);
groupSerialData->setLayout(serialDataLayout);
 
 
// Macro controller
groupMacro = new QGroupBox("Other");
btnMacro = new QPushButton("&Macros");
78,36 → 34,17
QHBoxLayout *macroLayout = new QHBoxLayout();
macroLayout->addWidget(btnMacro);
groupMacro->setLayout(macroLayout);
// groupMacro->setFixedSize(groupMacro->sizeHint());
 
 
// Main layout
QGridLayout *mainLayout = new QGridLayout();
mainLayout->addWidget(groupSerialInit, 0, 0, 1, 1, Qt::AlignLeft);
mainLayout->addWidget(groupMacro, 0, 1, 1, 1, Qt::AlignLeft);
mainLayout->addWidget(groupSerialData, 1, 0, 1, 2);
mainLayout->setColumnStretch(0, 0);
mainLayout->setColumnStretch(1, 1);
centralWidget->setLayout(mainLayout);
 
// Initialize thread for connection to serial port
serialThread = new QThread();
// Initialize serial controller
serialController = new SerialController();
serialController->moveToThread(serialThread);
connect(this, SIGNAL(Serial_QueryParameters()), serialController, SLOT(Serial_QueryParameters()));
connect(this, SIGNAL(Serial_Connect(QString,QString,QString,QString,QString,QString)),
serialController, SLOT(Serial_Connect(QString,QString,QString,QString,QString,QString)));
connect(this, SIGNAL(Serial_Disconnect()), serialController, SLOT(Serial_Disconnect()));
connect(this, SIGNAL(Serial_TransmitString(QString)), serialController, SLOT(Serial_TransmitString(QString)));
connect(serialController, SIGNAL(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)),
this, SLOT(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)));
connect(serialController, SIGNAL(Serial_ReceivedString(QString)), this, SLOT(Serial_ReceivedString(QString)));
connect(serialController, SIGNAL(Serial_Connected()), this, SLOT(Serial_Connected()));
connect(serialController, SIGNAL(Serial_Disconnected()), this, SLOT(Serial_Disconnected()));
connect(serialThread, SIGNAL(finished()), serialController, SLOT(deleteLater()));
serialThread->start();
groupSerialInit = new QGroupBox("Serial Connection");
QGridLayout *serialInitLayout = new QGridLayout();
serialInitLayout->setContentsMargins(0, 0, 0, 0);
serialInitLayout->addWidget(serialController);
groupSerialInit->setLayout(serialInitLayout);
connect(serialController, SIGNAL(UpdateStatus(QString)), this, SLOT(UpdateSerialStatus(QString)));
 
// Initialize macro widget
// Initialize macro controller
macroDockWidget = new QDockWidget("Macro Controller", this);
macroController = new MacroController(macroDockWidget);
macroDockWidget->setWidget(macroController);
119,35 → 56,31
connect(serialController, SIGNAL(Serial_Disconnected()), macroController, SLOT(Macro_DisableTransmit()));
 
// Connect local widgets
connect(btnSerialConnect, SIGNAL(clicked()), this, SLOT(Serial_ConnectToggleBtn()));
connect(btnSerialRefresh, SIGNAL(clicked()), this, SIGNAL(Serial_QueryParameters()));
connect(btnSerialTransmit, SIGNAL(clicked()), this, SLOT(Serial_PrepareTransmit()));
connect(textSerialTransmit, SIGNAL(returnPressed()), btnSerialTransmit, SIGNAL(clicked()));
connect(btnSerialClear, SIGNAL(clicked()), this, SLOT(Serial_ClearBtn()));
connect(btnMacro, SIGNAL(clicked()), macroDockWidget->toggleViewAction(), SLOT(trigger()));
 
emit Serial_QueryParameters();
Serial_Disconnected();
// Main layout
QGridLayout *mainLayout = new QGridLayout();
mainLayout->addWidget(groupSerialInit, 0, 0, 1, 1, Qt::AlignLeft);
mainLayout->addWidget(groupMacro, 0, 1, 1, 1, Qt::AlignLeft);
mainLayout->addWidget(groupSerialData, 1, 0, 1, 2);
mainLayout->setColumnStretch(0, 0);
mainLayout->setColumnStretch(1, 1);
centralWidget->setLayout(mainLayout);
 
setWindowTitle("Marlin Controller");
setWindowIcon(QIcon(":/External/Resources/Icon_16.bmp"));
 
statusSerial = new QLabel("Serial Status: Disconnected");
statusSerial->setMinimumWidth(300);
statusBar()->addPermanentWidget(statusSerial);
}
 
MainWindow::~MainWindow()
{
serialThread->quit();
}
 
void MainWindow::Serial_ConnectToggleBtn()
{
if (serialController->connected) {
emit Serial_Disconnect();
} else {
if (cboxSerialPort->currentText() != "" && cboxSerialSpeed->currentText() != "") {
emit Serial_Connect(cboxSerialPort->currentText(), cboxSerialSpeed->currentText(), cboxSerialDataBits->currentText(),
cboxSerialStopBits->currentText(), cboxSerialParity->currentText(), cboxSerialFlowControl->currentText());
}
}
}
 
void MainWindow::Serial_PrepareTransmit(QString string)
170,41 → 103,6
textSerialData->clear();
}
 
void MainWindow::Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
QStringList stopBits, QStringList parity, QStringList flowControl)
{
QString currPort = cboxSerialPort->currentText();
cboxSerialPort->clear();
cboxSerialPort->addItems(ports);
if (currPort != "" && ports.contains(currPort))
cboxSerialPort->setCurrentText(currPort);
 
QString currSpeed = cboxSerialSpeed->currentText();
cboxSerialSpeed->clear();
cboxSerialSpeed->addItems(speeds);
if (currSpeed != "") cboxSerialSpeed->setCurrentText(currSpeed);
 
int currData = cboxSerialDataBits->currentIndex();
cboxSerialDataBits->clear();
cboxSerialDataBits->addItems(dataBits);
if (currData >= 0) cboxSerialDataBits->setCurrentIndex(currData);
 
int currStop = cboxSerialStopBits->currentIndex();
cboxSerialStopBits->clear();
cboxSerialStopBits->addItems(stopBits);
if (currStop >= 0) cboxSerialStopBits->setCurrentIndex(currStop);
 
int currParity = cboxSerialParity->currentIndex();
cboxSerialParity->clear();
cboxSerialParity->addItems(parity);
if (currParity >= 0) cboxSerialParity->setCurrentIndex(currParity);
 
int currFlow = cboxSerialFlowControl->currentIndex();
cboxSerialFlowControl->clear();
cboxSerialFlowControl->addItems(flowControl);
if (currFlow >= 0) cboxSerialFlowControl->setCurrentIndex(currFlow);
}
 
void MainWindow::Serial_ReceivedString(QString string)
{
textSerialData->setTextColor(Qt::darkRed);
211,30 → 109,12
textSerialData->append("RX: " + string);
}
 
void MainWindow::Serial_Connected()
void MainWindow::UpdateStatus(QString string)
{
btnSerialConnect->setText("&Disconnect");
cboxSerialPort->setEnabled(false);
cboxSerialSpeed->setEnabled(false);
cboxSerialDataBits->setEnabled(false);
cboxSerialStopBits->setEnabled(false);
cboxSerialParity->setEnabled(false);
cboxSerialFlowControl->setEnabled(false);
btnSerialRefresh->setEnabled(false);
textSerialTransmit->setEnabled(true);
btnSerialTransmit->setEnabled(true);
statusBar()->showMessage(string, STATUS_TIMEOUT_MS);
}
 
void MainWindow::Serial_Disconnected()
void MainWindow::UpdateSerialStatus(QString string)
{
btnSerialConnect->setText("&Connect");
cboxSerialPort->setEnabled(true);
cboxSerialSpeed->setEnabled(true);
cboxSerialDataBits->setEnabled(true);
cboxSerialStopBits->setEnabled(true);
cboxSerialParity->setEnabled(true);
cboxSerialFlowControl->setEnabled(true);
btnSerialRefresh->setEnabled(true);
textSerialTransmit->setEnabled(false);
btnSerialTransmit->setEnabled(false);
statusSerial->setText(string);
}
/Misc Projects/PcMarlinInterface/MainWindow.h
14,21 → 14,14
~MainWindow();
 
public slots:
void Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
QStringList stopBits, QStringList parity, QStringList flowControl);
void Serial_PrepareTransmit(QString string = "");
void Serial_ReceivedString(QString string);
void Serial_Connected(void);
void Serial_Disconnected(void);
 
void Serial_ConnectToggleBtn(void);
void Serial_PrepareTransmit(QString string = "");
void Serial_ClearBtn(void);
 
void UpdateStatus(QString string);
void UpdateSerialStatus(QString string);
 
signals:
void Serial_QueryParameters(void);
void Serial_Connect(QString port, QString speed, QString dataBits,
QString stopBits, QString parity, QString flowControl);
void Serial_Disconnect(void);
void Serial_TransmitString(QString string);
 
protected:
38,24 → 31,10
 
// Serial controller + UI
SerialController *serialController;
QThread *serialThread;
 
QGroupBox *groupSerialInit;
QPushButton *btnSerialConnect;
QPushButton *btnSerialRefresh;
QLabel *labelSerialPort;
QLabel *labelSerialSpeed;
QLabel *labelSerialDataBits;
QLabel *labelSerialStopBits;
QLabel *labelSerialParity;
QLabel *labelSerialFlowControl;
QComboBox *cboxSerialPort;
QComboBox *cboxSerialSpeed;
QComboBox *cboxSerialDataBits;
QComboBox *cboxSerialStopBits;
QComboBox *cboxSerialParity;
QComboBox *cboxSerialFlowControl;
 
// Serial data
QGroupBox *groupSerialData;
QTextEdit *textSerialData;
QLineEdit *textSerialTransmit;
69,6 → 48,8
QGroupBox *groupMacro;
QPushButton *btnMacro;
 
QLabel *statusSerial;
 
};
 
#endif // MAINWINDOW_H
/Misc Projects/PcMarlinInterface/PcMarlinInterface.pro
15,12 → 15,14
SOURCES += main.cpp \
MainWindow.cpp \
SerialController.cpp \
MacroController.cpp
MacroController.cpp \
SerialHelper.cpp
 
HEADERS += GlobalDefines.h \
MainWindow.h \
SerialController.h \
MacroController.h
MacroController.h \
SerialHelper.h
 
RESOURCES += Resources.qrc
 
/Misc Projects/PcMarlinInterface/PcMarlinInterface.pro.user
1,6 → 1,6
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.3.0, 2015-01-03T20:08:30. -->
<!-- Written by QtCreator 3.3.0, 2015-01-07T23:41:29. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
61,11 → 61,11
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.4.0 MSVC2013 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.4.0 MSVC2013 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.54.win64_msvc2013_64_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/Kevin/Documents/Code/QT/PcMarlinInterface/build-PcMarlinInterface_Qt_5_4_0_MSVC2013_64bit-Debug</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/Kevin/Documents/Code/QT/PcMarlinInterface/build-PcMarlinInterface_Qt_5_4_0_MSVC2013_64bit</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
119,7 → 119,7
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/Kevin/Documents/Code/QT/PcMarlinInterface/build-PcMarlinInterface_Qt_5_4_0_MSVC2013_64bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/Kevin/Documents/Code/QT/PcMarlinInterface/build-PcMarlinInterface_Qt_5_4_0_MSVC2013_64bit</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
/Misc Projects/PcMarlinInterface/SerialController.cpp
1,140 → 1,227
#include "SerialController.h"
 
SerialController::SerialController(QObject *parent) : QObject(parent)
SerialController::SerialController(QWidget *parent) : QWidget(parent)
{
connected = false;
speeds << "9600" << "19200" << "38400" << "57600" << "115200" << "230400" << "250000";
dataBits << "8 Bits" << "7 Bits" << "6 Bits" << "5 Bits";
stopBits << "1 Bit" << "1.5 Bits" << "2 Bits";
parity << "None" << "Even" << "Odd" << "Space" << "Mark";
flowControl << "None" << "Hardware" << "Software";
bufferInIndex = 0;
bufferInOverflow = false;
// Serial connection UI
btnSerialConnect = new QPushButton("&Connect");
btnSerialRefresh = new QPushButton("&Refresh");
labelSerialPort = new QLabel("Serial Port:");
labelSerialSpeed = new QLabel("Baud Rate:");
labelSerialDataBits = new QLabel("Data:");
labelSerialStopBits = new QLabel("Stop:");
labelSerialParity = new QLabel("Parity Bit:");
labelSerialFlowControl = new QLabel("Flow Control:");
cboxSerialPort = new QComboBox();
cboxSerialPort->setMinimumWidth(80);
cboxSerialSpeed = new QComboBox();
cboxSerialSpeed->setEditable(true);
cboxSerialSpeed->setValidator(new QIntValidator(0, 100000000, this));
cboxSerialDataBits = new QComboBox();
cboxSerialDataBits->setMaximumWidth(60);
cboxSerialStopBits = new QComboBox();
cboxSerialStopBits->setMaximumWidth(60);
cboxSerialParity = new QComboBox();
cboxSerialFlowControl = new QComboBox();
cboxSerialFlowControl->setMinimumWidth(70);
 
QGridLayout *serialSettingsLayout = new QGridLayout();
serialSettingsLayout->addWidget(btnSerialConnect, 0, 0);
serialSettingsLayout->addWidget(labelSerialPort, 0, 1);
serialSettingsLayout->addWidget(cboxSerialPort, 0, 2);
serialSettingsLayout->addWidget(labelSerialDataBits, 0, 3);
serialSettingsLayout->addWidget(cboxSerialDataBits, 0, 4);
serialSettingsLayout->addWidget(labelSerialParity, 0, 5);
serialSettingsLayout->addWidget(cboxSerialParity, 0, 6);
 
serialSettingsLayout->addWidget(btnSerialRefresh, 1, 0);
serialSettingsLayout->addWidget(labelSerialSpeed, 1, 1);
serialSettingsLayout->addWidget(cboxSerialSpeed, 1, 2);
serialSettingsLayout->addWidget(labelSerialStopBits, 1, 3);
serialSettingsLayout->addWidget(cboxSerialStopBits, 1, 4);
serialSettingsLayout->addWidget(labelSerialFlowControl, 1, 5);
serialSettingsLayout->addWidget(cboxSerialFlowControl, 1, 6);
 
setLayout(serialSettingsLayout);
 
serialHelper = new SerialHelper();
serialThread = new QThread();
serialHelper->moveToThread(serialThread);
 
connect(serialHelper, SIGNAL(UpdateStatus(QString)), this, SIGNAL(UpdateStatus(QString)));
 
// Parameters
connect(this, SIGNAL(Serial_QueryParameters()), serialHelper, SLOT(Serial_QueryParameters()));
connect(serialHelper, SIGNAL(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)),
this, SLOT(Serial_UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)));
 
// Connect
connect(this, SIGNAL(Serial_Connect(QString,QString,QString,QString,QString,QString)),
serialHelper, SLOT(Serial_Connect(QString,QString,QString,QString,QString,QString)));
connect(serialHelper, SIGNAL(Serial_Connected()), this, SIGNAL(Serial_Connected()));
connect(serialHelper, SIGNAL(Serial_Connected()), this, SLOT(Serial_LocalConnected()));
 
// Disconnect
connect(this, SIGNAL(Serial_Disconnect()), serialHelper, SLOT(Serial_Disconnect()));
connect(serialHelper, SIGNAL(Serial_Disconnected()), this, SIGNAL(Serial_Disconnected()));
connect(serialHelper, SIGNAL(Serial_Disconnected()), this, SLOT(Serial_LocalDisconnected()));
 
// Transmit
connect(this, SIGNAL(Serial_TransmitString(QString)), serialHelper, SLOT(Serial_TransmitString(QString)));
connect(this, SIGNAL(Serial_TransmitByteArray(QByteArray)), serialHelper, SLOT(Serial_TransmitByteArray(QByteArray)));
 
// Receive
connect(serialHelper, SIGNAL(Serial_ReceivedByte(char)), this, SIGNAL(Serial_ReceivedByte(char)));
 
// Threading
connect(serialThread, SIGNAL(finished()), serialHelper, SLOT(deleteLater()));
serialThread->start();
 
// Local GUI
connect(btnSerialConnect, SIGNAL(clicked()), this, SLOT(Serial_ConnectToggleBtn()));
connect(btnSerialRefresh, SIGNAL(clicked()), this, SIGNAL(Serial_QueryParameters()));
 
emit Serial_QueryParameters();
Serial_LocalDisconnected();
}
 
SerialController::~SerialController()
{
serialPort->close();
delete serialPort;
serialThread->quit();
}
 
void SerialController::Serial_QueryParameters()
void SerialController::Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
QStringList stopBits, QStringList parity, QStringList flowControl)
{
QList<QSerialPortInfo> portsList = QSerialPortInfo::availablePorts();
QStringList ports;
for (int i = 0; i < portsList.size(); i++) {
ports.append(portsList[i].portName());
}
emit Serial_UpdateParameters(ports, speeds, dataBits, stopBits, parity, flowControl);
}
QString currPort = cboxSerialPort->currentText();
cboxSerialPort->clear();
cboxSerialPort->addItems(ports);
if (currPort != "" && ports.contains(currPort))
cboxSerialPort->setCurrentText(currPort);
 
void SerialController::Serial_Connect(QString port, QString speed, QString dataBits,
QString stopBits, QString parity, QString flowControl)
{
if (!connected) {
serialPort = new QSerialPort();
connect(serialPort, SIGNAL(readyRead()), this, SLOT(Serial_ProcessIncomingData()));
serialPort->setPortName(port);
if (!serialPort->open(QIODevice::ReadWrite)) {
Serial_Disconnect();
} else {
connected = true;
bool ok = false;
QString currSpeed = cboxSerialSpeed->currentText();
cboxSerialSpeed->clear();
cboxSerialSpeed->addItems(speeds);
if (currSpeed != "") cboxSerialSpeed->setCurrentText(currSpeed);
 
int baud = speed.toInt(&ok);
if (ok) serialPort->setBaudRate(baud);
int currData = cboxSerialDataBits->currentIndex();
cboxSerialDataBits->clear();
cboxSerialDataBits->addItems(dataBits);
if (currData >= 0) cboxSerialDataBits->setCurrentIndex(currData);
 
if (dataBits == "5 Bits")
serialPort->setDataBits(QSerialPort::Data5);
else if (dataBits == "6 Bits")
serialPort->setDataBits(QSerialPort::Data6);
else if (dataBits == "7 Bits")
serialPort->setDataBits(QSerialPort::Data7);
else
serialPort->setDataBits(QSerialPort::Data8);
int currStop = cboxSerialStopBits->currentIndex();
cboxSerialStopBits->clear();
cboxSerialStopBits->addItems(stopBits);
if (currStop >= 0) cboxSerialStopBits->setCurrentIndex(currStop);
 
if (stopBits == "1.5 Bits")
serialPort->setStopBits(QSerialPort::OneAndHalfStop);
else if (stopBits == "2 Bits")
serialPort->setStopBits(QSerialPort::TwoStop);
else
serialPort->setStopBits(QSerialPort::OneStop);
int currParity = cboxSerialParity->currentIndex();
cboxSerialParity->clear();
cboxSerialParity->addItems(parity);
if (currParity >= 0) cboxSerialParity->setCurrentIndex(currParity);
 
if (parity == "Even")
serialPort->setParity(QSerialPort::EvenParity);
else if (parity == "Odd")
serialPort->setParity(QSerialPort::OddParity);
else if (parity == "Space")
serialPort->setParity(QSerialPort::SpaceParity);
else if (parity == "Mark")
serialPort->setParity(QSerialPort::MarkParity);
else
serialPort->setParity(QSerialPort::NoParity);
 
if (flowControl == "Hardware")
serialPort->setFlowControl(QSerialPort::HardwareControl);
else if (flowControl == "Software")
serialPort->setFlowControl(QSerialPort::SoftwareControl);
else
serialPort->setFlowControl(QSerialPort::NoFlowControl);
 
// See http://umforum.ultimaker.com/index.php?/topic/5886-um2-controller-resetreboot-when-opening-usb-port-on-linux/
serialPort->setDataTerminalReady(1);
 
if (ok) emit Serial_Connected();
else Serial_Disconnect();
}
}
int currFlow = cboxSerialFlowControl->currentIndex();
cboxSerialFlowControl->clear();
cboxSerialFlowControl->addItems(flowControl);
if (currFlow >= 0) cboxSerialFlowControl->setCurrentIndex(currFlow);
}
 
void SerialController::Serial_Disconnect()
void SerialController::Serial_LocalConnected()
{
serialPort->close();
delete serialPort;
connected = false;
emit Serial_Disconnected();
btnSerialConnect->setText("&Disconnect");
// cboxSerialPort->setEnabled(false);
// cboxSerialSpeed->setEnabled(false);
// cboxSerialDataBits->setEnabled(false);
// cboxSerialStopBits->setEnabled(false);
// cboxSerialParity->setEnabled(false);
// cboxSerialFlowControl->setEnabled(false);
// btnSerialRefresh->setEnabled(false);
btnSerialRefresh->hide();
labelSerialPort->hide();
labelSerialSpeed->hide();
labelSerialDataBits->hide();
labelSerialStopBits->hide();
labelSerialParity->hide();
labelSerialFlowControl->hide();
cboxSerialPort->hide();
cboxSerialSpeed->hide();
cboxSerialDataBits->hide();
cboxSerialStopBits->hide();
cboxSerialParity->hide();
cboxSerialFlowControl->hide();
emit UpdateStatus("Serial: Connected");
}
 
void SerialController::Serial_TransmitString(QString string)
void SerialController::Serial_LocalDisconnected()
{
serialPort->write(string.toStdString().c_str());
serialPort->write(SERIAL_NEWLINE_CHAR);
btnSerialConnect->setText("&Connect");
// cboxSerialPort->setEnabled(true);
// cboxSerialSpeed->setEnabled(true);
// cboxSerialDataBits->setEnabled(true);
// cboxSerialStopBits->setEnabled(true);
// cboxSerialParity->setEnabled(true);
// cboxSerialFlowControl->setEnabled(true);
// btnSerialRefresh->setEnabled(true);
btnSerialRefresh->show();
labelSerialPort->show();
labelSerialSpeed->show();
labelSerialDataBits->show();
labelSerialStopBits->show();
labelSerialParity->show();
labelSerialFlowControl->show();
cboxSerialPort->show();
cboxSerialSpeed->show();
cboxSerialDataBits->show();
cboxSerialStopBits->show();
cboxSerialParity->show();
cboxSerialFlowControl->show();
emit UpdateStatus("Serial: Disconnected");
}
 
void SerialController::Serial_ProcessIncomingData()
void SerialController::Serial_ConnectToggleBtn()
{
char tmpBuffer[SERIAL_BUFFER_SIZE];
int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
while (len > 0) {
for (int i = 0; i < len; i++) {
// Save received data into local buffer
bufferIn[bufferInIndex] = tmpBuffer[i];
 
// If newline char is received, end current string
if (tmpBuffer[i] == '\n') {
if (bufferInOverflow)
currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
else
currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
bufferInOverflow = false;
bufferInIndex = 0;
emit Serial_ReceivedString(currString);
} else {
bufferInIndex++;
}
 
// If received string is larger than our serial buffer, append to previous data
if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
if (bufferInOverflow)
currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
else
currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
bufferInOverflow = true;
bufferInIndex = 0;
}
if (serialHelper->connected) {
emit Serial_Disconnect();
} else {
if (cboxSerialPort->currentText() != "" && cboxSerialSpeed->currentText() != "") {
emit Serial_Connect(cboxSerialPort->currentText(), cboxSerialSpeed->currentText(), cboxSerialDataBits->currentText(),
cboxSerialStopBits->currentText(), cboxSerialParity->currentText(), cboxSerialFlowControl->currentText());
}
// Check if there is more data to be read from the serial port
len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
}
}
 
//void SerialController::Serial_ProcessIncomingData()
//{
// char tmpBuffer[SERIAL_BUFFER_SIZE];
// int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
// while (len > 0) {
// for (int i = 0; i < len; i++) {
// // Save received data into local buffer
// bufferIn[bufferInIndex] = tmpBuffer[i];
 
// // If newline char is received, end current string
// if (tmpBuffer[i] == '\n') {
// if (bufferInOverflow)
// currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
// else
// currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
// bufferInOverflow = false;
// bufferInIndex = 0;
// emit Serial_ReceivedString(currString);
// } else {
// bufferInIndex++;
// }
 
// // If received string is larger than our serial buffer, append to previous data
// if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
// bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
// if (bufferInOverflow)
// currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
// else
// currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
// bufferInOverflow = true;
// bufferInIndex = 0;
// }
// }
// // Check if there is more data to be read from the serial port
// len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
// }
//}
/Misc Projects/PcMarlinInterface/SerialController.h
2,47 → 2,58
#define SERIALCONTROLLER_H
 
#include "GlobalDefines.h"
#include <QtSerialPort>
#include "SerialHelper.h"
 
#define SERIAL_NEWLINE_CHAR "\n"
#define SERIAL_BUFFER_SIZE 32
 
class SerialController : public QObject
class SerialController : public QWidget
{
Q_OBJECT
public:
SerialController(QObject *parent = 0);
SerialController(QWidget *parent = 0);
~SerialController();
 
bool connected;
public slots:
// Inbound from SerialHelper
void Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
QStringList stopBits, QStringList parity, QStringList flowControl);
 
public slots:
// Local GUI processing
void Serial_ConnectToggleBtn(void);
void Serial_LocalConnected(void);
void Serial_LocalDisconnected(void);
 
signals:
// Outbound to SerialHelper
void Serial_QueryParameters(void);
void Serial_Connect(QString port, QString speed, QString dataBits,
QString stopBits, QString parity, QString flowControl);
void Serial_Disconnect(void);
void Serial_TransmitString(QString string);
void Serial_ProcessIncomingData(void);
void Serial_TransmitByteArray(QByteArray buffer);
 
signals:
void Serial_UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
QStringList stopBits, QStringList parity, QStringList flowControl);
void Serial_ReceivedString(QString string);
// Outbound to MainWindow
void Serial_ReceivedByte(char byte);
void Serial_Connected(void);
void Serial_Disconnected(void);
void UpdateStatus(QString string);
 
private:
QSerialPort *serialPort;
QStringList speeds;
QStringList dataBits;
QStringList stopBits;
QStringList parity;
QStringList flowControl;
SerialHelper *serialHelper;
QThread *serialThread;
 
char bufferIn[SERIAL_BUFFER_SIZE];
int bufferInIndex;
bool bufferInOverflow;
QString currString;
QPushButton *btnSerialConnect;
QPushButton *btnSerialRefresh;
QLabel *labelSerialPort;
QLabel *labelSerialSpeed;
QLabel *labelSerialDataBits;
QLabel *labelSerialStopBits;
QLabel *labelSerialParity;
QLabel *labelSerialFlowControl;
QComboBox *cboxSerialPort;
QComboBox *cboxSerialSpeed;
QComboBox *cboxSerialDataBits;
QComboBox *cboxSerialStopBits;
QComboBox *cboxSerialParity;
QComboBox *cboxSerialFlowControl;
};
 
#endif // SERIALCONTROLLER_H