QT_USE_NAMESPACE int main(int argc, char *argv[]) { QCoreApplication coreApplication(argc, argv); int argumentCount = QCoreApplication::arguments().size(); QStringList argumentList = QCoreApplication::arguments(); QTextStream standardOutput(stdout); if (argumentCount == 1) { standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]").arg(argumentList.first()) << endl; return 1; } QSerialPort serialPort; QString serialPortName = argumentList.at(1); serialPort.setPortName(serialPortName); int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; serialPort.setBaudRate(serialPortBaudRate); if (!serialPort.open(QIODevice::ReadOnly)) { standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } SerialPortReader serialPortReader(&serialPort); return coreApplication.exec(); }
QT_USE_NAMESPACE int main(int argc, char *argv[]) { QCoreApplication coreApplication(argc, argv); int argumentCount = QCoreApplication::arguments().size(); QStringList argumentList = QCoreApplication::arguments(); QTextStream standardOutput(stdout); if (argumentCount == 1) { standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]").arg(argumentList.first()) << endl; return 1; } QSerialPort serialPort; QString serialPortName = argumentList.at(1); serialPort.setPortName(serialPortName); int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; serialPort.setBaudRate(serialPortBaudRate); if (!serialPort.open(QIODevice::WriteOnly)) { standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } QFile dataFile; if (!dataFile.open(stdin, QIODevice::ReadOnly)) { standardOutput << QObject::tr("Failed to open stdin for reading") << endl; return 1; } QByteArray writeData(dataFile.readAll()); dataFile.close(); if (writeData.isEmpty()) { standardOutput << QObject::tr("Either no data was currently available on the standard input for reading, or an error occurred for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } qint64 bytesWritten = serialPort.write(writeData); if (bytesWritten == -1) { standardOutput << QObject::tr("Failed to write the data to port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } else if (bytesWritten != writeData.size()) { standardOutput << QObject::tr("Failed to write all the data to port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } else if (!serialPort.waitForBytesWritten(5000)) { standardOutput << QObject::tr("Operation timed out or an error occurred for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } standardOutput << QObject::tr("Data successfully sent to port %1").arg(serialPortName) << endl; return 0; }
bool versionControl::runMercurial(QString options) { if (!this->isMercurialThere) return false; QSettings settings; // get current model path from QSettings QString path = settings.value("files/currentFileName", "No model").toString(); if (path == "No model") return false; // set up the environment for the spawned processes QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("PATH", qgetenv("PATH")); // to be used as a username if one is not set env.insert("EMAIL", QHostInfo::localHostName()); // make the QProcess QProcess * mercurial = new QProcess; mercurial->setWorkingDirectory(QDir::toNativeSeparators(path)); mercurial->setProcessEnvironment(env); connect(mercurial, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus))); connect(mercurial, SIGNAL(readyReadStandardOutput()), this, SLOT(standardOutput())); connect(mercurial, SIGNAL(readyReadStandardError()), this, SLOT(standardError())); // clear stdOut & stdErr texts stdOutText.clear(); stdErrText.clear(); // launch mercurial->start("hg " + options); // wait until complete, or 3 s bool noerror = mercurial->waitForFinished(3000); if (noerror) { // check output if (mercurial->exitCode() == 0) { delete mercurial; return true; } else { delete mercurial; return false; } } delete mercurial; return false; }
QT_USE_NAMESPACE int main(int argc, char *argv[]) { QCoreApplication coreApplication(argc, argv); int argumentCount = QCoreApplication::arguments().size(); QStringList argumentList = QCoreApplication::arguments(); QTextStream standardOutput(stdout); if (argumentCount == 1) { standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]").arg(argumentList.first()) << endl; return 1; } QSerialPort serialPort; QString serialPortName = argumentList.at(1); serialPort.setPortName(serialPortName); int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; serialPort.setBaudRate(serialPortBaudRate); if (!serialPort.open(QIODevice::ReadOnly)) { standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.error()) << endl; return 1; } QByteArray readData = serialPort.readAll(); while (serialPort.waitForReadyRead(5000)) readData.append(serialPort.readAll()); if (serialPort.error() == QSerialPort::ReadError) { standardOutput << QObject::tr("Failed to read from port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } else if (serialPort.error() == QSerialPort::TimeoutError && readData.isEmpty()) { standardOutput << QObject::tr("No data was currently available for reading from port %1").arg(serialPortName) << endl; return 0; } standardOutput << QObject::tr("Data successfully received from port %1").arg(serialPortName) << endl; standardOutput << readData << endl; return 0; }
QT_USE_NAMESPACE int main(int argc, char *argv[]) { QCoreApplication coreApplication(argc, argv); int argumentCount = QCoreApplication::arguments().size(); QStringList argumentList = QCoreApplication::arguments(); QTextStream standardOutput(stdout); if (argumentCount == 1) { standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]").arg(argumentList.first()) << endl; return 1; } QSerialPort serialPort; QString serialPortName = argumentList.at(1); serialPort.setPortName(serialPortName); int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600; serialPort.setBaudRate(serialPortBaudRate); serialPort.open(QIODevice::WriteOnly); QFile dataFile; if (!dataFile.open(stdin, QIODevice::ReadOnly)) { standardOutput << QObject::tr("Failed to open stdin for reading") << endl; return 1; } QByteArray writeData(dataFile.readAll()); dataFile.close(); if (writeData.isEmpty()) { standardOutput << QObject::tr("Either no data was currently available on the standard input for reading, or an error occurred for port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl; return 1; } SerialPortWriter serialPortWriter(&serialPort); serialPortWriter.write(writeData); return coreApplication.exec(); }
bool versionControl::isModelUnderMercurial() { if (!this->isMercurialThere) return false; QSettings settings; // get current model path from QSettings QString path = settings.value("files/currentFileName", "No model").toString(); if (path == "No model") return false; // set up the environment for the spawned processes QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("PATH", qgetenv("PATH")); // make the QProcess QProcess * mercurial = new QProcess; mercurial->setWorkingDirectory(QDir::toNativeSeparators(path)); mercurial->setProcessEnvironment(env); connect(mercurial, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus))); connect(mercurial, SIGNAL(readyReadStandardOutput()), this, SLOT(standardOutput())); connect(mercurial, SIGNAL(readyReadStandardError()), this, SLOT(standardError())); mercurial->start("hg summary"); bool noerror = mercurial->waitForFinished(1000); if (noerror) { // check output if (mercurial->exitCode() == 0) return true; else return false; } return false; }
void CPort::readDataFromPort() { time_t now; struct tm tstruct; char buf[80]; QTextStream standardOutput(stdout); QByteArray readData; while (true) { pSerialPort->waitForReadyRead(3000); readData = pSerialPort->readAll(); if (!readData.isEmpty()) { now = time(0); tstruct = *localtime(&now); strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct); if (pTable->InsertToBase(QString::fromUtf8(buf),mPortName,readData)) { standardOutput << QObject::tr("You get data from the port %1 :").arg(mPortName)<< readData << ", in the " << buf << ".The info is loading to database!" << endl; } } } }
void versionControl::detectVCSes() { // check for mercurial // set up the environment for the spawned processes QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("PATH", qgetenv("PATH")); // make the QProcess QProcess * mercurial = new QProcess; mercurial->setWorkingDirectory(qgetenv("HOME")); mercurial->setProcessEnvironment(env); connect(mercurial, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus))); connect(mercurial, SIGNAL(readyReadStandardOutput()), this, SLOT(standardOutput())); connect(mercurial, SIGNAL(readyReadStandardError()), this, SLOT(standardError())); mercurial->start("hg"); isMercurialThere = mercurial->waitForFinished(1000); delete mercurial; }