地面站终端 App
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

888 lines
21 KiB

15 years ago
/*=====================================================================
======================================================================*/
/**
* @file
* @brief Cross-platform support for serial ports
*
* @author Lorenz Meier <mavteam@student.ethz.ch>
*
*/
#include <QTimer>
#include <QDebug>
#include <QSettings>
15 years ago
#include <QMutexLocker>
#include "SerialLink.h"
#include "LinkManager.h"
#include "QGC.h"
15 years ago
#include <MG.h>
#include <iostream>
15 years ago
#ifdef _WIN32
#include "windows.h"
#endif
using namespace TNX;
//#define USE_QEXTSERIAL // this allows us to revert to old serial library during transition
15 years ago
SerialLink::SerialLink(QString portname, int baudRate, bool hardwareFlowControl, bool parity,
int dataBits, int stopBits) :
port(NULL)
15 years ago
{
// Setup settings
this->porthandle = portname.trimmed();
#ifdef _WIN32
// Port names above 20 need the network path format - if the port name is not already in this format
// catch this special case
if (this->porthandle.size() > 0 && !this->porthandle.startsWith("\\")) {
15 years ago
// Append \\.\ before the port handle. Additional backslashes are used for escaping.
this->porthandle = "\\\\.\\" + this->porthandle;
}
#endif
// Set unique ID and add link to the list of links
this->id = getNextLinkId();
setBaudRate(baudRate);
if (hardwareFlowControl)
{
portSettings.setFlowControl(QPortSettings::FLOW_HARDWARE);
}
else
{
portSettings.setFlowControl(QPortSettings::FLOW_OFF);
}
if (parity)
{
portSettings.setParity(QPortSettings::PAR_EVEN);
}
else
{
portSettings.setParity(QPortSettings::PAR_NONE);
}
setDataBits(dataBits);
setStopBits(stopBits);
15 years ago
// Set the port name
if (porthandle == "")
{
name = tr("Serial Link ") + QString::number(getId());
15 years ago
}
else
{
name = portname.trimmed();
15 years ago
}
loadSettings();
15 years ago
}
SerialLink::~SerialLink()
{
disconnect();
if(port) delete port;
15 years ago
port = NULL;
}
void SerialLink::loadSettings()
{
// Load defaults from settings
QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
settings.sync();
if (settings.contains("SERIALLINK_COMM_PORT")) {
if (porthandle == "") setPortName(settings.value("SERIALLINK_COMM_PORT").toString());
setBaudRateType(settings.value("SERIALLINK_COMM_BAUD").toInt());
setParityType(settings.value("SERIALLINK_COMM_PARITY").toInt());
setStopBits(settings.value("SERIALLINK_COMM_STOPBITS").toInt());
setDataBits(settings.value("SERIALLINK_COMM_DATABITS").toInt());
setFlowType(settings.value("SERIALLINK_COMM_FLOW_CONTROL").toInt());
}
}
void SerialLink::writeSettings()
{
// Store settings
QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
settings.setValue("SERIALLINK_COMM_PORT", this->porthandle);
settings.setValue("SERIALLINK_COMM_BAUD", getBaudRateType());
settings.setValue("SERIALLINK_COMM_PARITY", getParityType());
settings.setValue("SERIALLINK_COMM_STOPBITS", getStopBits());
settings.setValue("SERIALLINK_COMM_DATABITS", getDataBits());
settings.setValue("SERIALLINK_COMM_FLOW_CONTROL", getFlowType());
settings.sync();
}
15 years ago
/**
* @brief Runs the thread
*
**/
void SerialLink::run()
{
15 years ago
// Initialize the connection
hardwareConnect();
// Qt way to make clear what a while(1) loop does
forever {
15 years ago
// Check if new bytes have arrived, if yes, emit the notification signal
checkForBytes();
/* Serial data isn't arriving that fast normally, this saves the thread
* from consuming too much processing time
*/
MG::SLEEP::msleep(SerialLink::poll_interval);
}
}
void SerialLink::checkForBytes()
{
15 years ago
/* Check if bytes are available */
if(port && port->isOpen() && port->isWritable()) {
15 years ago
dataMutex.lock();
qint64 available = port->bytesAvailable();
dataMutex.unlock();
if(available > 0) {
readBytes();
15 years ago
}
} else {
15 years ago
emit disconnected();
}
}
void SerialLink::writeBytes(const char* data, qint64 size)
{
if(port && port->isOpen()) {
15 years ago
int b = port->write(data, size);
if (b > 0) {
// qDebug() << "Serial link " << this->getName() << "transmitted" << b << "bytes:";
15 years ago
// Increase write counter
bitsSentTotal += size * 8;
// int i;
// for (i=0; i<size; i++)
// {
// unsigned char v =data[i];
// qDebug("%02x ", v);
// }
// qDebug("\n");
} else {
disconnect();
// Error occured
emit communicationError(this->getName(), tr("Could not send data - link %1 is disconnected!").arg(this->getName()));
}
15 years ago
}
}
/**
* @brief Read a number of bytes from the interface.
*
* @param data Pointer to the data byte array to write the bytes to
* @param maxLength The maximum number of bytes to write
**/
void SerialLink::readBytes()
{
15 years ago
dataMutex.lock();
if(port && port->isOpen()) {
const qint64 maxLength = 2048;
char data[maxLength];
15 years ago
qint64 numBytes = port->bytesAvailable();
14 years ago
//qDebug() << "numBytes: " << numBytes;
if(numBytes > 0) {
15 years ago
/* Read as much data in buffer as possible without overflow */
if(maxLength < numBytes) numBytes = maxLength;
port->read(data, numBytes);
QByteArray b(data, numBytes);
emit bytesReceived(this, b);
//qDebug() << "SerialLink::readBytes()" << std::hex << data;
// int i;
// for (i=0; i<numBytes; i++){
// unsigned int v=data[i];
//
// fprintf(stderr,"%02x ", v);
// }
// fprintf(stderr,"\n");
bitsReceivedTotal += numBytes * 8;
}
}
dataMutex.unlock();
}
/**
* @brief Get the number of bytes to read.
*
* @return The number of bytes to read
**/
qint64 SerialLink::bytesAvailable()
{
if (port) {
return port->bytesAvailable();
} else {
return 0;
}
15 years ago
}
/**
* @brief Disconnect the connection.
*
* @return True if connection has been disconnected, false if connection couldn't be disconnected.
**/
bool SerialLink::disconnect()
{
if (port) {
//#if !defined _WIN32 || !defined _WIN64
/* Block the thread until it returns from run() */
//#endif
// dataMutex.lock();
port->flushInBuffer();
port->flushOutBuffer();
port->close();
delete port;
port = NULL;
// dataMutex.unlock();
15 years ago
if(this->isRunning()) this->terminate(); //stop running the thread, restart it upon connect
bool closed = true;
//port->isOpen();
15 years ago
emit disconnected();
emit connected(false);
return closed;
} else {
// No port, so we're disconnected
return true;
}
15 years ago
}
/**
* @brief Connect the connection.
*
* @return True if connection has been established, false if connection couldn't be established.
**/
bool SerialLink::connect()
{
if (this->isRunning()) this->disconnect();
this->start(LowPriority);
return true;
15 years ago
}
/**
* @brief This function is called indirectly by the connect() call.
*
* The connect() function starts the thread and indirectly calls this method.
*
* @return True if the connection could be established, false otherwise
* @see connect() For the right function to establish the connection.
**/
bool SerialLink::hardwareConnect()
{
if(port) {
port->close();
delete port;
}
port = new QSerialPort(porthandle, portSettings);
QObject::connect(port,SIGNAL(aboutToClose()),this,SIGNAL(disconnected()));
port->setCommTimeouts(QSerialPort::CtScheme_NonBlockingRead);
15 years ago
connectionStartTime = MG::TIME::getGroundTimeNow();
port->open();
15 years ago
bool connectionUp = isConnected();
if(connectionUp) {
15 years ago
emit connected();
emit connected(true);
}
//qDebug() << "CONNECTING LINK: " << __FILE__ << __LINE__ << "with settings" << port->portName() << getBaudRate() << getDataBits() << getParityType() << getStopBits();
writeSettings();
15 years ago
return connectionUp;
}
15 years ago
/**
* @brief Check if connection is active.
*
* @return True if link is connected, false otherwise.
**/
bool SerialLink::isConnected()
{
if (port) {
return port->isOpen();
} else {
return false;
}
15 years ago
}
int SerialLink::getId()
{
return id;
}
QString SerialLink::getName()
{
return name;
}
void SerialLink::setName(QString name)
{
this->name = name;
emit nameChanged(this->name);
}
qint64 SerialLink::getNominalDataRate()
{
15 years ago
qint64 dataRate = 0;
switch (portSettings.baudRate()) {
#ifndef Q_OS_WIN
case QPortSettings::BAUDR_50:
15 years ago
dataRate = 50;
break;
case QPortSettings::BAUDR_75:
15 years ago
dataRate = 75;
break;
case QPortSettings::BAUDR_110:
15 years ago
dataRate = 110;
break;
case QPortSettings::BAUDR_134:
15 years ago
dataRate = 134;
break;
case QPortSettings::BAUDR_150:
15 years ago
dataRate = 150;
break;
case QPortSettings::BAUDR_200:
15 years ago
dataRate = 200;
break;
#endif
case QPortSettings::BAUDR_300:
15 years ago
dataRate = 300;
break;
case QPortSettings::BAUDR_600:
15 years ago
dataRate = 600;
break;
case QPortSettings::BAUDR_1200:
15 years ago
dataRate = 1200;
break;
#ifndef Q_OS_WIN
case QPortSettings::BAUDR_1800:
15 years ago
dataRate = 1800;
break;
#endif
case QPortSettings::BAUDR_2400:
15 years ago
dataRate = 2400;
break;
case QPortSettings::BAUDR_4800:
15 years ago
dataRate = 4800;
break;
case QPortSettings::BAUDR_9600:
15 years ago
dataRate = 9600;
break;
#ifdef Q_OS_WIN
case QPortSettings::BAUDR_14400:
15 years ago
dataRate = 14400;
break;
#endif
case QPortSettings::BAUDR_19200:
15 years ago
dataRate = 19200;
break;
case QPortSettings::BAUDR_38400:
15 years ago
dataRate = 38400;
break;
#ifdef Q_OS_WIN
case QPortSettings::BAUDR_56000:
15 years ago
dataRate = 56000;
break;
#endif
case QPortSettings::BAUDR_57600:
15 years ago
dataRate = 57600;
break;
#ifdef Q_OS_WIN_XXXX // FIXME
case QPortSettings::BAUDR_76800:
15 years ago
dataRate = 76800;
break;
#endif
case QPortSettings::BAUDR_115200:
15 years ago
dataRate = 115200;
break;
#ifdef Q_OS_WIN
// Windows-specific high-end baudrates
case QPortSettings::BAUDR_128000:
15 years ago
dataRate = 128000;
break;
case QPortSettings::BAUDR_256000:
15 years ago
dataRate = 256000;
case QPortSettings::BAUDR_230400:
dataRate = 230400;
case QPortSettings::BAUDR_460800:
dataRate = 460800;
#endif
// All-OS high-speed
case QPortSettings::BAUDR_921600:
dataRate = 921600;
15 years ago
break;
case QPortSettings::BAUDR_UNKNOWN:
default:
// Do nothing
break;
15 years ago
}
return dataRate;
}
qint64 SerialLink::getTotalUpstream()
{
15 years ago
statisticsMutex.lock();
return bitsSentTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
statisticsMutex.unlock();
}
qint64 SerialLink::getCurrentUpstream()
{
15 years ago
return 0; // TODO
}
qint64 SerialLink::getMaxUpstream()
{
15 years ago
return 0; // TODO
}
qint64 SerialLink::getBitsSent()
{
15 years ago
return bitsSentTotal;
}
qint64 SerialLink::getBitsReceived()
{
15 years ago
return bitsReceivedTotal;
}
qint64 SerialLink::getTotalDownstream()
{
15 years ago
statisticsMutex.lock();
return bitsReceivedTotal / ((MG::TIME::getGroundTimeNow() - connectionStartTime) / 1000);
statisticsMutex.unlock();
}
qint64 SerialLink::getCurrentDownstream()
{
15 years ago
return 0; // TODO
}
qint64 SerialLink::getMaxDownstream()
{
15 years ago
return 0; // TODO
}
bool SerialLink::isFullDuplex()
{
15 years ago
/* Serial connections are always half duplex */
return false;
}
int SerialLink::getLinkQuality()
{
15 years ago
/* This feature is not supported with this interface */
return -1;
}
QString SerialLink::getPortName()
{
15 years ago
return porthandle;
}
int SerialLink::getBaudRate()
{
15 years ago
return getNominalDataRate();
}
int SerialLink::getBaudRateType()
{
return portSettings.baudRate();
15 years ago
}
int SerialLink::getFlowType()
{
return portSettings.flowControl();
15 years ago
}
int SerialLink::getParityType()
{
return portSettings.parity();
15 years ago
}
int SerialLink::getDataBitsType()
{
return portSettings.dataBits();
15 years ago
}
int SerialLink::getStopBitsType()
{
return portSettings.stopBits();
15 years ago
}
int SerialLink::getDataBits()
{
int ret = -1;
switch (portSettings.dataBits()) {
case QPortSettings::DB_5:
ret = 5;
break;
case QPortSettings::DB_6:
ret = 6;
break;
case QPortSettings::DB_7:
ret = 7;
break;
case QPortSettings::DB_8:
ret = 8;
break;
default:
ret = -1;
break;
}
return ret;
}
int SerialLink::getStopBits()
{
int ret = -1;
switch (portSettings.stopBits()) {
case QPortSettings::STOP_1:
ret = 1;
break;
case QPortSettings::STOP_2:
ret = 2;
break;
default:
ret = -1;
break;
}
return ret;
}
15 years ago
bool SerialLink::setPortName(QString portName)
{
if(portName.trimmed().length() > 0) {
15 years ago
bool reconnect = false;
if (isConnected()) reconnect = true;
disconnect();
15 years ago
this->porthandle = portName.trimmed();
setName(tr("serial port ") + portName.trimmed());
#ifdef _WIN32
// Port names above 20 need the network path format - if the port name is not already in this format
// catch this special case
if (!this->porthandle.startsWith("\\")) {
15 years ago
// Append \\.\ before the port handle. Additional backslashes are used for escaping.
this->porthandle = "\\\\.\\" + this->porthandle;
}
#endif
15 years ago
if(reconnect) connect();
return true;
} else {
15 years ago
return false;
}
}
bool SerialLink::setBaudRateType(int rateIndex)
{
bool reconnect = false;
bool accepted = true; // This is changed if none of the data rates matches
if(isConnected()) reconnect = true;
disconnect();
#ifdef Q_OS_WIN
const int minBaud = (int)QPortSettings::BAUDR_14400;
#else
const int minBaud = (int)QPortSettings::BAUDR_50;
#endif
if (rateIndex >= minBaud && rateIndex <= (int)QPortSettings::BAUDR_921600)
{
portSettings.setBaudRate((QPortSettings::BaudRate)rateIndex);
15 years ago
}
if(reconnect) connect();
return accepted;
}
bool SerialLink::setBaudRateString(const QString& rate)
{
bool ok;
int intrate = rate.toInt(&ok);
if (!ok) return false;
return setBaudRate(intrate);
}
15 years ago
bool SerialLink::setBaudRate(int rate)
{
//qDebug() << "BAUD RATE:" << rate;
15 years ago
bool reconnect = false;
bool accepted = true; // This is changed if none of the data rates matches
if(isConnected()) {
15 years ago
reconnect = true;
}
disconnect();
15 years ago
switch (rate) {
#ifndef Q_OS_WIN
15 years ago
case 50:
portSettings.setBaudRate(QPortSettings::BAUDR_50);
15 years ago
break;
case 75:
portSettings.setBaudRate(QPortSettings::BAUDR_75);
15 years ago
break;
case 110:
portSettings.setBaudRate(QPortSettings::BAUDR_110);
15 years ago
break;
case 134:
portSettings.setBaudRate(QPortSettings::BAUDR_134);
15 years ago
break;
case 150:
portSettings.setBaudRate(QPortSettings::BAUDR_150);
15 years ago
break;
case 200:
portSettings.setBaudRate(QPortSettings::BAUDR_200);
15 years ago
break;
#endif
15 years ago
case 300:
portSettings.setBaudRate(QPortSettings::BAUDR_300);
15 years ago
break;
case 600:
portSettings.setBaudRate(QPortSettings::BAUDR_600);
15 years ago
break;
case 1200:
portSettings.setBaudRate(QPortSettings::BAUDR_1200);
15 years ago
break;
#ifndef Q_OS_WIN
15 years ago
case 1800:
portSettings.setBaudRate(QPortSettings::BAUDR_1800);
15 years ago
break;
#endif
15 years ago
case 2400:
portSettings.setBaudRate(QPortSettings::BAUDR_2400);
15 years ago
break;
case 4800:
portSettings.setBaudRate(QPortSettings::BAUDR_4800);
15 years ago
break;
case 9600:
portSettings.setBaudRate(QPortSettings::BAUDR_9600);
15 years ago
break;
#ifdef Q_OS_WIN
15 years ago
case 14400:
portSettings.setBaudRate(QPortSettings::BAUDR_14400);
15 years ago
break;
#endif
15 years ago
case 19200:
portSettings.setBaudRate(QPortSettings::BAUDR_19200);
15 years ago
break;
case 38400:
portSettings.setBaudRate(QPortSettings::BAUDR_38400);
15 years ago
break;
#ifdef Q_OS_WIN
15 years ago
case 56000:
portSettings.setBaudRate(QPortSettings::BAUDR_56000);
15 years ago
break;
#endif
15 years ago
case 57600:
portSettings.setBaudRate(QPortSettings::BAUDR_57600);
15 years ago
break;
#ifdef Q_OS_WIN_XXXX // FIXME CHECK THIS
15 years ago
case 76800:
portSettings.setBaudRate(QPortSettings::BAUDR_76800);
15 years ago
break;
#endif
15 years ago
case 115200:
portSettings.setBaudRate(QPortSettings::BAUDR_115200);
15 years ago
break;
#ifdef Q_OS_WIN
15 years ago
case 128000:
portSettings.setBaudRate(QPortSettings::BAUDR_128000);
15 years ago
break;
case 230400:
portSettings.setBaudRate(QPortSettings::BAUDR_230400);
break;
15 years ago
case 256000:
portSettings.setBaudRate(QPortSettings::BAUDR_256000);
15 years ago
break;
case 460800:
portSettings.setBaudRate(QPortSettings::BAUDR_460800);
break;
#endif
case 921600:
portSettings.setBaudRate(QPortSettings::BAUDR_921600);
break;
15 years ago
default:
// If none of the above cases matches, there must be an error
accepted = false;
break;
}
if(reconnect) connect();
return accepted;
15 years ago
}
bool SerialLink::setFlowType(int flow)
{
15 years ago
bool reconnect = false;
bool accepted = true;
if(isConnected()) reconnect = true;
disconnect();
15 years ago
switch (flow) {
case (int)QPortSettings::FLOW_OFF:
portSettings.setFlowControl(QPortSettings::FLOW_OFF);
15 years ago
break;
case (int)QPortSettings::FLOW_HARDWARE:
portSettings.setFlowControl(QPortSettings::FLOW_HARDWARE);
15 years ago
break;
case (int)QPortSettings::FLOW_XONXOFF:
portSettings.setFlowControl(QPortSettings::FLOW_XONXOFF);
15 years ago
break;
default:
// If none of the above cases matches, there must be an error
accepted = false;
break;
}
15 years ago
if(reconnect) connect();
return accepted;
}
bool SerialLink::setParityType(int parity)
{
15 years ago
bool reconnect = false;
bool accepted = true;
if (isConnected()) reconnect = true;
disconnect();
15 years ago
switch (parity) {
case (int)QPortSettings::PAR_NONE:
portSettings.setParity(QPortSettings::PAR_NONE);
15 years ago
break;
case (int)QPortSettings::PAR_ODD:
portSettings.setParity(QPortSettings::PAR_ODD);
15 years ago
break;
case (int)QPortSettings::PAR_EVEN:
portSettings.setParity(QPortSettings::PAR_EVEN);
15 years ago
break;
case (int)QPortSettings::PAR_SPACE:
portSettings.setParity(QPortSettings::PAR_SPACE);
15 years ago
break;
default:
// If none of the above cases matches, there must be an error
accepted = false;
break;
}
if (reconnect) connect();
15 years ago
return accepted;
}
bool SerialLink::setDataBits(int dataBits)
{
//qDebug() << "Setting" << dataBits << "data bits";
bool reconnect = false;
if (isConnected()) reconnect = true;
15 years ago
bool accepted = true;
disconnect();
15 years ago
switch (dataBits) {
15 years ago
case 5:
portSettings.setDataBits(QPortSettings::DB_5);
15 years ago
break;
case 6:
portSettings.setDataBits(QPortSettings::DB_6);
15 years ago
break;
case 7:
portSettings.setDataBits(QPortSettings::DB_7);
15 years ago
break;
case 8:
portSettings.setDataBits(QPortSettings::DB_8);
15 years ago
break;
default:
// If none of the above cases matches, there must be an error
accepted = false;
break;
}
if(reconnect) connect();
15 years ago
return accepted;
}
bool SerialLink::setStopBits(int stopBits)
{
15 years ago
bool reconnect = false;
bool accepted = true;
if(isConnected()) reconnect = true;
disconnect();
15 years ago
switch (stopBits) {
15 years ago
case 1:
portSettings.setStopBits(QPortSettings::STOP_1);
15 years ago
break;
case 2:
portSettings.setStopBits(QPortSettings::STOP_2);
15 years ago
break;
default:
// If none of the above cases matches, there must be an error
accepted = false;
break;
}
if(reconnect) connect();
return accepted;
}
bool SerialLink::setDataBitsType(int dataBits)
{
bool reconnect = false;
bool accepted = false;
if (isConnected()) reconnect = true;
disconnect();
if (dataBits >= (int)QPortSettings::DB_5 && dataBits <= (int)QPortSettings::DB_8) {
portSettings.setDataBits((QPortSettings::DataBits) dataBits);
if(reconnect) connect();
accepted = true;
}
return accepted;
}
bool SerialLink::setStopBitsType(int stopBits)
{
bool reconnect = false;
bool accepted = false;
if(isConnected()) reconnect = true;
disconnect();
if (stopBits >= (int)QPortSettings::STOP_1 && stopBits <= (int)QPortSettings::STOP_2) {
portSettings.setStopBits((QPortSettings::StopBits) stopBits);
if(reconnect) connect();
accepted = true;
}
if(reconnect) connect();
return accepted;
}