QByteArray corrupts my data ???Why???



  • The below mentioned code works fine when placed inside of my MainWindow.cpp from where it is being called.

    But as soon as I move it to a class in a separate file the data being sent from the serial port corrupts.
    I Have narrowed it down to the <relay_arr.prepend("ch");> and <relay_arr.append("_onx");> commands.
    If I comment out these two lines the <Ch_on> is transmitted correctly.
    I'm not sure if this might be worth mentioning. The class does not inherit from any other classes.

    @QByteArray relay_arr;

    void RelayClass::RelayOn(int Ch_on)
    {
    relays.setPortName("COM1");
    relays.setBaudRate(QSerialPort::Baud9600);
    relays.setDataBits(QSerialPort::Data8);
    relays.setParity(QSerialPort::NoParity);
    relays.setStopBits(QSerialPort::OneStop);
    relays.setFlowControl(QSerialPort::NoFlowControl);
    relays.open(QIODevice::ReadWrite);

    relay_arr.clear();
    relay_arr.setNum(Ch_on);
    relay_arr.prepend("ch");
    relay_arr.append("_onx");
    relays.write(relay_arr);
    relays.waitForBytesWritten(-1);
    
    relays.close();
    

    }@



  • try it:

    @relays.open(QIODevice::ReadWrite);
    relays.setBaudRate(QSerialPort::Baud9600);@

    (call setBaudRate method after port opening)



  • Nope... Still same problem.
    For some reason I don't grasp, This function tends to randomly give problems. It now seems to have stopped functioning in the main .cpp file as well, and I know it did work before.

    I tried cleaning the project.
    Changing Build directories.
    Deleting Build directories.
    Tested on other PCs.


  • Moderators

    You should wait and check for the device to be ready to write.



  • Ok. I have simplified the code to the bare bone, and still getting the same problem.
    But from what I can notice the problem does no longer seem to be related to the QByteArray.
    The code bellow also displays corrupted data.

    @#include <QtSerialPort>
    QSerialPort relays;

    void MainWindow::on_pushButton_clicked()
    {
    relays.setPortName("COM1");
    relays.setDataBits(QSerialPort::Data8);
    relays.setParity(QSerialPort::NoParity);
    relays.setStopBits(QSerialPort::OneStop);
    relays.setFlowControl(QSerialPort::NoFlowControl);
    relays.open(QIODevice::ReadWrite);
    relays.setBaudRate(QSerialPort::Baud9600);

    relays.write("all_offx");
    relays.waitForBytesWritten(-1);
    
    relays.close();
    

    }@



  • bq. You should wait and check for the device to be ready to write. bq.

    The only function I can find that comes close to this is <WaitForReadyRead>


  • Moderators

    @
    if (relays.open(QSerialPort::ReadWrite) {
    // Set port settings here
    QByteArray relay_arr("ch" + QByteArray::number(Ch_on) + "_onx");
    relays.write(relay_arr);
    }
    @



  • Thanks Sierdzio.
    I Will give your code a try tomorrow.

    Although I have found a semi sort of solution.

    If I add a 1second delay after opening the ports and also a 1second delay before closing them, then it works.
    This tells me that the <relays.waitForBytesWriten(-1)> does not work.
    As far as I know this function is supposed to pause until all data has been written to the device. Can anybody maybe comment on this statement?



    1. What is version of QtSerialPort?
    2. What is serial port type?


  • Hi kuzulis. Im not sure where to check the version number for QtSerialPort.
    The Serial port is a RS232 device.

    Sierdzio. I implemented your above mentioned code.
    No difference. The only thing I fount to work is if I add a 1sec delay after opening and before closing the port.



  • 2 achmed

    bq.
    Im not sure where to check the version number for QtSerialPort.

    From where you took the QtSerialPort?

    bq. The Serial port is a RS232 device.

    This is USB/Serial converter, on-board device or something else? What chip?

    bq. The code bellow also displays corrupted data.

    What do you see when do writing of relays.write("all_offx") ?



  • Someone else did the installation of Qt on my PC.
    Not sure where they got it from.

    We are using more that one Type/Brand/Chip. They are all giving the same problem.



  • bq. They are all giving the same problem.

    What is a problem? I repeat a question again: what do you see when do writing of relays.write(“all_offx”) on other side?

    bq. Not sure where they got it from.

    It is in your interests to know it. Without it further reasonings are senseless.



  • [quote author="kuzulis" date="1415359623"]What is a problem? I repeat a question again: what do you see when do writing of relays.write(“all_offx”) on other side?
    [/quote]

    As Mentioned in the first post. The output data is corrupted.
    Example: ^%$%^&*(
    In Other words unreadable.



  • This can not be in principle. Can you check same, but without closing of device?



  • [quote author="achmed" date="1415284517"]If I add a 1second delay after opening the ports and also a 1second delay before closing them, then it works.[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.