Solved How to share Serial port object between two classes
-
Hello Developers,
As mentioned above I was to send command using signals and slot approach to main window.
Now I want also to read data received from serial port in MainWindow from Dialog box.
I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?
thanks you very much :)
-
@saurabh162
Hi
You mean after serial have read all in main, you want to
send it to a dialog you open ?
Or do you mean that dialog all reads from serial while data is coming in ? -
@saurabh162 Add a getter method to your dialog:
Dialog dialog(this); dialog.exec(); MyData data = dialog.data();
-
Hello Mrji,
thank you for fast reply
Yes, I my application data received from serial port is first stored in buffer in Main Window. I want read this saved data from Dialog box.
In dialog box I am sending serial port commands to Main Window using signal and slot. But do not know what is right way to read serial data saved in Main window from Dialog Box.
-
Hello jsulm
Thank you for your fast response.
I am sorry but i could not understand your solution. I have serial data collected in Main window. This i want to read inside Dialog box.
So can you please explain your answer in little details
Thank you :)
-
@saurabh162 said in How to share Serial port object between two classes:
I have read that signal and slot approach is not good to get data or return values. So can you please guide me what can I do ?
Where have you read this? You can surely use signals&slots to exchange data. That's what signals&slots are all about :)
-
@saurabh162 You should explain better what you want to do.
Pass the data from main window to dialog via constructor:Dialog dialog(this, mySerialData); dialog.exec();
-
Thank you aha for reply
I have read it in following posts
https://forum.qt.io/topic/11000/getting-a-return-value-from-an-emitted-signal/3
https://stackoverflow.com/questions/5842124/can-qt-signals-return-a-value
According to above posts we should be cautious when we are returning data from a slot.
Additionally I have tried it and got segmentation fault. Following are the steps I have taken.
- I have a array " char receive_buffer[RBSIZE]" declared in Main window class with private access. This buffer is always filled with new data when we send serial command from Dialog Box using signal and slot.
- In order to read this buffer, I declared char pointer in my Dialog Box and intialize this pointer to address of "receive_buffer" in Serial communication slot in Main Window.
- After this when I try to read data in receive_buffer[RBSIZE] from Dialog Box using char pointer I get segementation fault.
Please let me know if you need any other information.
thank you :)
-
https://forum.qt.io/topic/11000/getting-a-return-value-from-an-emitted-signal/3
Yes, thats bad design. But you don't need to do it like this. You can emit another signal from your dialog when data is ready, and connect it to a slot in your main window.
Regards
-
ok aha_1980 I try this and will update you ..thanks :)
-
hello aha_1980,
Thank you very much it worked.
I have taken following steps to achieve it. It is working but I also want to ensure whether it is correct way of programming.
-
I have created signal (in Dialog Box) and slot (in main window) with same name
"void ReadSerialBuffer(void);" -
Declared a array char serialReceivebuffer[RBSIZE]; in dialog box. Additionally a public function "UpdateSerialReadBuffer"in Dialog box to copy data from source buffer to serialReceivebuffer in Dialog Box.
-
After this I defined slot in main window which is calling "UpdateSerialReadBuffer" function of dialog box to copy data from buffer in Main window to buffer in Dialog box.
-
In end I connected both signal and slot using "connect" method
Thank you very much :)
-
-
I also came across a similar situation. I'm a beginner of Qt and c++. It will be very helpful if you could share a sample code to communicate the serial port object in two forms.
many thanks