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
| #include <QDebug>
#include <QSerialDevice/QSerialDevice/abstractserial.h>
#include "RS232.h"
RS232::RS232(QObject *parent) : QObject(parent)
{
// Initialisation de la connexion série
port = new AbstractSerial(this);
connect(port, SIGNAL(readyRead()), this, SLOT(slotRead()));
port->setDeviceName("/dev/ttyS0");
if (!port->open(AbstractSerial::ReadWrite))
{
qDebug() << "Opening device error.";
}
if (!port->setBaudRate(AbstractSerial::BaudRate9600))
{
qDebug() << "Set baud rate " << AbstractSerial::BaudRate9600 << " error.";
}
if (!port->setDataBits(AbstractSerial::DataBits8))
{
qDebug() << "Set data bits " << AbstractSerial::DataBits8 << " error.";
}
if (!port->setParity(AbstractSerial::ParityEven))
{
qDebug() << "Set parity " << AbstractSerial::ParityEven << " error.";
}
if (!port->setStopBits(AbstractSerial::StopBits1))
{
qDebug() << "Set stop bits " << AbstractSerial::StopBits1 << " error.";
}
if (!port->setFlowControl(AbstractSerial::FlowControlOff))
{
qDebug() << "Set flow " << AbstractSerial::FlowControlOff << " error.";
}
if (!port->setCharIntervalTimeout(50))
{
qDebug() << "Set timeout error.";
}
}
RS232::~RS232()
{
port->close();
}
void RS232::slotRead()
{
QByteArray ba = port->readAll();
QString str(ba);
qDebug() << "Donnees recues : " << str << " -> " << ba.toHex();
}
void RS232::sendData()
{
QString temp = "ABCD";
temp.append(QChar(0x0A));
QByteArray ba = temp.toAscii();
qDebug() << "Donnees envoyees : " << temp << " -> " << ba.toHex();
port->write(ba);
} |
Partager