Page 1 of 1

Serial Communications Problem

Posted: Fri Apr 25, 2008 7:27 pm
by dhouston
I have a serial receive procedure, patterned after one I use under Windows, that runs in a background thread. It works after a fashion, but only updates the editor gadget when the mouse moves. The Windows procedure does not have this issue but updates as each line is received. All incoming lines terminate with #CRLF$.

Code: Select all

Procedure ComEventRcv()                     ;serial input thread
  Protected ComRcv.s,*buf
  *buf=AllocateMemory(1)                    ;freed automatically at program end
  Repeat
    If fd>-1
      If read_(fd,*buf,1)>0
        If PeekC(*buf)=10
          If Trim(ComRcv)<>""
            ComRcv=RemoveString(ComRcv,#CR$)
            AddGadgetItem(#EDT_IO_MAIN,-1,ComRcv)
            ScrollToEnd(#EDT_IO_MAIN)
            ComRcv=""
          EndIf  
        Else
          ComRcv+PeekS(*buf,1)  
        EndIf 
      EndIf
    EndIf
  ForEver
EndProcedure
It's called as soon as the main window opens (The serial port is intialized and opened earlier).

Code: Select all

If OpenWindowMain()
  If fd>-1:ComThread=CreateThread(@ComEventRcv(),0):EndIf
  ...
How can I persuade it to update as each line is received?

Posted: Thu May 08, 2008 3:00 pm
by dhouston
I'm still trying to resolve this. Further investigation indicates that the serial communications are OK. It's the updating of the editor gadget that is the problem. The last line doesn't get displayed unless the window is manually scrolled with the mouse wheel or scroll bar. In some cases, merely moving the mouse within the editor gadget will cause it to scroll to the last line.

Here's my ScrollToEnd procedure...

Code: Select all

Procedure ScrollToEnd(gadget)              ;scrolls text window to show last line
  Protected end_mark,*buffer, end_iter.GtkTextIter
  *buffer=gtk_text_view_get_buffer_(GadgetID(gadget)) 
  gtk_text_buffer_get_end_iter_(*buffer,@end_iter)
  ;gtk_text_buffer_place_cursor_(*buffer,@end_iter)
  end_mark=gtk_text_buffer_create_mark_(*buffer,"",@end_iter,#False)
  gtk_text_view_scroll_mark_onscreen_(GadgetID(gadget),end_mark)
EndProcedure
Obviously, there's something subtle that I'm missing but I have no clue as to what it might be.