FLTK-term

For issues with communication ports, protocols, etc.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Post by dasyar »

Why don't you get it from the Source: fltk.org/documentation?

Instead of using trial & error Method ???
Since this is a freeBasic programming forum, I was hoping to keep as close to FBC language as possible. I could be wrong about all this, maybe freeBasic is changing to C++.
Introduction to FLTK

This manual describes FLTK version 2.0, a cross-platform C++ Graphical User Interface (GUI) toolkit. Each of the "related pages" in this manual is designed as a tutorial for using FLTK, while the main sections provide a complete reference for all FLTK widgets and functions.

FLTK (pronounced "fulltick") is a LGPL'd C++ graphical user interface toolkit for X (UNIX®), OpenGL®, Microsoft® Windows®, OS/X, and several other platforms. It was originally developed by Mr. Bill Spitzak and is currently maintained by a small group of developers across the world with a central repository in the US.
Again, it is greatly appreciated that D.J.Peters took the time and energy to make FLTK available for freeBasic. Not sure what enhancements, if any, were made by D.J.Peters to be able to make FLTK run in FBC, but he would be the one to know what is consistent. Now if we can get back on topic.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FLTK-term

Post by MrSwiss »

dasyar wrote:Since this is a freeBasic programming forum, I was hoping to keep as close to FBC language as possible.
I don't know which Planet you come from, but here on Earth: if we use a Library, we get the Doc's from the Library-Provider.
BTW: look for FLTK 1.3.3 -- NOT 2.0 !!
The Language used, to Code the Library, is "out of the Scope of Interest", because:
D.J.Peters provided the necessary WRAPPER, that makes it usable in FB!
dasyar wrote:Now if we can get back on topic.
Right there ... on the Spot.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Post by dasyar »

I have been looking at the program below, and now I see the commands that are needed to get to a string that you would need to do a 'print #1,'.

Code: Select all

  if char=10 andalso len(*text)>1 then
    print "exeute command: " & *Text
  end if
In the snippet above, would I have to add a CR, 'print #1, *Text & chr(10)', in order to send the string with a CR. Since the if loop is checking 'char' for 10, is that included in the *Text string? Also, is chr(10) sufficient, would I also need a chr(13) included. Since I am doing this on a Windows 10 system, just want to be sure.

Now I have to think about an input solution, in a console version, I use something like this:

Code: Select all

