C++ (Qt)class PortError : public std::runtime_error{public: PortError( const std::string &portName ) : std::runtime_error( "" ) { m_message = "Error: unable to open the port \"" + portName + "\""; } // ...
C++ (Qt)void MainWindow::runReceiver(){ try { m_receiver->run(); connect( m_receiver, SIGNAL( signalReceivedData( QByteArray ) ), this, SLOT( slotReceivedData( QByteArray ) ) ); } catch ( const PortError &e ) { QString message( e.what() ); QMessageBox::information( this, tr( "Error" ), message ); return; } catch( ... ) { QString message( "Error: unknown exception" ); QMessageBox::information( this, tr( "Error" ), message ); return; }}
C++ (Qt)#ifndef RECEIVER_H#define RECEIVER_H #include <QObject>#include <QString>#include <QSerialPort>#include <stdexcept>#include <string>#include "PortError.h" class Receiver : public QObject { Q_OBJECTpublic: Receiver( const QString &portName = QString( "COM2" ), QSerialPort::BaudRate baudRate = QSerialPort::Baud9600, QSerialPort::DataBits dataBits = QSerialPort::Data8, QSerialPort::Parity parity = QSerialPort::NoParity, QSerialPort::StopBits stopBits = QSerialPort::OneStop, QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl ); Receiver( const Receiver &receiver ); ~Receiver(); void run( ) throw( PortError ); QString getPortName() const; QSerialPort::BaudRate getBaudRate() const; QSerialPort::DataBits getDataBist() const; QSerialPort::Parity getParity() const; QSerialPort::StopBits getStopBits() const; QSerialPort::FlowControl getFlowControl() const; signals: void signalReceivedData( QByteArray data ); private slots: void slotReadyRead( ); private: QSerialPort m_serialPort; QString m_portName; QSerialPort::BaudRate m_baudRate; QSerialPort::DataBits m_dataBits; QSerialPort::Parity m_parity; QSerialPort::StopBits m_stopBits; QSerialPort::FlowControl m_flowControl;}; #endif // RECEIVER_H
C++ (Qt)#include "Receiver.h" Receiver::Receiver( const QString &portName, QSerialPort::BaudRate baudRate, QSerialPort::DataBits dataBits, QSerialPort::Parity parity, QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl ) : m_portName( portName ), m_baudRate( baudRate ), m_dataBits( dataBits ), m_parity( parity ), m_stopBits( stopBits ), m_flowControl( flowControl ){} Receiver::Receiver( const Receiver &receiver ){ this->m_portName = receiver.getPortName(); this->m_baudRate = receiver.getBaudRate(); this->m_dataBits = receiver.getDataBist(); this->m_parity = receiver.getParity(); this->m_stopBits = receiver.getStopBits(); this->m_flowControl = receiver.getFlowControl();} Receiver::~Receiver(){ m_serialPort.close();} void Receiver::run( ) throw( PortError ){ m_serialPort.setPortName( m_portName ); if ( !m_serialPort.open( QIODevice::ReadOnly ) ) { throw PortError( m_portName.toStdString() ); } m_serialPort.setBaudRate( m_baudRate ); m_serialPort.setDataBits( m_dataBits ); m_serialPort.setParity( m_parity ); m_serialPort.setStopBits( m_stopBits ); m_serialPort.setFlowControl( m_flowControl ); connect( &m_serialPort, SIGNAL( readyRead( ) ), this, SLOT( slotReadyRead( ) ) );} QString Receiver::getPortName() const{ return m_portName;} QSerialPort::BaudRate Receiver::getBaudRate() const{ return m_baudRate;} QSerialPort::DataBits Receiver::getDataBist() const{ return m_dataBits;} QSerialPort::Parity Receiver::getParity() const{ return m_parity;} QSerialPort::StopBits Receiver::getStopBits() const{ return m_stopBits;} QSerialPort::FlowControl Receiver::getFlowControl() const{ return m_flowControl;} void Receiver::slotReadyRead( ){ QByteArray data; data = m_serialPort.readAll( ); emit signalReceivedData( data );}
C++ (Qt)#ifndef PORTERROR_H#define PORTERROR_H #include <stdexcept>#include <string> class PortError : public std::runtime_error{public: PortError( const std::string &portName ) : std::runtime_error( "" ) { m_message = "Error: unable to open the port \"" + portName + "\""; } virtual ~PortError() throw() { } virtual const char *what() const throw() { return m_message.c_str(); } std::string getMessage() { return m_message; } private: std::string m_message;}; #endif // PORTERROR_H
C++ (Qt) try { m_sender->open(); } catch( const PortOpenError &e ) { QString message( e.what() ); QMessageBox::critical( this, tr( "Error" ), message ); return; }
C++ (Qt)void MainWindow::on_sendButton_clicked(){ QByteArray data = "Hello!"; try { m_sender->send( data ); } catch( const PortError &e ) { QString message( e.what() ); QMessageBox::critical( this, tr( "Error" ), message ); return; } catch( ... ) { QMessageBox::critical( this, tr( "Error" ), tr ( "Error: unknown exception." ) ); return; }}
C++ (Qt)#ifndef SENDER_H#define SENDER_H #include <QSerialPort>#include <QString>#include "PortOpenError.h"#include "PortSendDataError.h" class Sender{public: Sender( const QString &portName = QString( "COM1" ), QSerialPort::BaudRate baudRate = QSerialPort::Baud9600, QSerialPort::DataBits dataBits = QSerialPort::Data8, QSerialPort::Parity parity = QSerialPort::NoParity, QSerialPort::StopBits stopBits = QSerialPort::OneStop, QSerialPort::FlowControl flowControl = QSerialPort::NoFlowControl ); Sender( const Sender &sender ); ~Sender(); void setPortName( const QString &portName ); QString getPortName() const; QSerialPort::BaudRate getBaudRate() const; QSerialPort::DataBits getDataBits() const; QSerialPort::Parity getParity() const; QSerialPort::StopBits getStopBits() const; QSerialPort::FlowControl getFlowControl() const; void open() throw( PortOpenError ); void send( const QByteArray &data ) throw( PortOpenError, PortSendDataError ); private: QSerialPort m_serialPort; QString m_portName; QSerialPort::BaudRate m_baudRate; QSerialPort::DataBits m_dataBits; QSerialPort::Parity m_parity; QSerialPort::StopBits m_stopBits; QSerialPort::FlowControl m_flowControl;}; #endif // SENDER_H
C++ (Qt)#include "Sender.h" Sender::Sender( const QString &portName, QSerialPort::BaudRate baudRate, QSerialPort::DataBits dataBits, QSerialPort::Parity parity, QSerialPort::StopBits stopBits, QSerialPort::FlowControl flowControl ) : m_portName( portName ), m_baudRate( baudRate ), m_dataBits( dataBits ), m_parity( parity ), m_stopBits( stopBits ), m_flowControl( flowControl ){} void Sender::open() throw( PortOpenError ){ m_serialPort.setPortName( m_portName ); std::string functionName = "Sender::open()"; // Open the port if ( !m_serialPort.open( QIODevice::WriteOnly ) ) { throw PortOpenError( m_serialPort.portName().toStdString(), functionName ); } // Set settings m_serialPort.setBaudRate( m_baudRate ); m_serialPort.setDataBits( m_dataBits ); m_serialPort.setParity( m_parity ); m_serialPort.setStopBits( m_stopBits ); m_serialPort.setFlowControl( m_flowControl );} void Sender::send( const QByteArray &data )throw( PortOpenError, PortSendDataError ){ std::string functionName = "Sender::send()"; // Check opened or not if ( !m_serialPort.isOpen() ) { throw PortOpenError( m_serialPort.portName().toStdString(), functionName ); } // Write data to the port if ( m_serialPort.write( data ) == -1 ) { throw PortSendDataError( m_serialPort.portName().toStdString(), functionName ); }} Sender::Sender( const Sender &sender ){ this->m_portName = sender.getPortName(); this->m_baudRate = sender.getBaudRate(); this->m_dataBits = sender.getDataBits(); this->m_parity = sender.getParity(); this->m_stopBits = sender.getStopBits(); this->m_flowControl = sender.getFlowControl();} Sender::~Sender(){ m_serialPort.close();} void Sender::setPortName( const QString &portName ){ m_portName = portName;} QString Sender::getPortName() const{ return m_portName;} QSerialPort::BaudRate Sender::getBaudRate() const{ return m_baudRate;} QSerialPort::DataBits Sender::getDataBits() const{ return m_dataBits;} QSerialPort::Parity Sender::getParity() const{ return m_parity;} QSerialPort::StopBits Sender::getStopBits() const{ return m_stopBits;} QSerialPort::FlowControl Sender::getFlowControl() const{ return m_flowControl;}
C++ (Qt)#ifndef PORTOPENERROR_H#define PORTOPENERROR_H #include "PortError.h" class PortOpenError : public PortError{public: PortOpenError( const std::string &portName, const std::string &functionName ) : PortError( portName, functionName ) { m_message = "Error: unable to open the port \"" + m_portName + "\"" + " in the function " + "\"" + m_functionName + "\""; }}; #endif // PORTOPENERROR_H
C++ (Qt)#ifndef PORTSENDDATAERROR_H#define PORTSENDDATAERROR_H #include "PortError.h" class PortSendDataError : public PortError{public: PortSendDataError( const std::string &portName, const std::string &functionName ) : PortError( portName, functionName ) { m_message = "Error: unable to send data to the port \"" + m_portName + "\"" + " in the function " + "\"" + m_functionName + "\""; }}; #endif // PORTSENDDATAERROR_H
C++ (Qt)#ifndef PORTERROR_H#define PORTERROR_H #include <stdexcept>#include <string> class PortError : public std::runtime_error{public: PortError( const std::string &portName, const std::string &functionName ) : std::runtime_error( "" ), m_portName( portName ), m_functionName( functionName ) { } virtual ~PortError() throw() { } virtual const char *what() const throw() { return m_message.c_str(); } protected: std::string m_message; std::string m_portName; std::string m_functionName;}; #endif // PORTERROR_H