main.cpp 5.06 KB
Newer Older
James Goppert's avatar
James Goppert committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/*
 * Unofficial Qt Serial Port Library
 *
 * Copyright (c) 2010 Inbiza Systems Inc. All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * author labs@inbiza.com
 */

#include <QtCore/QCoreApplication>
#include <QStringList>
#include <QDebug>
#include <QThread>
#include <QSerialPort>
#include <iostream>
#include "receiver.h"
#include "transmitter.h"
#include <QDateTime>

void printHelp()
{
  std::cout << "Usage: tranceiver [--help] [--role= transmit|receive] [--serport=<serial port>] " \
               "[--listen=<port>] [--remotehost=<ip:port>] [--buffersize=<number of bytes>|*]" << std::endl;
  std::cout << "==========" << std::endl;
  std::cout << "Example: tranceiver --role=transmit --serport=/dev/ttySAC0 --listen=8765 --remotehost=193.168.0.102:8765" << std::endl;
  std::cout << "Example: tranceiver --role=receive --serport=/dev/ttyUSB0 --listen=8765 --remotehost=192.168.0.23:8765 --buffersize=*" << std::endl;
  std::cout << "==========" << std::endl;
}

bool processArgs(const QStringList &args, QString &role, QString &serPort, QString &udpPort,
                 QString &remoteHost, QString &bufferSize)
{
  QHash<QString, QString> argPairs;

  if ( args.size() < 2 )
    goto LPrintHelpAndExit;

  foreach( QString ar, args ) {
    if ( ar.split('=').size() == 2 )
      argPairs.insert(ar.split('=').at(0), ar.split('=').at(1));
    else {
      if ( ar == "--help" )
        goto LPrintHelpAndExit;
    }
  }

#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
    serPort = argPairs.value("--serport", "com3");
#else
    serPort = argPairs.value("--serport", "/dev/ttyS0");
#endif

  role = argPairs.value("--role", "transmit");

  if ( role == "transmit" ) {
    remoteHost = argPairs.value("--remotehost", "localhost:8756");
    udpPort = argPairs.value("--listen", "9766");
  }
  else if ( role == "receive" ) {
    remoteHost = argPairs.value("--remotehost", "localhost:9766");
    udpPort = argPairs.value("--listen", "8756");
    bufferSize = argPairs.value("--buffersize", "*");
  }
  else {
    // invalid role
    std::cout << "Invalid role specified." << std::endl;
    goto LPrintHelpAndExit;
  }
  return true;

LPrintHelpAndExit:
  printHelp();
  return false;
}

int main(int argc, char **argv)
{
  QString role;
  QString serPort;
  QString udpPort;
  QString remoteHost;
  QString bufferSize;
  QCoreApplication a(argc, argv);

  std::cout << "Serial port transmitter/receiver example, Copyright (c) 2010 Inbiza Systems Inc." << std::endl;
  std::cout << "Created by Inbiza Labs <labs@inbiza.com>" << std::endl;
  std::cout << "==========" << std::endl;
  std::cout << "Connect two networked computers with null-modem cable, run tranceiver with \"--role=transmit\" on one, and with \"--role=receive\" on ther other." << std::endl;
  std::cout << "==========" << std::endl;
  std::cout << std::endl;

  if ( !processArgs(a.arguments(), role, serPort, udpPort, remoteHost, bufferSize) )
    return 0;

  qDebug() << "Mode:" << role << ", Serial Port:" << serPort << ", Port Settings:" << "9600,8,n,1" <<
              ", Listening at port: " << udpPort << ", Testing peer at: " << remoteHost << ", Buffer size: " << (bufferSize == "*" ? "equal to data set size" : bufferSize);

  TNX::QSerialPort serialPort(serPort, "9600,8,n,1");

  TNX::CommTimeouts commTimeouts;

  commTimeouts.PosixVMIN = 1;
  commTimeouts.PosixVTIME = 0;
  commTimeouts.Win32ReadIntervalTimeout = 75;
  commTimeouts.Win32ReadTotalTimeoutConstant = 250;
  commTimeouts.Win32ReadTotalTimeoutMultiplier = 25;
  commTimeouts.Win32WriteTotalTimeoutConstant = 250;
  commTimeouts.Win32WriteTotalTimeoutMultiplier = 75;

  if ( !serialPort.setCommTimeouts(commTimeouts) )
    qWarning("Cannot set communications timeout values at port %s.", qPrintable(serPort));

  Receiver *receiver = NULL;
  Transmitter *transmitter = NULL;
  if ( 0 == role.compare("transmit", Qt::CaseInsensitive) ) {
    transmitter = new Transmitter(serialPort, remoteHost, udpPort.toInt(), &a);
    if ( !transmitter->start() ) {
      std::cout << "Cannot start transmitter. Quitting." << std::endl;
      return 0;
    }

  }
  else if ( 0 == role.compare("receive", Qt::CaseInsensitive) ) {
    receiver = new Receiver(serialPort,  remoteHost, udpPort.toInt(), QString((bufferSize == "*" ? "0" : bufferSize)).toInt(), &a);
    if ( !receiver->start() ) {
      std::cout << "Cannot start receiver. Quitting." << std::endl;
      return 0;
    }
  }
  else {
    std::cout << "Wrong role defined. Quitting." << std::endl;
    return 0;
  }

  return a.exec();
}