serial communication PIC Qt

0

I'm doing an interface in QT which should show values of Different sensors, I use a PIC 18f4580 with RS232 communication for reading and transmitting data, to send the data I put them in a string of characters and send them as follows:

#use RS232(BAUD=9600,BITS=8,PARITY=N,XMIT=PIN_C6,RCV=PIN_C7)
k[0]='a';
k[1]='1';
k[2]='2';
k[3]='1';
k[4]='2';
k[5]='1';
k[6]='3';
k[7]=' '; 
while(1){
printf("%s",k);
delay_(250);
}

And to receive them in Qt I configure the port and I receive it in the following way:

ui->setupUi(this);
serial = new QSerialPort (this);
serial->setPortName("com3");
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
connect (serial , & QSerialPort :: readyRead , this , & MainWindow :: read);
void MainWindow::read()
{
if(serial->isReadable()){
QString k=serial->readAll();
ui->label_7->setText(k);
}

The idea is that I compare the first value of the string, I compare it and assign the value to the corresponding variable, but I started first to show what Qt received in a label but note that it did not receive always the complete chain or just received only part or sometimes nothing. I tried to put it in a cycle so that it would receive the 8 elements of the chain but it would freeze and the application would not respond.

try with readLine() and read(k,8) to specify how many elements the string would be but I get the same, even sending the data every 2 seconds the same thing happens.

    
asked by rasengan um 05.07.2018 в 00:41
source

1 answer

-1

Googling a bit I came to the following solution:

void MainWindow::readData()
{
    while (m_serial->canReadLine()) 
        qInfo() << m_serial->readLine();
}

With those simple lines, we have in the buffer the complete line that arrives from the serial port. From what I understand, the serial port would be the one that does the buffering until the line is complete and just there you can do the readLine. To me, these lines solved a big problem.

    
answered by 05.07.2018 в 19:01