Unsolved How to send data in QserialPort thread
-
Hi @Gokhan
You can be interested with this link of qt doc
Which Qt Thread Technology Should You Use?
and
Choosing an appropriate approach
I hope this can help you !
-
QSerialPort is already asynchronous so normally you wouldn't need a second thread to write to it.
On a separate note, the code you posted is probably not the right way to handle communication over QSerialPort. you should check how to use QDataStream
-
-
@Gokhan said in How to send data in QserialPort thread:
all data can't be sent
Can you elaborate this point?
-
@VRonin Serial port must not miss any data while reading, therefore I can use thread method. Sometimes, while reading big data which is sent immediately so byte-to-byte, unfortunately, have to send the big data. When the data is being sent, there must be a few delay between bytes, so I give a 1ms delay (QThread::msleep(1);). However, because reading and writing are at same thread, it can not read the buffer during writing and it gets an error that is bus-off. What should I do to read/write the serial port in a thread at the same time?
-
I can't find any solution for this, please help me to solve. I have to set a delay is around 1ms while sending bytes. Moreover, at the same time, it has to read the buffer, but the thread is sleeping then and so it misses data.
-
Hi,
You seem to be blocking the communication, why not use Qt's asynchronous nature and buffer the data you receive until you have everything you need to go further ?
-
You can send it using byte array ,by serializing the data in an array and de-serializing it .
-
@SGaist Then it misses data while clicking the window bar, I have to use a thread for reading.
@Amogh I have already used the byte array for sending. While sending, it's spending lots of time because it's size is around 512byte and it sends byte-to-byte.
I'm using it like this link for reading and have tried to add the sending method. (qsp-no-freeze)
https://bugreports.qt.io/browse/QTBUG-61233 -
Then move the complete serial port handling to that thread and communicate information back to the main thread when appropriate but there shouldn't be any need to sleeping or blocking.
-
@SGaist Well, how should I communicate with the serial port thread? Signals/Slots or Concurrency? Which better? You say to shouldn't block, but while writing the send buffer, have to wait in order to be writtenBytes. Then how can I check whether it was written a byte?
-
This post is deleted! -
Please be a bit more patient before bumping your own thread. This is a community forum and people answering here may not live in the same timezone as you.
Signals/Slots, concurrency has nothing to do with inter thread communication.
Why do you have to wait when writing to your serial port ?
-
@SGaist Sorry for impatient, I am struggling with this problem for a week.
To make sure whether really it's written because the receiver has limited receive buffer, also these data will be parsed after that it will send through can bus. -
Are you mixing RS232 and CAN ?
-
@SGaist It's USB-CAN module, so it converts the received data from USB to send can bus. At the same time, it might be data in can bus line and it has to read. It should never miss data even if the line is full(%100) while reading/writing.
-
These are two different buffers so you should get data even if you are writing something to it.
-
However, while it's writing, should read the buffer otherwise the line will be bus off and get an error. Unfortunately, because both are in the same thread and shouldn't wait to transfer the data.
-
Well, how can I write data to the serial port when it's in another thread. Is it right method to access directly the writing register? Or Should I use signals/slots?
serilPort *serial= new serialPort(); serial->write(//data); it's right?
-
You can send that using signals and slots or QMetaObject::invokeMethod.