Serial port Communication with pc through serial Adapter
-
@J-Hilk said in Serial port Communication with pc through serial Adapter:
void MainWindow::serialReceived()
{
QString glen = serial->readAll();
if (glen.length()>0){
/QString/ line = ui->receive->toPlainText();
ui->receive->setText(ui->receive->toPlainText()+line+"\n");
ui->receive->setText(line + "\r\n" + glen + "\r\n");
line = txt3;
}
QMetaObject::invokeMethod(this, &MainWindow::checkAndSendData, Qt::QueuedConnection);
}I Have tried this my output is same like this
this is my code is there is any problem in my code#include "mainwindow.h" #include "ui_mainwindow.h" #include <QList> #include <QComboBox> #include <QString> #include <QTextEdit> #include <QTime> #include <QDebug> #include <QtSerialPort/QSerialPortInfo> #include <QtSerialPort> #include <QFile> #include <QList> QString line ; QString txt3; QSerialPort *serial; QStringList m_lines; //QByteArray rawData; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QString filename1="/home/adx-soft1/Shankar/serial/myfile1.txt"; QFile file(filename1); if(!file.exists()){ qDebug() << "NO FILE "<<filename1; }else{ qDebug() << filename1<<" ..."; } QString line1; ui->sender->clear(); if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ ui->sender->setText(file.readAll()); } QList<QSerialPortInfo> list; list = QSerialPortInfo::availablePorts(); for (int i= 0; i < list.length(); i++) { ui->comboBox->addItem(list[i].portName()); } } MainWindow::~MainWindow() { delete ui; serial->close(); } void MainWindow::on_pushButton_clicked() { serial = new QSerialPort (this) ; serial->setPortName(ui->comboBox->currentText()); serial->setBaudRate (QSerialPort::Baud115200); serial->setDataBits(QSerialPort::Data8); serial->setParity (QSerialPort::NoParity); serial->setStopBits (QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->open(QIODevice::ReadWrite); connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived())); // connect(serial, &QSerialPort::readyRead,this, &MainWindow::serialReceived); ui->progressBar->setValue(100); // ui->receive->clear(); } void MainWindow::checkAndSendData() { if(!m_lines.isEmpty()){ QByteArray inBytes; QByteArray baToSend = m_lines.takeFirst().toUtf8(); serial->write(baToSend); ui->aknowmnt->setText("Sent Message -->" "\n"+ QString(baToSend) + "\n"); } else { ui->aknowmnt->setText("Receiving "); } } void MainWindow::on_send_clicked() { line = ui->sender->toPlainText(); QString txt1 = ui->receive->toPlainText(); /*QString*/ txt3 = ui->aknowmnt->toPlainText(); if(m_lines.isEmpty()){ QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt")); inputFile.open(QIODevice::ReadOnly); if (!inputFile.isOpen()) return; QTextStream stream(&inputFile); while (!stream.atEnd()){ line = stream.readLine(); m_lines.append(line+QString("\r\n")); } } checkAndSendData(); } void MainWindow::serialReceived() { QString glen = serial->readAll(); if (glen.length()>0){ /*QString*/ line = ui->receive->toPlainText(); ui->receive->setText(ui->receive->toPlainText()+line+"\n"); ui->receive->setText(line + "\r\n" + glen + "\r\n"); line = txt3; } checkAndSendData(); QMetaObject::invokeMethod(this, &MainWindow::checkAndSendData, Qt::QueuedConnection); }
But I have not get the correct output @J-Hilk
-
@sankarapandiyan check your encode function
-
@small_bird Encode function means What you mean ? i didnt get you
-
@sankarapandiyan
ok, lets simplify your code, I assumereceive
andaknowmnt
are QTextEdits or QTextBrowser, since they have atoPlainText
functionvoid MainWindow::checkAndSendData() { if(!m_lines.isEmpty()){ QByteArray baToSend = m_lines.takeFirst().toUtf8(); serial->write(baToSend); ui->aknowmnt->append("Sent Message --> "+ QString(baToSend)); } else { ui->aknowmnt->append("All are send "); } }
void MainWindow::serialReceived() { QString glen = serial->readAll(); if (glen.length()>0){ ui->receive->append( "Received -->" + QString(glen)); } QMetaObject::invokeMethod(this, &MainWindow::checkAndSendData, Qt::QueuedConnection); }
-
Yes i have tried this method , the correct data is not shown in the receiver side ..
void MainWindow::on_pushButton_clicked() { serial = new QSerialPort (this) ; serial->setPortName(ui->comboBox->currentText()); serial->setBaudRate (QSerialPort::Baud115200); serial->setDataBits(QSerialPort::Data8); serial->setParity (QSerialPort::NoParity); serial->setStopBits (QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->open(QIODevice::ReadWrite); // connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived())); connect(serial, &QSerialPort::readyRead,this, &MainWindow::serialReceived); ui->progressBar->setValue(100); // ui->receive->clear(); } void MainWindow::checkAndSendData() { if(!m_lines.isEmpty()){ QByteArray baToSend = m_lines.takeFirst().toUtf8(); serial->write(baToSend); ui->aknowmnt->append("Sent Message --> "+ QString(baToSend)); } else { ui->aknowmnt->append("All are send "); } } void MainWindow::on_send_clicked() { line = ui->sender->toPlainText(); QString txt1 = ui->receive->toPlainText(); /*QString*/ txt3 = ui->aknowmnt->toPlainText(); if(m_lines.isEmpty()){ QFile inputFile(QString("/home/adx-soft1/Shankar/serial/myfile1.txt")); inputFile.open(QIODevice::ReadOnly); if (!inputFile.isOpen()) return; QTextStream stream(&inputFile); while (!stream.atEnd()){ line = stream.readLine(); m_lines.append(line+QString("\r\n")); } } checkAndSendData(); } void MainWindow::serialReceived() { QString glen = serial->readAll(); if (glen.length()>0){ ui->receive->append( "Received -->" + QString(glen)); } QMetaObject::invokeMethod(this, &MainWindow::checkAndSendData, Qt::QueuedConnection); }
-
@J-Hilk said in Serial port Communication with pc through serial Adapter:
ok, lets simplify your code, I assume receive and aknowmntare QTextEdits or QTextBrowser, since they have a toPlainTextfunction
Yes Its a QTextEdits
-
@sankarapandiyan
you're obviously receiving non ascii bytesvoid MainWindow::serialReceived() { QByteArray glen = serial->readAll(); if (glen.length()>0){ ui->receive->append( QString("Received --> %1").arg(QString(glen.toHex(' '))); } QMetaObject::invokeMethod(this, &MainWindow::checkAndSendData, Qt::QueuedConnection); }
-
@J-Hilk said in Serial port Communication with pc through serial Adapter:
void MainWindow::serialReceived()
{
QByteArray glen = serial->readAll();
if (glen.length()>0){
ui->receive->append( QString("Received --> %1").arg(QString(glen.toHex(' ')));
}
QMetaObject::invokeMethod(this, &MainWindow::checkAndSendData, Qt::QueuedConnection);}
this out put is what i receive from another pc
And the next below image is shown as a data send as some thing differentEven Now also i did not get the exact output sir what should i do i am stuck here .. @J-Hilk
-
@sankarapandiyan Maybe it is a silly question, but are you sure the serial settings your are using, are correct?
- 115200 bauds?
- 8 bits?
Are you using a crossed/null modem serial cable? ==> cf https://en.wikipedia.org/wiki/Null_modem.
-
@KroMignon I am Using hl-340 usb-serial adapter . so here , what i want to give as a baud rate.
void MainWindow::on_pushButton_clicked() { serial = new QSerialPort (this) ; serial->setPortName(ui->comboBox->currentText()); serial->setBaudRate (QSerialPort::Baud115200); serial->setDataBits(QSerialPort::Data8); serial->setParity (QSerialPort::NoParity); serial->setStopBits (QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); serial->open(QIODevice::ReadWrite); connect(serial,SIGNAL(readyRead()),this, SLOT (serialReceived())); ui->progressBar->setValue(100); // ui->receive->clear(); }
-
@sankarapandiyan said in Serial port Communication with pc through serial Adapter:
I am Using hl-340 usb-serial adapter
Okay, that's for having a RS232 port on you PC, but to connect two PC you need a "null modem" cable (to cross Tx and Rx signal between the 2 serial ports).
Have you something like this? -
@KroMignon Yes you are right ..
this is my null modem i am using
I dontknow i am ryt or wrong and i am new to this topic .. the above image shows my serial adapter with null modem
-
@sankarapandiyan Okay, so the hardware side seems to be right. Can you give your software environnement:
- Operation system you are using (Linux ? Windows? MacOS?)
- Qt Kit Version you are using (not Qt Creator version!)
-
Operation system you are using (Linux ? Windows? MacOS?)
Linux_Ubuntu18.04.3
Qt Kit Version you are using (not Qt Creator version!)
**I dont know how to find the Qt kit version but any way little bit i have find out i think ..the below image shows the version of kit ** @KroMignon -
@sankarapandiyan said in Serial port Communication with pc through serial Adapter:
Linux_Ubuntu18.04.3
Have you tried your serial interface with another tool like cutecom.
On Ubuntu/Debian, you can install it withapt-get install cutecom
-
YEs I have done its working in the application ... @KroMignon