While LOC(1)>0
buffer = Input$(LOC(1),#1)
Print buffer;
print #1,Chr(10);
Wend
I guess the While...Wend would be replaced with a SUB? But how do I get the incoming char/string to print inside the ModifyCb window/area? I will probably do a hard code for the COM and BAUD selection, and maybe try and see if I can get this to respond too some incoming stuff.

Code: Select all

#INCLUDE "fltk-c.bi"
sub ModifyCb cdecl (byval p           as long, _
                    byval nInserted   as long, _
                    byval nDeleted    as long, _
                    byval nRestyled   as long, _
                    byval deletedText as const zstring ptr, _
                    byval Editor   as any ptr)
  if nInserted=0 andalso nDeleted=0 then exit sub 
  dim as Fl_Text_Buffer ptr TextBuffer = Fl_Text_DisplayGetBuffer(Editor)
  dim as zstring ptr Text
  dim as long e , s=p , char = Fl_Text_BufferCharAt(TextBuffer,p)
  if (nInserted > 0) then e = s + nInserted
  if (nDeleted  > 0) then e = s + nDeleted
  s = Fl_Text_BufferLineStart(TextBuffer,s)
  e = Fl_Text_BufferLength(TextBuffer)
  'e = Fl_Text_BufferLineEnd(TextBuffer,e)
  Text  = Fl_Text_BufferTextRange(TextBuffer ,s,e)
  if char=10 andalso len(*text)>1 then
    print "exeute command: " & *Text
  end if
end sub
'
' main
'
var win = Fl_Double_WindowNew(620,520,"Terminal Test")
var edt = Fl_Text_EditorNew(10,10,Fl_WidgetGetW(win)-20,Fl_WidgetGetH(win)-20)
var buf = Fl_Text_BufferNew()
Fl_Text_DisplaySetBuffer(edt,buf)
Fl_Text_BufferAddModifyCallback(buf,@ModifyCB,edt)
Fl_GroupSetResizable(win,edt)
Fl_WindowShow(win)
Fl_Run()

MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FLTK-term

Post by MrSwiss »

dasyar wrote:I guess the While...Wend would be replaced with a SUB?
No, you can put it into a Sub/Function (but NOT replace it), since:

you are "getting" the String "Byte by Byte" --- also, adding Chr(10) after each Byte, would give
you something like a String-Array (NOT what you want!). You'll have to *make* the String inside
While ... Wend and add the CR (or CRLF) after the W...W Loop, similar to:

Code: Select all

...
' before entering the Loop, clear the "receiver"-String(s)
InText = "" : buffer = ""

While LOC( 1 ) > 0
    buffer = Input( LOC( 1 ), #1 )    ' #1 is the Serial-Port (NOT a File)
    ' use a 'outside' declared String, e.g. 'InText' (otherwise the String is "lost")
    InText += buffer    ' add Byte to String
    buffer = ""
Wend

InText += Chr(10)    ' finish the String with CR (or CRLF), if needed only!
...
Then, you only have to tell the FLTK-Subroutine, to display the (finished/complete) String ...
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FLTK-term

Post by MrSwiss »

General to the use of "External-Library's":

The code (written in FB) has to deliver, what the provided Procedure (of the LIB) can handle.
This is called a *Dependency*.

One can't give it a Byte/Short/Long/LongInt/Single/Double, when the Procedure expects a
String, meaning:
the above mentioned NUMERIC Variables/Constants, have to be converted to String, before
"handing" them over, to the LIB-Procedure.

The same applies vice-versa, a String "received" from LIB-Procedure, may have to be conver-
ted to the respective NUMERIC Var., before it can be used in "FB-Code".

This must be clear, to anybody, wanting to use any "External LIB" to interface with FB-Code!
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Post by dasyar »

Below is my latest test program. I added a menu bar, and open port code.

The program below compiles without errors, and it is a very rough code implementation. For some reason when I added the menu bar, the bottom of the window does not have the nice border for the editor window, not sure how to fix that. I added the message box for showing that the 'Open COM' worked, I will have to improve on that. I also added a 'Close' to the 'Quit' selection to make sure that the port does get closed when you hit the 'Quit' button.

Now that I am opening the Com Port, I was trying to figure out how you would insure a port close if you did not use the 'Quit' button?

Next thing will be to hook something up on the com port and see what the other end is really getting.

Code: Select all

#INCLUDE "fltk-c.bi"

DECLARE SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)

DIM SHARED AS Fl_Menu_Bar PTR Menu

sub ModifyCb cdecl (byval p           as long, _
                    byval nInserted   as long, _
                    byval nDeleted    as long, _
                    byval nRestyled   as long, _
                    byval deletedText as const zstring ptr, _
                    byval Editor   as any ptr)
  if nInserted=0 andalso nDeleted=0 then exit sub 
  dim as Fl_Text_Buffer ptr TextBuffer = Fl_Text_DisplayGetBuffer(Editor)
  dim as zstring ptr Text
  dim as long e , s=p , char = Fl_Text_BufferCharAt(TextBuffer,p)
  if (nInserted > 0) then e = s + nInserted
  if (nDeleted  > 0) then e = s + nDeleted
  s = Fl_Text_BufferLineStart(TextBuffer,s)
  e = Fl_Text_BufferLength(TextBuffer)
  'e = Fl_Text_BufferLineEnd(TextBuffer,e)
  Text  = Fl_Text_BufferTextRange(TextBuffer ,s,e)
  if char=10 andalso len(*text)>1 then
    'print "exeute command: " & *Text
    Print #1,*Text
  end if
end sub

SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
	close
	END
END SUB
'
' main
'
var win = Fl_Double_WindowNew(620,560,"Terminal Test")
var edt = Fl_Text_EditorNew(10,30,Fl_WidgetGetW(win)-20,Fl_WidgetGetH(win)-20)
var buf = Fl_Text_BufferNew()
' Add a menu bar
Menu = Fl_Menu_BarNew(0,0,620,20)
Fl_Menu_Add(Menu,"Quit",,@EndProgram())
''''
' Open COM port
Open Com "COM3:115200,n,8,1,cs0,ds0,cd0,rs" as #1
flMessageBox("COM Information","COM port!")
''''
Fl_Text_DisplaySetBuffer(edt,buf)
Fl_Text_BufferAddModifyCallback(buf,@ModifyCB,edt)
Fl_GroupSetResizable(win,edt)
Fl_WindowShow(win)
Fl_Run()
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Post by dasyar »

A quick question, I am using

Code: Select all

SUB About CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
	flMessageBox("About Information", _ 
	"This is the FBGterm terminal program." _ 
	"Coded in freeBASIC using FLTK toolkit.")
END SUB
is their someway to insert CR in the messagebox, so I could see something like this:
This is the FBGterm terminal program.
Coded in freeBASIC using FLTK toolkit.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FLTK-term

Post by MrSwiss »

There are two Options:
  • Escaped String (see FB-Doc's for details)
  • Add a LF (line feed) to the first String

Code: Select all

...
"This is the FBGterm terminal program." + Chr(10) _
+ "Coded in FreeBASIC using FLTK toolkit."
...
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FLTK-term

Post by D.J.Peters »

You can use the new line character !"\n" also.

Joshy

Code: Select all

flMessageBox("About Information", _
   !"This is the FBGterm terminal program.\n" _
   "Coded in freeBASIC using FLTK toolkit.")
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Post by dasyar »

Since I am using Windows 10, inserting "/n" is not working for me, it just shows /n. Must be a Linux thing only. Also not sure what the '!' is supposed to do, I did not see anything different occurring.

Now all I have to do is figure out how I am going to hook up two computers via the USB port so I can start testing the terminal program. Not sure if I have a USB cable for the job.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Post by dasyar »

I have a one sided conversation going with the program. So, the send part seems to be working as expected.

My setup, Windows 10 to develop the program, and a Raspberry Pi on the other end of a USB-TTL cable. The Windows 10 side is COM4 and the Raspberry Pi side is using /dev/ttyAMA0, with a BAUD of 115,200.

Now I think I am ready for the receive part for the terminal program, but I have no idea as to how I should approach this. Yes, this should include displaying the incoming text to the editor window. Any help will be appreciated.

Code: Select all

'FBGterm.bas
#INCLUDE "fltk-c.bi"

' Declarations
DECLARE SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
DECLARE SUB About CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)

' Variables and etc
DIM SHARED AS Fl_Menu_Bar PTR Menu

' Text editor window control
sub ModifyCb cdecl (byval p           as long, _
                    byval nInserted   as long, _
                    byval nDeleted    as long, _
                    byval nRestyled   as long, _
                    byval deletedText as const zstring ptr, _
                    byval Editor   as any ptr)
  if nInserted=0 andalso nDeleted=0 then exit sub 
  dim as Fl_Text_Buffer ptr TextBuffer = Fl_Text_DisplayGetBuffer(Editor)
  dim as zstring ptr Text
  dim as long e , s=p , char = Fl_Text_BufferCharAt(TextBuffer,p)
  if (nInserted > 0) then e = s + nInserted
  if (nDeleted  > 0) then e = s + nDeleted
  s = Fl_Text_BufferLineStart(TextBuffer,s)
  e = Fl_Text_BufferLength(TextBuffer)
  Text  = Fl_Text_BufferTextRange(TextBuffer ,s,e)
' Check for just a CR
'  If char=10 then
'	Print #1, chr(13)
'  End If
' Check for string and CR
  if char=10 andalso len(*text)>1 then    
    Print #1,*Text + chr(13);    
  end if
end sub

' End the program, close open COMs
SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
	close
	END
END SUB

' Help mesage box
SUB About CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
	flMessageBox("About Information", _ 
	"This is the FBGterm terminal program." + chr(10) + _ 
	"Coded in freeBASIC" + chr(10) + _
	"Enhanced with FLTK GUI toolkit.")
END SUB
'
' main
'
var win = Fl_Double_WindowNew(900,600,"FBGterm test")
var edt = Fl_Text_EditorNew(10,30,Fl_WidgetGetW(win)-20,Fl_WidgetGetH(win)-20)
var buf = Fl_Text_BufferNew()
' Add a menu bar
Menu = Fl_Menu_BarNew(0,0,900,20)
Fl_Menu_Add(Menu,"Quit",,@EndProgram())
Fl_Menu_Add(Menu,"About",,@About())
''''
' Open COM port
Open Com "COM4:115200,n,8,1,cs0,ds0,cd0,rs" as #1
'Test if COM available
If Err <> 0 Then
flMessageBox("COM Information","Error opening COM port!")
Endif
''''
Fl_Text_DisplaySetBuffer(edt,buf)
Fl_Text_BufferAddModifyCallback(buf,@ModifyCB,edt)
Fl_GroupSetResizable(win,edt)
Fl_WindowShow(win)
Fl_Run()
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Post by dasyar »

To test for an incoming string/char, I added GetInBuff() SUB, which prints out too the console screen the incoming string/char, not working as expected.

What is occurring is that I have to press a key in the GUI screen to get something too show up on the console screen. I thought that the incoming string/char would automatically show up in the console screen, not happening. So it seems like the Text Editor screen only responds to a keypress, in order to get things to move along. The one good thing is that when I do a keypress, it does display what was typed on the other end, so that is a good thing.

I am not sure as to how I can get GetInBuff() SUB to interact automatically within the Text Editor, in the test scenario of the console window, let alone getting the incoming string/char to work with the Text Editor directly.

Code: Select all

'FBGterm.bas
#INCLUDE "fltk-c.bi"

' Declarations
DECLARE SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
DECLARE SUB About CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
DECLARE SUB GetInBuff()

' Variables and etc
DIM SHARED AS Fl_Menu_Bar PTR Menu

' Text editor window control
sub ModifyCb cdecl (byval p           as long, _
                    byval nInserted   as long, _
                    byval nDeleted    as long, _
                    byval nRestyled   as long, _
                    byval deletedText as const zstring ptr, _
                    byval Editor   as any ptr)
  if nInserted=0 andalso nDeleted=0 then exit sub 
  dim as Fl_Text_Buffer ptr TextBuffer = Fl_Text_DisplayGetBuffer(Editor)
  dim as zstring ptr Text
  dim as long e , s=p , char = Fl_Text_BufferCharAt(TextBuffer,p)
  if (nInserted > 0) then e = s + nInserted
  if (nDeleted  > 0) then e = s + nDeleted
  s = Fl_Text_BufferLineStart(TextBuffer,s)
  e = Fl_Text_BufferLength(TextBuffer)
  Text  = Fl_Text_BufferTextRange(TextBuffer,s,e)
' Check for just a CR
'  If char=10 then
'	Print #1, chr(13)
'  End If
' Check for string and CR
  if char=10 andalso len(*text)>1 then    
    Print #1,*Text + chr(13); ' Send Text Com #1   
  end if
' Get and display incoming string
'
  GetInBuff()
  Print chr(13)

end sub

SUB GetInBuff()
  Dim As String inBuffer
  While LOC(1)>0
	inBuffer = Input$(LOC(1),#1)
	Print inBuffer + chr(13);
	Print #1, Chr(10);
  Wend
END SUB
' End the program, close open COMs
SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
	close
	END
END SUB

' Help mesage box
SUB About CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
	flMessageBox("About Information", _ 
	"This is the FBGterm terminal program." + chr(10) + _ 
	"Coded in freeBASIC" + chr(10) + _
	"Enhanced with FLTK GUI toolkit.")
END SUB
'
' main
'
var win = Fl_Double_WindowNew(900,600,"FBGterm test")
var edt = Fl_Text_EditorNew(10,30,Fl_WidgetGetW(win)-20,Fl_WidgetGetH(win)-20)
var buf = Fl_Text_BufferNew()
' Add a menu bar
Menu = Fl_Menu_BarNew(0,0,900,20)
Fl_Menu_Add(Menu,"Quit",,@EndProgram())
Fl_Menu_Add(Menu,"About",,@About())
''''
' Open COM port
Open Com "COM4:115200,n,8,1,cs0,ds0,cd0,rs" as #1
'Test if COM available
If Err <> 0 Then
flMessageBox("COM Information","Error opening COM port!")
Endif
''''
Fl_Text_DisplaySetBuffer(edt,buf)
Fl_Text_BufferAddModifyCallback(buf,@ModifyCB,edt)
Fl_GroupSetResizable(win,edt)
Fl_WindowShow(win)
Fl_Run()
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FLTK-term

Post by MrSwiss »

dasyar wrote:Now I think I am ready for the receive part for the terminal program, but I have no idea as to how I should approach this. Yes, this should include displaying the incoming text to the editor window. Any help will be appreciated.
Simply see further above ...
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Post by dasyar »

Well, I reread "the further above", I am still trying to figure out what you have stated there.

My general impression of how the Text Editor is working is such, when you press a key, it shows the keypress on the screen, meaning to me, it is working with a char, and not a string, but it does go further to add the keypress to a string buffer, which is Text, and that gets sent. It seems like the Text Editor has some parts that work on a char level, and some parts that work on a string level. So, if I use the 'char' of the statement 'char = Fl_Text_BufferCharAt(TextBuffer,p)', will that get incoming chars onto the Text Editor screen?

The other part that I am curious about is why I have to do a keypress too have something show up in the console window, with my code lines that I have now?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FLTK-term

Post by MrSwiss »

dasyar wrote:Well, I reread "the further above", I am still trying to figure out what you have stated there.
This is the CORE of the "receive" Routine. You just have to display the resulting String "InText" ...
Displaying each "incoming" CHAR, simply makes no sense, in this context, speed etc. .
Post Reply