GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Apr 17, 2014 1:10 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Problems with rs232 serial data and generating gtk events.
PostPosted: Mon Nov 05, 2007 5:26 am 
Offline

Joined: Mon Nov 05, 2007 4:48 am
Posts: 4
Greetings All:

I'm having problems acking data from a serial port and having the new data generate a gtk event so my data is displayed. I have one gtk entry per serial port configured thusly.
Code:
  ports->serial_port[channel].data_text = gtk_text_buffer_new(NULL);
  ports->serial_port[channel].data_dialog = gtk_text_view_new_with_buffer(ports->serial_port[channel].data_text);
  ports->serial_port[channel].data_text = gtk_text_view_get_buffer(GTK_TEXT_VIEW(ports->serial_port[channel].data_dialog));

Each open port receives a thread that uses a blocking read thusly.
Code:
    ports->serial_port[channel].thread_i_ret =
      pthread_create(&ports->serial_port[channel].thread_proc,
                     NULL,spawn_listen,(void*)ports);

That process looks like this.
Code:
  pthread_mutex_lock(&ports->serial_port[channel].mutex);

    bzero(ports->serial_port[channel].port_data,
          sizeof(ports->serial_port[channel].port_data));
    ports->serial_port[channel].data_length =
    read(ports->serial_port[channel].chan,&ports->serial_port[channel].port_data,9);

      sprintf(buffer,"%s",ports->serial_port[channel].port_data);
    gtk_text_buffer_get_start_iter(ports->serial_port[channel].data_text,&start);
    gtk_text_buffer_get_end_iter(ports->serial_port[channel].data_text,&end);
    len = gtk_text_buffer_get_char_count(ports->serial_port[channel].data_text);
    gtk_text_buffer_delete(ports->serial_port[channel].data_text,&start,&end);
      gtk_text_buffer_set_text(ports->serial_port[channel].data_text,buffer,strlen(buffer));

The data arrives just fine from any number of ports, but unless I move the cursor over the gtk_entry, no data is displayed. So I tried running a gtk_main_itteration_do(TRUE) every 30 milliseconds. This gave me a more than acceptable refresh and a smooth display, however after some time, I think the iters of gtk main and each of the spawned processes collide and crash gtk main.

What am I missing? I don't think I should need to run the gtk_main_itteration_do polling callback. Why won't the act of using gtk_text_buffer_set_text generate the needed gtk event to cuase the GUI to update and display the new data?

Thanks for any help you can give!
gtk_n008


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 12:48 pm 
Offline
Familiar Face

Joined: Sun Nov 05, 2006 8:56 am
Posts: 19
try and have your thread update a few objects that store the data and create a timer that updates the widget that displays the data every so often. Just an Idea...

I'm not familiar with the text view widget yet, but It sounds like it may be a thread issue?

edit: maybe this will help: http://library.gnome.org/devel/gdk/unst ... reads.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 3:52 pm 
Offline

Joined: Mon Nov 05, 2007 4:48 am
Posts: 4
Ah!

So the API has an entire section dedicated to threads! I missed that. I was using posix threads raw. I considered that threads may be the problem. THANKS! I have some reading to do!

I do have a timed callback running 30 times a second but it slams gtk_main. I was also reading up on defining my own gtk signal that would occur after the blocking read acks data from the port. But the threads section of the API looks quite promising. I'll inform of any results etc ...

gtk_N008


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 8:24 pm 
Offline
Familiar Face

Joined: Sun Nov 05, 2006 8:56 am
Posts: 19
it may be as simple as adding something like this to your code:
Code:
int main(int argc, char **argv)
{
  gtk_init(&argc, &argv);
  g_thread_init (NULL);
  gdk_threads_init ();
  gdk_threads_enter ();

  // program init here

  gtk_main();
  gdk_threads_leave ();

return 0;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 6:41 am 
Offline

Joined: Mon Nov 05, 2007 4:48 am
Posts: 4
Hummm.... My gtk is crashing on gtkinit() ... gotta do more reading. Darn! Thaks for all the help, I'll post more as things develop.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 10, 2007 5:01 pm 
Offline

Joined: Mon Nov 05, 2007 4:48 am
Posts: 4
I came to a non solution solution. Betacuse gtk can only support one thread, and I need a thread for every open serial port I decided to make my timed callback refresh routine and use it as the function that updates the gui. I really wanted to make something that totally used each serial port and avoid polling. So now each open port thread writes data into a small buffer that is part of the overall structure that gets passed about. The timed callback drains these buffers and copies the data into the various gtk objects. This works and is stable. The code cleaned up a tad too. Still not what I wanted, but I'll take it for now. Thanks for all the input folks, it really helped.

gtk-N008


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group