LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2008, 11:35 AM   #1
DiBosco
Member
 
Registered: Nov 2001
Location: Manchester, UK
Distribution: Mageia
Posts: 807

Rep: Reputation: 40
Waiting for an incoming serial message in a GUI program


This is probably more a question about programming methods than an request for getting some specific code going. Bear in mind I have come from an embedded programming background which is very much run to complete software.

I have finished a little Qt4 C++ program that sends and receives serial data for programming embedded microcontrollers. I send out little packets of data over a USB virtual com port (the virtual com port sends and receives data in exactly the same way as a normal ttySx com port).

Now, I have a big array of memory content that I parse and then send out 16 bytes at a time with a checksum. I then wait for an acknowledgement byte back from the microcontroller. When I say wait, I really mean wait. The program is just stuck in an endless loop waiting for the ack byte.

I am sure that, apart from the fact that if no serial message comes back, this is just not the way to write PC programs. In an embedded routine I would either have a timer go off if nothing was received and report an error or be calling a serial handling routine from my main loop. There doesn't seem to be this main loop way of writing code in PC based GUI applications.

It seems in C++ global variables are really frowned upon. I could, I suppose, have a timer go off and check the routine that does all this parsing and handling, going through this large state machine a stage at a time, but I think that would necessitate having loads of global variables.

Is it easy either to explain or point me to an article on how this is done in this sort of application (it's using KDevelop and Qt4)? I have tried Googling around, but it's such a verbose question it doesn't work too wll in a search engine!

Many thanks.
 
Old 11-18-2008, 12:41 PM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
You can use for example a blocking read with a time out like that:
Code:
struct pollfd ufds;
ufds.fd = tty_dev;
ufds.events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
ufds.revents = 0;
if (poll (&ufds, 1, timeout) < 0) {
   /* Error or timeout */
} else {
   read (tty_dev, buffer, buf_size);
}
"poll" function will return a soon as something append on device "tty_dev" (error, incoming data...), or return after "timeout" in ms.
You can monitor for event on several device with only one "poll" since the function take an array of "struct pollfd", one per device to monitor.
 
Old 11-18-2008, 01:00 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
In GUI programming, I think there is usually a way of hooking into an 'idle' condition such that your callback is called whenever the GUI is idle. You could create and register a callback that uses the select() function to determine whether there is data available in your serial device. This is a standard facility provided by the OS for such circumstances. You would probably be well served to find an online tutorial or two that explains the use of the select() call, as well as the small number of functions that are used in conjunction with it.
--- rod.
 
Old 11-18-2008, 01:27 PM   #4
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
In GUI programming with packages like QT or WX -- and most others -- the "main loop" is generally handled by the library itself to abstract event handling and other low level things like drawing to the screen.

In your case and almost ALL cases you want to handle non-GUI specific things like database, serial and socket connections outside of your GUI code so the interface stays responsive and the user productive while other tasks are performed. You can do this with other processes, threads or simply call a function in your serial handling class to poll if data is in the buffer.

This is generally done in an MVC model and keeps the code clean, maintainable and extensible. In simple cases, you could keep this all balled up into one like you have it now and just present a modal window with a progress bar and cancel button. Obviously, that's not what you want.

In short, look up MVC GUI programming techniques.
 
Old 11-18-2008, 05:35 PM   #5
DiBosco
Member
 
Registered: Nov 2001
Location: Manchester, UK
Distribution: Mageia
Posts: 807

Original Poster
Rep: Reputation: 40
Thanks everyone, that's given me lots to go on. I'll do some reading up.
 
Old 11-18-2008, 06:29 PM   #6
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
I'd vote for using the idle loop to dispatch to certain functions depending on a state machine. The state machine will at least need static variables in the idle loop.

Ignore the fools who scream about using globals; while it is bad practise to use globals merely for convenience, coming up with even worse schemes for the sake of avoiding globals is just ridiculous. To make things slightly neater, you can create one struct to hold globals.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial Modem with Incoming Call/Hold feature student1 Linux - Hardware 0 01-29-2008 09:35 AM
My browser, all day today: 'waiting for linuxquestions.org...' ..waiting.. waiting .. GrapefruiTgirl LQ Suggestions & Feedback 18 05-25-2007 05:35 AM
How to simulate incoming character in serial communication? Stor_G Programming 7 09-01-2004 02:22 AM
Cannot see incoming message in Squirrelmail 1.2.11 choth Linux - Software 1 08-28-2003 03:44 AM
saving incoming serial data to a file mssucks Linux - Networking 1 12-14-2001 10:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration