How to open more than once SerialPort
-
void MainWindow::openSerialPort() { QVector<QSerialPort *> v; Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) { QSerialPort* serialPort = new QSerialPort(); serialPort->setPort(port); serialPort->setBaudRate(QSerialPort::Baud9600); serialPort->setDataBits(QSerialPort::Data8); serialPort->setParity(QSerialPort::NoParity); serialPort->setStopBits(QSerialPort::OneStop); serialPort->setFlowControl(QSerialPort::NoFlowControl); if (serialPort->open(QIODevice::ReadWrite)) { v.append(serialPort); ui->listWidget->addItem(p.name); } else { QMessageBox::critical(this, tr("Error"), serialPort->errorString()); showStatusMessage(tr("Open error")); } } } void MainWindow::closeSerialPort() { if (serialPort->isOpen()) { serialPort->close(); QList<QListWidgetItem*> items = ui->listWidget->selectedItems(); foreach(QListWidgetItem * item, items) { delete ui->listWidget->takeItem(ui->listWidget->row(item)); } } showStatusMessage(tr("Disconnected")); }
it's ok?
-
Hi
In void MainWindow::closeSerialPort()
you call if (serialPort->isOpen())But that is only serialPort pointer. you are not using the
Vector<QSerialPort *> v; at all so i wonder if this is what u want ?I imagine you need to loop over v and close all and not just whatever
serialPort points to ? -
@erytcg
Hi
its a list of comports.so v[index] gives a QSerialPort *
But you will have to answer what app does.
Currently you open all comports available on device.
Should closeSerialPort() close them all again?
to close one you would do
QSerialPort * Cur=v[0];
Cur->close();
or even
v[0]->close();Notice i use 0 for closing the first.
So to help you , i need to understand what app does as its uncommon to open ALL comports :)
-
Hi
if you move
QVector<QSerialPort *> v;
to the MainWindow.h (in the class)
and remove serialPort variable also!then you could do
void MainWindow::closeSerialPort( int index)
{
QSerialPort * serialPort = v[index];
if (serialPort->isOpen())
{
serialPort->close();
..and close the one u want with
closeSerialPort( some number) -
Oh u posted while i was writing
Can you use the row index directly to the V list ?
-
If there is a 1 to 1 to the V list ( please rename to comports or something :)
Then you can just use the
void MainWindow::closeSerialPort( int index)
{
QSerialPort * serialPort = v[index];
...and call it from
foreach(QListWidgetItem * item, items) {
int index= ui->listWidget->row(item); //
closeSerialPort( index); -
Hey can you look at code? Now it's ok? :)
void MainWindow::addAvailableSerialPorts() { QVector<QSerialPort *> v; Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()) { QSerialPort* serialPort = new QSerialPort(); serialPort->setPort(port); serialPort->setBaudRate(QSerialPort::Baud9600); serialPort->setDataBits(QSerialPort::Data8); serialPort->setParity(QSerialPort::NoParity); serialPort->setStopBits(QSerialPort::OneStop); serialPort->setFlowControl(QSerialPort::NoFlowControl); v.append(serialPort); } } void MainWindow::openSerialPort(int index) { QSerialPort * serialPort = v[index]; if (serialPort->open(QIODevice::ReadWrite)) { showStatusMessage(tr("Connected to %1 : OK").arg(p.name)); } else { QMessageBox::critical(this, tr("Error"), serialPort->errorString()); showStatusMessage(tr("Open error")); } }
-
@erytcg said in How to open more than once SerialPort:
Almost but one fatal error !
void MainWindow::addAvailableSerialPorts()
{
QVector<QSerialPort *> v; <<<< This is a local var. It should be in class. windows .h. should not be defined here. only used.So in
void MainWindow::openSerialPort(int index)
{
QSerialPort * serialPort = v[index]; <<< what v is this . it cannot be the one from addAvailableSerialPorts as that was local. -
Yes I talk about model, view , controller.
I add all avaibleports to vector in addAvailableSerialPorts ()
i open in openSerialPort(int index) / index=QComboBox->currentIndex();
and close in closeSerialPort(int index) / index=QListWidget->currentRow()+1;and all works fine I think, but if I want show received data on QCustomPlot widget.
I don't know how to convert QByteArray to QVector<double>.
My received data from qDebug() - "\x0B"
Thanks a lot for help :) -
@erytcg
Nope a list of Class * is not really MVC.Do you really need to convert QByteArray to QVector<double> ?
Why not convert to doubles when you read and add directly to QVector instead?Also who sends the doubles? Its not trivial to send doubles over serial as they dont map
nicely to chars and there can be a difference in byteorder and precision loss.