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
|
//widget.h
//----------------------------------
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget(){}
static QString errorString(QSerialPort::SerialPortError error) const;
//...
private:
QSerialPort m_serialPort;
QByteArray m_readData;
//...
private slots:
//Réception et traitement des données reçues sur le port série
void handleReadyRead();
//Gestion des erreurs du port série
void handleError(QSerialPort::SerialPortError error);
//...
}
//widget.cpp
//----------------------------------
Widget::Wdiget(QWidget *parent)
: QWidget(parent)
{
//initialisation GUI
//...
//Configuration du port série
m_serialPort.setPortName("COM1");
m_serialPort.setBaudRate(QSerialPort::Baud9600);
//Connection au signal readyRead() afin d'être notifié lors de la réception de nouvelle données sur le port série.
connect(&m_serialPort, &QSerialPort::readyRead, this, &Widget::handleReadyRead);
//Connection au signal errorOccurred() afin d'être notifié lorsqu'une erreur survient sur le port série.
connect(&m_serialPort, &QSerialPort::errorOccurred, this, &Widget::handleError);
//Ouverture du port. En cas d'erreur, le slot handleError() sera appelé avec le code erreur.
m_serialPort.open(QSerialPort::ReadWrite);
}
void Widget::handleReadyRead()
{
//Traitement des données, supposant que chaque valeur est codée en ASCII
//et séparée par un retour à la ligne.
// Exemple: "10\n12\n14\n"
//Récupération de toutes les données reçues.
m_readData.append(m_serialPort->readAll());
//Détection de retour à la ligne.
int pos = m_readData.indexOf("\n");
//récupération et conversion des valeurs disponibles
while(pos > 0){
bool ok = false;
//Récupération de la valeur codée en ASCII
double value = m_readData.left(pos - 1).toDouble(&ok);
//int value = m_readData.left(pos - 1).toInt(&ok);
//On enlève les données traitées du buffer principal
m_readData = m_readData.mid(pos);
if(ok){
//Converion réussie !
//Affichage de la nouvelle valeurs sur la jauge (VU-mètre)
//Il est peut être inutile d'afficher chaque valeur. On pourrait
//n'afficher que la dernière valeur reçue lorsque plusieurs valeurs
//sont disponibles dans le buffer.
m_vuMetre->setValue(value);
} else {
//Erreur de conversion
displayError(QString("Error, cannot convert value %1").arg(value));
}
}
}
void Widget::handleError(QSerialPort::SerialPortError serialPortError)
{
displayError(errorString(serialPortError));
}
QString Widget::errorString(QSerialPort::SerialPortError serialPortError) const
{
switch(serialPortError){
case QSerialPort::NoError: return "No error occurred.";
case QSerialPort::DeviceNotFoundError: return "An error occurred while attempting to open an non-existing device.";
case QSerialPort::PermissionError: return "An error occurred while attempting to open an already opened device by another process or a user not having enough permission and credentials to open.";
case QSerialPort::OpenError: return "An error occurred while attempting to open an already opened device in this object.";
case QSerialPort::ParityError: return "Parity error detected by the hardware while reading data. This value is obsolete. We strongly advise against using it in new code.";
case QSerialPort::FramingError: return "Framing error detected by the hardware while reading data. This value is obsolete. We strongly advise against using it in new code.";
case QSerialPort::BreakConditionError: return "Break condition detected by the hardware on the input line. This value is obsolete. We strongly advise against using it in new code.";
case QSerialPort::WriteError: return "An I/O error occurred while writing the data.";
case QSerialPort::ReadError: return "An I/O error occurred while reading the data.";
case QSerialPort::ResourceError: return "An I/O error occurred when a resource becomes unavailable, e.g. when the device is unexpectedly removed from the system.";
case QSerialPort::UnsupportedOperationError: return "The requested device operation is not supported or prohibited by the running operating system.";
case QSerialPort::TimeoutError: return "A timeout error occurred.";
case QSerialPort::NotOpenError: return "This error occurs when an operation is executed that can only be successfully performed if the device is open.";
default:
case QSerialPort::UnknownError: return "An unidentified error occurred.";
}
} |
Partager