MyTerminal graphic prog

For issues with communication ports, protocols, etc.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: MyTerminal graphic prog

Post by BasicCoder2 »

dasyar wrote:I am trying to stay away from making this look like my personal blog, and maybe "tutorial" was the wrong word to use.
Well maybe it is your project?
I see the thread has been move to the hardware interface/communication section.

It is not sleep 2.1 it is sleep 2,1 (comma not full stop)
When you click on the ComboBox and make a selection, it is not displaying the chosen selection.
The fun of programming is debugging :)
However how you go about designing code can make bugs much less likely.
Cowboy programming as I have done is prone to errors.

My code above did have errors and I went back and corrected them.
If you write your own code based on your own logic and not just copy without understanding, errors become easier to figure out.

Checking area for header is y to y+16
Checking area for list is y+16 to y+h
Item on list will be (my-y-16)\16

Code: Select all

'  +-----------------+ x+w,y
'  | BAUD -          |
'  +-----------------+ x+w,y+16
'  |   9600          |
'  |  19200          |
'  |  38400          |
'  |  57600          |
'  | 115200          |
'  +-----------------+ x+w,y+h
Your version with corrections. Note I can't be bothered with .bi files in such a small example so I just place them at the start.

Code: Select all

Type COMBO_BOX
   As String title
   As String List(0 to 4)
   As Integer x
   As Integer y
   As Integer w
   As Integer h
   As String selItem
   As Integer a
End Type

' ComboBox
Sub drawComboBox(cmb as COMBO_BOX)   
   Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16), rgb(0,0,0), b
   Draw String (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
   If cmb.a = 1 Then      
      Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b      
      For i as Integer = 0 to 4         
         Draw String (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
      Next i
   End If
End Sub

Type BUTTON
   As String title
   As Integer x
   As Integer y
   As Integer w
   As Integer h
   As ulong c1
   As ulong c2
   As Integer a
End Type

' Button
Sub drawButton(btn as BUTTON)
   Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf ' Antique white
' Button outline, fixed color is gray
   Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(190,190,190), b  'Gray
   Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub
'================================================================================
' fbtermg.bas
'
' October 26, 2017
'

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using fb
#endif
'#include "mygui.bi"

'Screen 20  '1024x768
Screenres 640,480,32
Color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"

Dim e As EVENT
Dim Shared as Integer mx,my,mb



Dim Shared As BUTTON btnConnect, btnQuit
Dim Shared As COMBO_BOX cmbPort, cmbBaud


Declare Sub update()

Dim As Integer hit

' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 6      ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"
'btnQuit.c2 = rgb(238,223,204)  ' Button background
'btnQuit.c1 = rgb(0,250,154)        ' Button foreground

' Connect button
btnConnect.x = 60
btnConnect.y = 0
btnConnect.w = 8 * 9
btnConnect.h = 16 * 6
btnConnect.title = "Connect"
'btnQuit.c2 = rgb(238,223,204)
'btnQuit.c1 = rgb(0,250,154)

'Port combobox
cmbPort.x = 150
cmbPort.y = 0
cmbPort.w = 8 * 22
cmbPort.h = 16 * 6
cmbPort.title = "Port -"

cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:"
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/TTYUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"

'Baud combobox
cmbBaud.x = 345
cmbBaud.y = 0
cmbBaud.w = 8 * 22
cmbBaud.h = 16 * 6
cmbBaud.title = "BAUD -"

cmbBaud.list(0) = "9600"
cmbBaud.list(1) = "19200"
cmbBaud.list(2) = "38400"
cmbBaud.list(3) = "57600"
cmbBaud.list(4) = "115200"


Do
   getmouse mx,my,,mb  ' Start mouse
   ' Close the window using the "X"   
   If(ScreenEvent(@e)) Then
      Select Case e.Type
      Case EVENT_WINDOW_CLOSE   ' X in window
         Close
         End
      End Select
   End If
   
   If mb = 1 Then
      hit = 0
      ' Mouse over btnQuit
      If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then
         btnQuit.a = 1
         btnConnect.a = 0
         cmbBaud.a = 0
         cmbPort.a = 0
         hit = 1
         Close
         End
      End If
      
      If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
         btnQuit.a = 0
         btnConnect.a = 1
         cmbBaud.a = 0
         cmbPort.a = 0
         hit = 1
      End If
      
        'is it over cmbBaud HEADER?
        if mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y and my<cmbBaud.y+16 then
            cmbBaud.a = 1
            cmbPort.a = 0
            hit = 1
        end if
        
        'is it over cmbPort HEADER?
        if mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y and my<cmbPort.y+16 then
            cmbPort.a = 1
            cmbBaud.a = 0
            hit = 1
        end if
        
        'is it over cmbBaud LIST
        if cmbBaud.a = 1 and mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y + 16 and my<cmbBaud.y+cmbBaud.h then
            cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y-16)\16)
            hit = 1
        end if
        
        'is it over cmbPort LIST
        if cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
            cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
            hit = 1
        end if 
        
        if hit = 0 then  'mouse down outside any object so deactivate any activated object
            cmbPort.a = 0
            cmbBaud.a =0
        end if
      
   End If
   
   update()
   ' Mouse event
   While mb = 1
      getmouse mx,my,,mb
   Wend
   Sleep 2,1
Loop until multikey(&H01)  ' Esc key to close

''''''''''''''''''''''''''''''
End

' Screen update
Sub update()
   Screenlock
   Cls
   drawButton(btnQuit)
   drawButton(btnConnect)    ' Not active for now
   drawComboBox(cmbPort)
   drawComboBox(cmbBaud)
   Screenunlock
End Sub
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

Now comes the harder part, how to make a functional 'Connect' session. Below is my code snippet for testing to see if the cmbPort select has been selected and usable with Open Com. I used two different styles, and they both seem to be working.

Now once I connect, what is the best way to setup I/O? Since the program has an update() within the Do ... Loop that will wipe out any Print or Draw String output. If I insert another Do Loop within the 'Connect' and use 'View Print 4 to 47', I guess all I would need then is some way of breaking out of this Do Loop. Not sure which is the best way to accomplish this 'Connect' session.

Code: Select all

If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
			btnQuit.a = 0
			btnConnect.a = 1
			cmbBaud.a = 0
			cmbPort.a = 0
			hit = 1
			Open Com p & b & ",n8,1,cs0,ds0,cd0,rs" As #1
			If Err <> 0 Then
				View Print 4 to 7
				Print "Error opening", p
				Sleep 3000,1
			End If
				'comerr = "Error opening "
				'Draw "BM 0,50"  'This is not necessary
				'Draw String (0,25),comerr & p,4
				'Sleep 3000,1
			'End If
			
		End If

' Is it over cmbPort LIST
		If cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
            cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
			p = cmbPort.selItem
			hit =1			
		End If
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: MyTerminal graphic prog

Post by BasicCoder2 »

My computer doesn't have a serial port so I have no way to test it.
In the old days I used bidirectional parallel ports to interface with hardware.
With regards to wiping out any print or draw string output by the update() you would simple have your strings stored in string variables and redrawn with each cycle.
.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: MyTerminal graphic prog

Post by D.J.Peters »

@BasicCoder2 you can buy a USB to RS233 adapter or cable for 1$ at eBay :-)

The 5$ arduino clones comes with USB to RS232 adapters onboard also.

Joshy
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: MyTerminal graphic prog

Post by BasicCoder2 »

@Joshy,
That's true but I also meant I have nothing to connect a serial port to. Actually I might have such an adapter if that is used to program the Arduino. I haven't been working on any electronics projects for a long time now. The Arduino was the last thing I played with. Like dasyar I had an interest in robotics but I used a small windows xp laptop for a brain and the k8055 to interface to the motors and sensors. Commands to the robot was sent via a wireless keyboard thus no physical connections required. That was seven years ago now. I don't seem to be enthused anymore for any kind of project.
.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

Below is a code snippet of a test 'Connect' function. I have a data streaming source on the ttyUSB1 port, so I get streaming data, every 30 seconds .

Open Com works as expected, I get the correct port and baud selection connection. I think that my 'While LOC(1)' loop captures one char at a time, I wonder if this should be changed to larger buffer, so I could view, maybe 256 chars at a time. The buffer variable at this time is 'Dim Shared As String buffer', how would I change that to hold 256 chars, for test purposes?

I am not sure how I will change this to a real terminal I/O session, but at least I know that I am able to get connected to a selected port and baud.

Code: Select all

		If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
			btnQuit.a = 0
			btnConnect.a = 1
			cmbBaud.a = 0
			cmbPort.a = 0
			hit = 1
			Locate 3,0
			Print "Comm session"
			Sleep 1000,1
			If tagcomm = 0 Then
				Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
				If Err <> 0 Then
					View Print 4 to 7
					Print "Error opening", p
					Sleep 1000,1				
				End If
				tagcomm = 1 ' Com port is open
			End If
			' Capture incoming data
			While LOC(1) > 0
				buffer = Input(LOC(1),#1)
				'Locate 5,0
				'Print buffer;
				Draw String (0,30), buffer
			Wend
			Sleep 3000,1
			
		End If
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

I am having problems with the graphics char output, when I use either Print "xxx" or Draw String, in particular places. In some places when I use Print "xxx", the char display is normal, in other places it displays as a line of vertical black lines. This also occurs when I have a View Print present.

In this program, I guess I am not comprehending how to use the graphics screen correctly.

The program below seems to be working as expected, except for the chars display part when in Com I/O session. Now I have to figure how to get the keyboard I/O to work. At this point when I implement the Print Key to display a key press, the screen gets cleared when I enter a Com I/O. Weird things are occurring.

Code: Select all

' fbtermg.bas
'
' October 26, 2017
'

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using fb
#endif
#include "mygui.bi"

'Screen 20  '1024x768
Screenres 640,480,32
Color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"

Dim e As EVENT
Dim Shared as Integer mx,my,mb
Dim Shared As String p,b,comerr,Key, buffer
Dim Shared As Integer tagcomm = 0


Dim Shared As BUTTON btnConnect, btnQuit
Dim Shared As COMBO_BOX cmbPort, cmbBaud


Declare Sub update()

Dim As Integer hit

Sub com_IO()

Do
	Key = InKey
	If Key = chr(113) Then ' q					
		Locate 3,0
		Print "Leaving Commm session" 'This displays vertical black lines
		Sleep 3000,1
		
		update()
		Exit Do
	End If
	
	If Key = Chr(13) Then
		Print #1, Chr(13)
	Else
		Print #1, Key;
		'Need to code for keyboard echo on/off
		'Print Key + " "  ' Using this clears the screen, when in I/O
	End If
	
	While LOC(1) > 0
		buffer = Input(LOC(1),#1)
		'Locate 5,0
		'Print buffer;
		Draw String (0,30), buffer  'This displays vertical black lines
		
	Wend
Loop
End Sub


' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 6      ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"
'btnQuit.c2 = rgb(238,223,204)  ' Button background
'btnQuit.c1 = rgb(0,250,154)        ' Button foreground

' Connect button
btnConnect.x = 60
btnConnect.y = 0
btnConnect.w = 8 * 9
btnConnect.h = 16 * 6
btnConnect.title = "Connect"
'btnQuit.c2 = rgb(238,223,204)
'btnQuit.c1 = rgb(0,250,154)

'Port combobox
cmbPort.x = 150
cmbPort.y = 0
cmbPort.w = 8 * 22
cmbPort.h = 16 * 6
cmbPort.title = "Port -"

cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:"
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/TTYUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"

'Baud combobox
cmbBaud.x = 345
cmbBaud.y = 0
cmbBaud.w = 8 * 22
cmbBaud.h = 16 * 6
cmbBaud.title = "BAUD -"

cmbBaud.list(0) = "9600"
cmbBaud.list(1) = "19200"
cmbBaud.list(2) = "38400"
cmbBaud.list(3) = "57600"
cmbBaud.list(4) = "115200"

update()
Do
	getmouse mx,my,,mb  ' Start mouse
' Close the window using the "X"	
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		Case EVENT_WINDOW_CLOSE   ' X in window
			Close
			End
		End Select
	End If
	
	If mb = 1 Then
		hit = 0
' Mouse over btnQuit
		If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then
			'btnQuit.a = 1
			'btnConnect.a = 0
			'cmbBaud.a = 0
			'cmbPort.a = 0
			'hit = 1
			Close
			End
		End If
' Mouse over btnConnect		
		If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
			cmbBaud.a = 0
			update()
			hit = 1
		' Announce	
			Locate 3,0
			Print "Com session"  ' This displays in readable chars
			drawButton(btnConnect)
		' Check to if tagcomm is 0
			If tagcomm = 0 Then
				Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
				If Err <> 0 Then					
					Print "Error opening", p	' This displays normal				
					goto bout				
				End If
				tagcomm = 1 ' Com port is open
			End If
		' Open a Com I/O session
			com_IO()			
			bout:			
		End If
		' Is it over cmbPort HEADER
		If mx>cmbPort.x And mx<cmbPort.x+cmbPort.w And my>cmbPort.y And my<cmbPort.y+16 Then
			cmbPort.a = 1
			cmbBaud.a = 0
			hit = 1
			update()
		End If
		' Is it over cmbPort LIST
		If cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
            cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
			p = cmbPort.selItem
			hit =1
			update()
						
		End If
		' Is it over cmbBaud HEADER
		If mx>cmbBaud.x And mx<cmbBaud.x+cmbBaud.w And my>cmbBaud.y And my<cmbBaud.y+16 Then
			cmbBaud.a = 1
			cmbPort.a = 0
			hit = 1
			update()
		End If
		'Is it over cmbBaud LIST
		If cmbBaud.a = 1 and mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y + 16 and my<cmbBaud.y+cmbBaud.h then
            cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y-16)\16)
			b = cmbBaud.selItem
			hit =1
			update()
		End If
		
		If hit = 0 Then
			cmbPort.a = 0
			cmbBaud.a = 0
		End If
	End If
	
	'update()
	' Mouse event
	While mb = 1
		getmouse mx,my,,mb
	Wend
	Sleep 2,1
Loop until multikey(&H01)  ' Esc key to close
Close
''''''''''''''''''''''''''''''
End




' Screen update
Sub update()
	Screenlock
	Cls
	drawButton(btnQuit)
	drawButton(btnConnect)    ' Not active for now
	drawComboBox(cmbPort)
	drawComboBox(cmbBaud)
	Screenunlock
End Sub
mygui.bi

Code: Select all


Type COMBO_BOX
	As String title
	As String List(0 to 4)
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As String selItem
	As Integer a
End Type

' ComboBox
Sub drawComboBox(cmb as COMBO_BOX)	
	Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16), rgb(0,0,0), b
	Draw String (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
	If cmb.a = 1 Then		
		Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b		
		For i as Integer = 0 to 4			
			Draw String (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
		Next i
		
	End If
End Sub

Type BUTTON
	As String title
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As ulong c1
	As ulong c2
	As Integer a
End Type

' Button
Sub drawButton(btn as BUTTON)
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf ' Antique white
' Button outline, fixed color is gray
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(190,190,190), b  'Gray
	Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub

dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

I think I narrowed the problem down to Screenres and Com port. It seems that some choices of Screenres defs has a specific affect on how the Com buffer gets displayed to the screen.

In the test program below, when the Screenres 640,480,16 is used then the Com buffer output to the screen is as expected. But when I use that Screenres value in my fbtermg.bas program, the Com buffer output to the screen is completely different. I guess there is a connection between Screenres and Com buffer, anybody know what the secret is?

Code: Select all

' gui_test.bas

Screenres 640,480,16
'Screen 12

Color rgb(0,0,0),rgb(135,206,250):Cls

Dim As String text1,buffer

text1 = "What is it?"

Draw String (0,0),text1

locate 4,0
Print "Do not know"

Open Com "/dev/ttyUSB1:115200,n,8,1,cs0,ds0,cd0,rs" As #1
If Err <> 0 Then
	'View Print 4 to 7
	Print "Error opening"				
End If

do

While LOC(1) > 0
	buffer = Input(LOC(1),#1)
	Print buffer;
Wend

loop until multikey(&H01)
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

The below program seems to be working as expected. With this version of code and using Sreenres 640,480,16, the display of the Com buffer is what you would expect.

Because I am using a comm connection that is streaming data, I have not implemented a keyboard I/O functionality in this version. I will probably set up another device that has an I/O capable program to test out the keyboard I/O capability on this program. Maybe a Raspberry Pi with the login on the ttyS0 pins, might be a useful experiment.

If somebody has a chance to do a real live test and see where some bugs are hidden, would be appreciated.

Code: Select all

' fbtermg.bas
'
' 
' October 29, 2017
'
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using fb
#endif
#include "mygui.bi"

Screenres 640,480,16
Color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"

Dim e As EVENT

Dim Shared as Integer mx,my,mb
Dim Shared as String Key,buffer,p,b
Dim Shared As Integer tagcomm = 0

Dim Shared As BUTTON btnConnect, btnQuit
Dim Shared As COMBO_BOX cmbPort, cmbBaud

Dim As Integer hit

Declare Sub update()

' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 6      ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"
btnQuit.c2 = rgb(238,223,204)  ' Button background
btnQuit.c1 = rgb(25,25,112)        ' Button foreground

' Connect button
btnConnect.x = 60
btnConnect.y = 0
btnConnect.w = 8 * 9
btnConnect.h = 16 * 6
btnConnect.title = "Connect"
btnQuit.c2 = rgb(238,223,204)
btnQuit.c1 = rgb(25,25,112)

'Port combobox
cmbPort.x = 150
cmbPort.y = 0
cmbPort.w = 8 * 22
cmbPort.h = 16 * 6
cmbPort.title = "Port -"

cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:"
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/TTYUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"

'Baud combobox
cmbBaud.x = 345
cmbBaud.y = 0
cmbBaud.w = 8 * 22
cmbBaud.h = 16 * 6
cmbBaud.title = "BAUD -"

cmbBaud.list(0) = "9600"
cmbBaud.list(1) = "19200"
cmbBaud.list(2) = "38400"
cmbBaud.list(3) = "57600"
cmbBaud.list(4) = "115200"


drawButton(btnQuit)
drawButton(btnConnect)
drawComboBox(cmbPort)
drawComboBox(cmbBaud)

Do
getmouse mx,my,,mb  ' Start mouse

' Close the window using the "X"	
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		Case EVENT_WINDOW_CLOSE   ' X in window
			Close
			End
		End Select
	End If
	
If mb = 1 Then
	hit = 0
' Mouse over btnQuit
	If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then

		Close
		End
	End If
' Mouse over btnConnect	
	If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
		cmbBaud.a = 0
		update()
		hit = 1
		Locate 3,0
		Print "The Connect session"
		' Open and tag the Com port
		If tagcomm = 0 Then
			Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
			If Err <> 0 Then					
				Print "Error opening", p	' This displays normal				
					goto bout				
			End If
			tagcomm = 1 ' Com port is open
		End If
		
	bout:	
	End If
	
	If mx>cmbPort.x And mx<cmbPort.x+cmbPort.w And my>cmbPort.y And my<cmbPort.y+16 Then
			cmbPort.a = 1
			cmbBaud.a = 0
			hit = 1
			
			update()
	End If
	' Is it over cmbPort LIST
	If cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
        cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
		p = cmbPort.selItem
		hit =1
			
		update()
						
	End If
	' Is it over cmbBaud HEADER
	If mx>cmbBaud.x And mx<cmbBaud.x+cmbBaud.w And my>cmbBaud.y And my<cmbBaud.y+16 Then
		cmbBaud.a = 1
		cmbPort.a = 0
		hit = 1
			
		update()
	End If
	'Is it over cmbBaud LIST
	If cmbBaud.a = 1 and mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y + 16 and my<cmbBaud.y+cmbBaud.h then
            cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y-16)\16)
		b = cmbBaud.selItem
		hit =1
			
		update()
	End If
	
	
End If
	' Mouse event
	While mb = 1
		getmouse mx,my,,mb
	Wend
	Sleep 2,1
' Get and display the Com port buffer	
	While LOC(1) > 0
		buffer = Input(LOC(1),#1)
		
		Print buffer;
		
	Wend

Loop until multikey(&H01)  ' Esc key to close
Close
''''''''''''''''''''''''''''''
End

' Screen update
Sub update()
	Screenlock
	Cls
	drawButton(btnQuit)
	drawButton(btnConnect)    ' Not active for now
	drawComboBox(cmbPort)
	drawComboBox(cmbBaud)
	Screenunlock
End Sub
mygui.bi

Code: Select all


Type COMBO_BOX
	As String title
	As String List(0 to 4)
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As String selItem
	As Integer a
End Type

' ComboBox
Sub drawComboBox(cmb as COMBO_BOX)	
	'Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16), rgb(0,0,0), b
	Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf
	Draw String (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
	If cmb.a = 1 Then		
		Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b
		'Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf	
		For i as Integer = 0 to 4			
			Draw String (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
		Next i
		
	End If
End Sub

Type BUTTON
	As String title
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As ulong c1
	As ulong c2
	As Integer a
End Type

' Button
Sub drawButton(btn as BUTTON)
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf ' Antique white
' Button outline, fixed color is gray
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(190,190,190), b  'Gray
	Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

Finally I got something to work as expected. This is a simple terminal program, you choice the port, BAUD, and then click on 'Connect'. To end the session you can click on 'Quit' or hit the window X.

I made a serial connection to a Raspberry Pi using the Tx/Rx pins, and the serial setting was left at default. After logging on and running SimTermg program, everything seemed to work as expected.

This program will stay as is, I will be reworking my FBtermg program to try out upgrades and some different functionality.

Code: Select all

'SimTerm.bas
'Simple terminal program
'
' October 30, 2017
'

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
using fb
#endif
'#include "mygui.bi"


Screenres 640,480,16
color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "SimTermg Program"

Type COMBO_BOX
	As String title
	As String List(0 to 4)
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As String selItem
	As Integer a
End Type

' ComboBox
Sub drawComboBox(cmb as COMBO_BOX)	
	'Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16), rgb(0,0,0), b
	Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf
	Draw String (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
	If cmb.a = 1 Then		
		Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b		
		For i as Integer = 0 to 4			
			Draw String (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
		Next i
		
	End If
End Sub

Type BUTTON
	As String title
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As ulong c1
	As ulong c2
	As Integer a
End Type

' Button
Sub drawButton(btn as BUTTON)
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf ' Antique white
' Button outline, fixed color is gray
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(190,190,190), b  'Gray
	Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub


Dim e As EVENT

Dim Shared as Integer mx,my,mb
Dim As String Key,buffer,p,b
Dim Shared As Double t
Dim Shared As Long column,row
Dim As Boolean flag

Dim Shared As BUTTON btnConnect, btnQuit
Dim Shared As COMBO_BOX cmbPort, cmbBaud
Dim Shared As Integer tagcomm = 0
Dim As Integer hit

Sub TextCursor(ByRef flag As Boolean)
	column = Pos
	row = CsrLin
	If Timer - t > .5 Then
		flag = not(flag)
		If flag Then Print "_" Else Print " ";
		Locate row,column
		t = Timer
	End If
End Sub

' Screen update
Sub update()
	Screenlock
	Cls
	drawButton(btnQuit)
	drawButton(btnConnect)    ' Not active for now
	drawComboBox(cmbPort)
	drawComboBox(cmbBaud)
	locate 3,0
	Screenunlock
End Sub

' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 6      ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"
btnQuit.c2 = rgb(238,223,204)  ' Button background
btnQuit.c1 = rgb(25,25,112)        ' Button foreground

' Connect button
btnConnect.x = 60
btnConnect.y = 0
btnConnect.w = 8 * 9
btnConnect.h = 16 * 6
btnConnect.title = "Connect"
btnQuit.c2 = rgb(238,223,204)
btnQuit.c1 = rgb(25,25,112)

'Port combobox
cmbPort.x = 150
cmbPort.y = 0
cmbPort.w = 8 * 22
cmbPort.h = 16 * 6
cmbPort.title = "Port -"

'Baud combobox
cmbBaud.x = 345
cmbBaud.y = 0
cmbBaud.w = 8 * 22
cmbBaud.h = 16 * 6
cmbBaud.title = "BAUD -"

cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:"
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/ttyUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"

cmbBaud.list(0) = "9600"
cmbBaud.list(1) = "19200"
cmbBaud.list(2) = "38400"
cmbBaud.list(3) = "57600"
cmbBaud.list(4) = "115200"

'Change the Com port and BAUD rate to what you need
'Open Com "COM1:9600,n,8,1,cs0,ds0,cd0,rs" As #1
'Open Com "/dev/ttyUSB2:115200,n,8,1,cs0,ds0,cd0,rs" As #1


'Print "SimTerm, a simple terminal program."

drawButton(btnQuit)
drawButton(btnConnect)
drawComboBox(cmbPort)
drawComboBox(cmbBaud)

' Locate cursor start below menu line
locate 3,0

'main serial send/receive routine
Do
getmouse mx,my,,mb  ' Start mouse

	If(ScreenEvent(@e)) Then
		Select Case e.Type
		case EVENT_WINDOW_CLOSE
			Close
			End
		End Select
	End If
	
   Key = Inkey$
	TextCursor(flag)
	
   If Key = Chr$(27) Then    'Esc key
      Exit Do                'End the program
   End If

   If Key <> "" Then
      If Key = Chr$(13) Then  'This handles CR/LF
         Print #1, Chr$(13);
         Print Chr$(10);
      Else
         Print #1,Key;        'Send the char 
         'Print Key;           'This is the echo
      End If
   End If

'Check for input
    While Loc(1) > 0
        buffer = Input$(Loc(1),#1) 'Grab a char
        Print buffer;              'Print the char
    Wend

' Start of menu item things   
If mb = 1 Then
	hit = 0
	
' Mouse over btnQuit
	If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then

		Close
		End
	End If
' Mouse over btnConnect	
	If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
		cmbBaud.a = 0
		update()
		hit = 1
		' Open and tag the Com port
		If tagcomm = 0 Then
			Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
			If Err <> 0 Then
				Locate 3,0
				Print "Error opening", p				
				goto bout				
			End If
			tagcomm = 1 ' Com port is open
		End If
	bout:
	End If
	
	If mx>cmbPort.x And mx<cmbPort.x+cmbPort.w And my>cmbPort.y And my<cmbPort.y+16 Then
			cmbPort.a = 1
			cmbBaud.a = 0
			hit = 1
			
			update()
	End If
	' Is it over cmbPort LIST
	If cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
        cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
		p = cmbPort.selItem
		hit =1
			
		update()
						
	End If
	' Is it over cmbBaud HEADER
	If mx>cmbBaud.x And mx<cmbBaud.x+cmbBaud.w And my>cmbBaud.y And my<cmbBaud.y+16 Then
		cmbBaud.a = 1
		cmbPort.a = 0
		hit = 1
			
		update()
	End If
	'Is it over cmbBaud LIST
	If cmbBaud.a = 1 and mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y + 16 and my<cmbBaud.y+cmbBaud.h then
            cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y-16)\16)
		b = cmbBaud.selItem
		hit =1
			
		update()
	End If
			
	' Mouse event
	While mb = 1
		getmouse mx,my,,mb
	Wend
	
End If
	
Sleep 1,0
Loop

'close the port(s)
Close
End
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

I am back to the fbtermg.bas program. I added a couple of new menu items, and the menu line is getting filled up very quickly. If I want to add some more menu items, not sure how I will accomplish that.

When I was checking out the program I ran into a problem. When you fill up the screen with text to the bottom of the screen, the next line down pushes the menu line text of the screen. Then the cursor goes into an auto mode where it drops down a line, and continues doing that. Not sure how fix that problem, which raises another question, is there a way to buffer the screen? Then of course I would need scroll bars.

Code: Select all

' fbtermg.bas
'
' October 30, 2017
'
'
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
using fb
#endif
#include "mygui.bi"

Screenres 640,480,16
color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"

Dim e As EVENT
Dim Shared as Long mx,my,mb
Dim As String Key,buffer
Dim Shared As Long tagcomm = 0
Dim As Long hit
Dim Shared As Double t
Dim Shared As Long column,row
Dim Shared As String chEcho,p,b
Dim As Boolean flag

Dim Shared As BUTTON btnQuit,btnClrScrn,btnConnect
Dim Shared As COMBO_BOX cmbEcho,cmbPort,cmbBaud

Declare Sub TextCursor(ByRef flag As Boolean)
Declare Sub update()

' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 6      ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"
btnQuit.c2 = rgb(238,223,204)  ' Button background
btnQuit.c1 = rgb(25,25,112)        ' Button foreground

' ClrScrn button
btnClrScrn.x = 410
btnClrScrn.y = 0
btnClrScrn.w = 10 * 6
btnClrScrn.h = 16 * 6
btnClrScrn.title = "ClrScrn"
btnClrScrn.c2 = rgb(238,223,204)
btnClrScrn.c1 = rgb(25,25,112)

' Connect button
btnConnect.x = 53 '60
btnConnect.y = 0
btnConnect.w = 8 * 9
btnConnect.h = 16 * 6
btnConnect.title = "Connect"
btnQuit.c2 = rgb(238,223,204)
btnQuit.c1 = rgb(25,25,112)

' Echo combobox
cmbEcho.x = 475
cmbEcho.y = 0
cmbEcho.w = 8 * 10
cmbEcho.h = 16 * 6
cmbEcho.title = "Echo - "

cmbEcho.list(0) = "On "
cmbEcho.list(1) = "Off"

'Port combobox
cmbPort.x = 132 '150
cmbPort.y = 0
cmbPort.w = 8 * 20 '22
cmbPort.h = 16 * 6
cmbPort.title = "Port -"

'Baud combobox
cmbBaud.x = 300 '318
cmbBaud.y = 0
cmbBaud.w = 8 * 13
cmbBaud.h = 16 * 6
cmbBaud.title = "BAUD -"

cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:"
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/ttyUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"

cmbBaud.list(0) = "9600"
cmbBaud.list(1) = "19200"
cmbBaud.list(2) = "38400"
cmbBaud.list(3) = "57600"
cmbBaud.list(4) = "115200"

drawButton(btnQuit)
drawButton(btnConnect)
drawButton(btnClrScrn)
drawComboBox(cmbEcho)
drawComboBox(cmbPort)
drawComboBox(cmbBaud)


' Locate cursor start below menu line
locate 3,0

' Main loop
Do
getmouse mx,my,,mb  ' Start mouse

' Window close using the X
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		case EVENT_WINDOW_CLOSE
			Close
			End
		End Select
	End If

' Terminal I/O	
	Key = Inkey
	TextCursor(flag)
	
	If Key = Chr$(27) Then     'Esc key
		Exit Do                'End the program
	End If

	If Key <> "" Then
		If Key = Chr$(13) Then  'This handles CR/LF
			Print #1, Chr$(13);
			Print Chr$(10);
		Else
			Print #1,Key;        'Send the char 
			If chEcho = "On " Then
				Print Key;           'This is the echo
			End If
		End If
   End If

'Check for input
	While Loc(1) > 0
		buffer = Input$(Loc(1),#1) 'Grab a char
		Print buffer;              'Print the char
	Wend
'##############################
' Start of menu control  
If mb = 1 Then
	hit = 0
' Mouse over btnQuit
	If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then

		Close
		End
	End If
' Mouse over btnConnect	
	If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
		cmbBaud.a = 0
		update()
		hit = 1
		If tagcomm = 0 Then
			Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
				If Err <> 0 Then
					Locate 3,0
					Print "Error opening", p				
					goto bout				
				End If
				tagcomm = 1
				Locate 3,0
		End If
	bout:
	End If
	If mx>cmbPort.x And mx<cmbPort.x+cmbPort.w And my>cmbPort.y And my<cmbPort.y+16 Then
			cmbPort.a = 1
			cmbBaud.a = 0
			hit = 1
			
			update()
	End If
	' Is it over cmbPort LIST
	If cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
        cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
		p = cmbPort.selItem
		hit =1
			
		update()
						
	End If
	' Is it over cmbBaud HEADER
	If mx>cmbBaud.x And mx<cmbBaud.x+cmbBaud.w And my>cmbBaud.y And my<cmbBaud.y+16 Then
		cmbBaud.a = 1
		cmbPort.a = 0
		hit = 1
			
		update()
	End If
	'Is it over cmbBaud LIST
	If cmbBaud.a = 1 and mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y + 16 and my<cmbBaud.y+cmbBaud.h then
            cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y-16)\16)
		b = cmbBaud.selItem
		hit =1
			
		update()
	End If


' Mouse over btnClrScrn	
	If mx>btnClrScrn.x And mx<btnClrScrn.x+btnClrScrn.w and my>btnClrScrn.y and my<btnClrScrn.y+16 Then
		update()
	End If
' Mouse over cmbEcho HEADER
	If mx>cmbEcho.x And mx<cmbEcho.x+cmbEcho.w And my>cmbEcho.y And my<cmbEcho.y+16 Then
			cmbEcho.a = 1
			'cmbEcho.a = 0
			hit = 1
			
			update()
	End If
' Mouse over cmbEcho LIST
	If cmbEcho.a = 1 and mx>cmbEcho.x and mx<cmbEcho.x+cmbEcho.w and my>cmbEcho.y + 16 and my<cmbEcho.y+cmbEcho.h Then
        cmbEcho.selItem = cmbEcho.list((my-cmbEcho.y-16)\16)
		chEcho = cmbEcho.selItem
		cmbEcho.a = 0
		hit =1			
		update()
	End If
	
	' Mouse event
	While mb = 1
		getmouse mx,my,,mb
	Wend
End If

Sleep 1,0

Loop until multikey(&H01)  ' Esc key
Close
End

'################################################

Sub TextCursor(ByRef flag As Boolean)
	column = Pos
	row = CsrLin
	If Timer - t > .5 Then
		flag = not(flag)
		If flag Then Print "_" Else Print " ";
		Locate row,column
		t = Timer
	End If
End Sub

' Screen update
Sub update()
	Screenlock
	Cls
	drawButton(btnQuit)
	drawButton(btnClrScrn)
	drawComboBox(cmbEcho)
	drawButton(btnConnect)    ' Not active for now
	drawComboBox(cmbPort)
	drawComboBox(cmbBaud)
	locate 3,0
	Screenunlock
End Sub

mygui.bi

Code: Select all


Type COMBO_BOX
	As String title
	As String List(0 to 4)
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As String selItem
	As Integer a
End Type

' ComboBox
Sub drawComboBox(cmb as COMBO_BOX)	
	'Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16), rgb(0,0,0), b
	Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf
	Draw String (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
	If cmb.a = 1 Then		
		Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b
		'Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf	
		For i as Integer = 0 to 4			
			Draw String (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
		Next i
		
	End If
End Sub

Type BUTTON
	As String title
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As ulong c1
	As ulong c2
	As Integer a
End Type

' Button
Sub drawButton(btn as BUTTON)
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf ' Antique white
' Button outline, fixed color is gray
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(190,190,190), b  'Gray
	Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

I am calling this Version 1.0, I fixed some problems from the previous version. So. this just about replicates the FBterm.bas, in a gui version. Still one item missing, the ability to enter raw numbers, not ascii.

I would like to thank BasicCoder2, again, for the help with the proper usage of Type constructs and related code. Why didn't I think of that? I added more comments than I normally do, have get to the habit of more comments the better.

Now there is another project that I want to do, replicate a Python comm/UI program that uses threads, into a freeBasic version. Anybody have a useful tutorial that shows how to use threads in freeBasic.

Code: Select all

' fbtermg.bas
' Linux version
'
' October 30, 2017
'
' Version 1.0
'
''''''''''''''''''''''''''''''''''''''''
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
using fb
#endif
#include "mygui.bi"

Screenres 640,480,16
color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"

Dim e As EVENT
Dim Shared as Long mx,my,mb
Dim As String Key,buffer
Dim Shared As Long tagcomm = 0
Dim As Long hit
Dim Shared As Double t
Dim Shared As Long column,row
Dim Shared As String chEcho,p,b
Dim As Boolean flag

Dim Shared As BUTTON btnQuit,btnClrScrn,btnConnect
Dim Shared As COMBO_BOX cmbEcho,cmbPort,cmbBaud

Declare Sub TextCursor(ByRef flag As Boolean)
Declare Sub update()

' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 5  '6    ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"
btnQuit.c2 = rgb(238,223,204)  ' Button background
btnQuit.c1 = rgb(25,25,112)        ' Button foreground

' ClrScrn button
btnClrScrn.x = 382
btnClrScrn.y = 0
btnClrScrn.w = 10 * 6
btnClrScrn.h = 16 * 6
btnClrScrn.title = "ClrScrn"
btnClrScrn.c2 = rgb(238,223,204)
btnClrScrn.c1 = rgb(25,25,112)

' Connect button
btnConnect.x = 42  '53
btnConnect.y = 0
btnConnect.w = 8 * 8
btnConnect.h = 16 * 6
btnConnect.title = "Connect"
btnQuit.c2 = rgb(238,223,204)
btnQuit.c1 = rgb(25,25,112)

' Echo combobox
cmbEcho.x = 445
cmbEcho.y = 0
cmbEcho.w = 8 * 10
cmbEcho.h = 16 * 3 ' 2 select items
cmbEcho.title = "Echo - "

cmbEcho.list(0) = "On "
cmbEcho.list(1) = "Off"

'Port combobox
cmbPort.x = 110
cmbPort.y = 0
cmbPort.w = 8 * 20
cmbPort.h = 16 * 7  ' 6 select items
cmbPort.title = "Port -"

'Baud combobox
cmbBaud.x = 275
cmbBaud.y = 0
cmbBaud.w = 8 * 13
cmbBaud.h = 16 * 6  ' 5 select items
cmbBaud.title = "BAUD -"

cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:"
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/ttyUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"
cmbPort.list(5) = "/dev/ttyUSB4:"

cmbBaud.list(0) = "9600"
cmbBaud.list(1) = "19200"
cmbBaud.list(2) = "38400"
cmbBaud.list(3) = "57600"
cmbBaud.list(4) = "115200"

drawButton(btnQuit)
drawButton(btnConnect)
drawButton(btnClrScrn)
drawComboBox(cmbEcho)
drawComboBox(cmbPort)
drawComboBox(cmbBaud)


' Locate cursor start below menu line
locate 3,0
' Keep the menu line from disappearing
' Set printable area between lines 3 and 59
View print 3 to 59
' Main loop
Do
getmouse mx,my,,mb  ' Start mouse

' Window close using the X
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		case EVENT_WINDOW_CLOSE
			Close
			End
		End Select
	End If

' Terminal I/O	
	Key = Inkey
	TextCursor(flag)
	
	If Key = Chr$(27) Then     'Esc key
		Exit Do                'End the program
	End If

	If Key <> "" Then
		If Key = Chr$(13) Then  'This handles CR/LF
			Print #1, Chr$(13);
			Print Chr$(10);
		Else
			Print #1,Key;        'Send the char 
			If chEcho = "On " Then
				Print Key;           'This is the echo
			End If
		End If
		' Check if at line 59
		row = CsrLin
		If row = 59 Then
			locate 58,0
			Print Chr$(10);  'Line feed
			row = 58
		End If
   End If

'Check for input
	While Loc(1) > 0
		' Check if reached end of screen
		row = CsrLin
		If row >= 59 Then
			Locate 58,0
			Print Chr$(10);  ' Line feed
			row = 58
		End If
		buffer = Input$(Loc(1),#1) 'Grab a char
		Print buffer;              'Print the char
	Wend
'##############################
' Start of menu control  
If mb = 1 Then
	hit = 0
' Mouse over btnQuit
	If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then

		Close
		End
	End If
' Mouse over btnConnect	
	If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
		cmbBaud.a = 0
		update()
		hit = 1
		' Prevent a connect after you are already connected
		If tagcomm = 0 Then
			Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
				If Err <> 0 Then
					Locate 3,0
					Print "Error opening", p				
					goto bout				
				End If
				tagcomm = 1
				Locate 3,0
				View print 3 to 59
		End If
	bout:
	End If
	If mx>cmbPort.x And mx<cmbPort.x+cmbPort.w And my>cmbPort.y And my<cmbPort.y+16 Then
			cmbPort.a = 1
			cmbBaud.a = 0
			hit = 1
			
			update()
	End If
	' Is it over cmbPort LIST
	If cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
        cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
		p = cmbPort.selItem
		hit =1
			
		update()
						
	End If
	' Is it over cmbBaud HEADER
	If mx>cmbBaud.x And mx<cmbBaud.x+cmbBaud.w And my>cmbBaud.y And my<cmbBaud.y+16 Then
		cmbBaud.a = 1
		cmbPort.a = 0
		hit = 1
			
		update()
	End If
	'Is it over cmbBaud LIST
	If cmbBaud.a = 1 and mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y + 16 and my<cmbBaud.y+cmbBaud.h then
            cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y-16)\16)
		b = cmbBaud.selItem
		hit =1
			
		update()
	End If


' Mouse over btnClrScrn	
	If mx>btnClrScrn.x And mx<btnClrScrn.x+btnClrScrn.w and my>btnClrScrn.y and my<btnClrScrn.y+16 Then
		update()
	End If
' Mouse over cmbEcho HEADER
	If mx>cmbEcho.x And mx<cmbEcho.x+cmbEcho.w And my>cmbEcho.y And my<cmbEcho.y+16 Then
			cmbEcho.a = 1
			'cmbEcho.a = 0
			hit = 1
			
			update()
	End If
' Mouse over cmbEcho LIST
	If cmbEcho.a = 1 and mx>cmbEcho.x and mx<cmbEcho.x+cmbEcho.w and my>cmbEcho.y + 16 and my<cmbEcho.y+cmbEcho.h Then
        cmbEcho.selItem = cmbEcho.list((my-cmbEcho.y-16)\16)
		chEcho = cmbEcho.selItem
		cmbEcho.a = 0
		hit =1			
		update()
	End If
	
	' Mouse event
	While mb = 1
		getmouse mx,my,,mb
	Wend
End If

Sleep 1,0

Loop until multikey(&H01)  ' Esc key
Close
End

'################################################

Sub TextCursor(ByRef flag As Boolean)
	column = Pos
	row = CsrLin
	If Timer - t > .2 Then  ' Was .5
		flag = not(flag)
		If flag Then Print "_" Else Print " ";
		Locate row,column
		t = Timer
		' Keep cursor from going below line 59
		If row >= 59 Then
			Locate 58,0
			row = 58
		End If
	End If
End Sub

' Screen update
Sub update()
	Screenlock
	Cls
	drawButton(btnQuit)
	drawButton(btnClrScrn)
	drawComboBox(cmbEcho)
	drawButton(btnConnect)
	drawComboBox(cmbPort)
	drawComboBox(cmbBaud)
	locate 3,0
	Screenunlock
End Sub
mygui.bi

Code: Select all


Type COMBO_BOX
	As String title
	As String List(0 to 5)
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As String selItem
	As Integer a
End Type

' ComboBox
Sub drawComboBox(cmb as COMBO_BOX)	
	'Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16), rgb(0,0,0), b
	Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf
	Draw String (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
	If cmb.a = 1 Then		
		Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b
		'Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf	
		For i as Integer = 0 to 5			
			Draw String (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
		Next i
		
	End If
End Sub

Type BUTTON
	As String title
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As ulong c1
	As ulong c2
	As Integer a
End Type

' Button
Sub drawButton(btn as BUTTON)
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf ' Antique white
' Button outline, fixed color is gray
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(190,190,190), b  'Gray
	Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: MyTerminal graphic prog

Post by dasyar »

In this revision, I added Windows compile compatibility. For windows use, it now has COM1: - COM15: ports available. Also I am adding more comments, probably for my own clarity, so a week from now, I will still be able to understand how this program is supposed to work.

Code: Select all

' fbtermg.bas
' Linux and Windows version
'
' October 30, 2017
'
' Version 1.0
'
' November 4, 2017
' Version 1.0 revision 01
' - Added Windows compile compatibility. COM1: - COM15:
' - changed code lines to reflect on Windows screen
' - adding comments for clarity
'
''''''''''''''''''''''''''''''''''''''''
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
using fb
#endif
#include "mygui.bi"

Screenres 640,480,16
color rgb(0,0,0),rgb(135,206,250):cls
WindowTitle "FBtermg Program"
''''''''''''''''''''''''''''''''''''''''
Dim e As EVENT
Dim Shared as Long mx,my,mb
Dim As String Key,buffer
Dim Shared As Long tagcomm = 0
Dim As Long hit
Dim Shared As Double t
Dim Shared As Long column,row
Dim Shared As String chEcho,p,b
Dim As Boolean flag

Dim Shared As BUTTON btnQuit,btnClrScrn,btnConnect
Dim Shared As COMBO_BOX cmbEcho,cmbPort,cmbBaud

Declare Sub TextCursor(ByRef flag As Boolean)
Declare Sub update()
''''''''''''''''''''''''''''''''''''''''
' Define the widgets
' Quit button
btnQuit.x = 0          ' Window column
btnQuit.y = 0          ' Window row
btnQuit.w = 8 * 5  '6    ' Width of button outline
btnQuit.h = 16 * 6     ' Height of button outline
btnQuit.title = "Quit"
btnQuit.c2 = rgb(238,223,204)  ' Button background
btnQuit.c1 = rgb(25,25,112)        ' Button foreground

' ClrScrn button
btnClrScrn.x = 382
btnClrScrn.y = 0
btnClrScrn.w = 10 * 6
btnClrScrn.h = 16 * 6
btnClrScrn.title = "ClrScrn"
btnClrScrn.c2 = rgb(238,223,204)
btnClrScrn.c1 = rgb(25,25,112)

' Connect button
btnConnect.x = 42  '53
btnConnect.y = 0
btnConnect.w = 8 * 8
btnConnect.h = 16 * 6
btnConnect.title = "Connect"
btnQuit.c2 = rgb(238,223,204)
btnQuit.c1 = rgb(25,25,112)

' Echo combobox
cmbEcho.x = 445
cmbEcho.y = 0
cmbEcho.w = 8 * 10
cmbEcho.h = 16 * 3 ' 2 select items
cmbEcho.title = "Echo - "

cmbEcho.list(0) = "On "
cmbEcho.list(1) = "Off"

' Windows compatable
#ifdef __FB_WIN32__
cmbPort.x = 110
cmbPort.y = 0
cmbPort.w = 8 * 20
cmbPort.h = 16 * 16  ' 15 select items
cmbPort.title = "Port -"
#ELSE
'Port combobox
cmbPort.x = 110
cmbPort.y = 0
cmbPort.w = 8 * 20
cmbPort.h = 16 * 7  ' 6 select items
cmbPort.title = "Port -"
#ENDIF

'Baud combobox
cmbBaud.x = 275
cmbBaud.y = 0
cmbBaud.w = 8 * 13
cmbBaud.h = 16 * 6  ' 5 select items
cmbBaud.title = "BAUD -"

#ifdef __FB_WIN32__
cmbPort.list(0) = "COM1:"
cmbPort.list(1) = "COM2:"
cmbPort.list(2) = "COM3:"
cmbPort.list(3) = "COM4:"
cmbPort.list(4) = "COM5:"
cmbPort.list(5) = "COM6:"
cmbPort.list(6) = "COM7:"
cmbPort.list(7) = "COM8:"
cmbPort.list(8) = "COM9:"
cmbPort.list(9) = "COM10:"
cmbPort.list(10) = "COM11:"
cmbPort.list(11) = "COM12:"
cmbPort.list(12) = "COM13:"
cmbPort.list(13) = "COM14:"
cmbPort.list(14) = "COM15:"
#Else
cmbPort.list(0) = "/dev/ttyAMA0:"
cmbPort.list(1) = "/dev/ttyUSB0:"
cmbPort.list(2) = "/dev/ttyUSB1:"
cmbPort.list(3) = "/dev/ttyUSB2:"
cmbPort.list(4) = "/dev/ttyUSB3:"
cmbPort.list(5) = "/dev/ttyUSB4:"
#ENDIF

cmbBaud.list(0) = "9600"
cmbBaud.list(1) = "19200"
cmbBaud.list(2) = "38400"
cmbBaud.list(3) = "57600"
cmbBaud.list(4) = "115200"
''''''''''''''''''''''''''''''''''''''''
' Create the graphics screen
drawButton(btnQuit)
drawButton(btnConnect)
drawButton(btnClrScrn)
drawComboBox(cmbEcho)
drawComboBox(cmbPort)
drawComboBox(cmbBaud)

' Locate cursor start below menu line
locate 3,0
' Keep the menu line from disappearing
' Set printable area between lines 3 and 59
View print 3 to 59
''''''''''''''''''''''''''''''''''''''''
' Main loop
Do
getmouse mx,my,,mb  ' Start mouse

' Window close using the X
	If(ScreenEvent(@e)) Then
		Select Case e.Type
		case EVENT_WINDOW_CLOSE
			Close
			End
		End Select
	End If
''''''''''''''''''''''''''''''''''''''''
' Terminal I/O	
	Key = Inkey
	TextCursor(flag)            'Text cursor
	
	If Key = Chr$(27) Then     'Esc key
		Exit Do                'End the program
	End If

	If Key <> "" Then
		If Key = Chr$(13) Then  'This handles CR/LF
			Print #1, Chr$(13);
			Print Chr$(10);
		Else
			Print #1,Key;        'Send the char 
			If chEcho = "On " Then  'For echo on/off sequencing
				Print Key;           'This is the echo
			End If
		End If
		' Check if at line 59
		row = CsrLin
		If row = 59 Then
			locate 58,0
			Print Chr$(10);  'Line feed
			row = 58
		End If
   End If

'Check for input
	While Loc(1) > 0
		' Check if reached end of screen
		row = CsrLin
		If row >= 59 Then
			Locate 58,0
			Print Chr$(10);  ' Line feed
			row = 58
		End If
		buffer = Input$(Loc(1),#1) 'Grab a char
		Print buffer;              'Print the char
	Wend
'##############################
' Start of menu control  
If mb = 1 Then
	hit = 0
' Mouse over btnQuit
	If mx>btnQuit.x And mx<btnQuit.x+btnQuit.w and my>btnQuit.y and my<btnQuit.y+16 Then

		Close   ' Closes all open files and ports
		End
	End If
' Mouse over btnConnect	
	If mx>btnConnect.x And mx<btnConnect.x+btnConnect.w and my>btnConnect.y and my<btnConnect.y+16 Then
		cmbBaud.a = 0
		update()
		hit = 1
		' Prevent a connect after you are already connected
		If tagcomm = 0 Then
			Open Com p & b & ",n,8,1,cs0,ds0,cd0,rs" As #1
				If Err <> 0 Then
					Locate 3,0
					Print "Error opening", p				
					goto bout				
				End If
				tagcomm = 1
				Locate 3,0
				View print 3 to 59
		End If
	bout:
	End If
	If mx>cmbPort.x And mx<cmbPort.x+cmbPort.w And my>cmbPort.y And my<cmbPort.y+16 Then
			cmbPort.a = 1
			cmbBaud.a = 0
			hit = 1
			
			update()
	End If
	' Is it over cmbPort LIST
	If cmbPort.a = 1 and mx>cmbPort.x and mx<cmbPort.x+cmbPort.w and my>cmbPort.y + 16 and my<cmbPort.y+cmbPort.h then
        cmbPort.selItem = cmbPort.list((my-cmbPort.y-16)\16)
		p = cmbPort.selItem
		hit =1
			
		update()
						
	End If
	' Is it over cmbBaud HEADER
	If mx>cmbBaud.x And mx<cmbBaud.x+cmbBaud.w And my>cmbBaud.y And my<cmbBaud.y+16 Then
		cmbBaud.a = 1
		cmbPort.a = 0
		hit = 1
			
		update()
	End If
	'Is it over cmbBaud LIST
	If cmbBaud.a = 1 and mx>cmbBaud.x and mx<cmbBaud.x+cmbBaud.w and my>cmbBaud.y + 16 and my<cmbBaud.y+cmbBaud.h then
            cmbBaud.selItem = cmbBaud.list((my-cmbBaud.y-16)\16)
		b = cmbBaud.selItem
		hit =1
			
		update()
	End If


' Mouse over btnClrScrn	
	If mx>btnClrScrn.x And mx<btnClrScrn.x+btnClrScrn.w and my>btnClrScrn.y and my<btnClrScrn.y+16 Then
		update()
	End If
' Mouse over cmbEcho HEADER
	If mx>cmbEcho.x And mx<cmbEcho.x+cmbEcho.w And my>cmbEcho.y And my<cmbEcho.y+16 Then
			cmbEcho.a = 1
			'cmbEcho.a = 0
			hit = 1
			
			update()
	End If
' Mouse over cmbEcho LIST
	If cmbEcho.a = 1 and mx>cmbEcho.x and mx<cmbEcho.x+cmbEcho.w and my>cmbEcho.y + 16 and my<cmbEcho.y+cmbEcho.h Then
        cmbEcho.selItem = cmbEcho.list((my-cmbEcho.y-16)\16)
		chEcho = cmbEcho.selItem
		cmbEcho.a = 0
		hit =1			
		update()
	End If
	
	' Mouse event
	While mb = 1
		getmouse mx,my,,mb
	Wend
End If

Sleep 1,0

Loop until multikey(&H01)  ' Esc key
Close  'Closes all open files and ports
End

'################################################

Sub TextCursor(ByRef flag As Boolean)
	column = Pos
	row = CsrLin
	If Timer - t > .2 Then  ' Was .5
		flag = not(flag)
		If flag Then Print "_" Else Print " ";
		Locate row,column
		t = Timer
		' Keep cursor from going below line 59
		If row >= 59 Then
			Locate 58,0
			row = 58
		End If
	End If
End Sub

' Screen update
Sub update()
	Screenlock
	Cls
	drawButton(btnQuit)
	drawButton(btnClrScrn)
	drawComboBox(cmbEcho)
	drawButton(btnConnect)
	drawComboBox(cmbPort)
	drawComboBox(cmbBaud)
	locate 3,0
	Screenunlock
End Sub
mygui.bi

Code: Select all


Type COMBO_BOX
	As String title
	As String List(0 to 16)
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As String selItem
	As Integer a
End Type

' ComboBox
Sub drawComboBox(cmb as COMBO_BOX)	
	'Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + 16), rgb(0,0,0), b
	Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf
	Draw String (cmb.x+4,cmb.y+4),cmb.title & cmb.selItem
	If cmb.a = 1 Then		
		Line (cmb.x,cmb.y)-(cmb.x + cmb.w, cmb.y + cmb.h),rgb(0,0,0),b
		'Line (cmb.x,cmb.y)-(cmb.x + cmb.w,cmb.y + 16),rgb(238,223,204), bf	
		For i as Integer = 0 to 16		
			Draw String (cmb.x+4,cmb.y+i*16+4+16),cmb.list(i)
		Next i
		
	End If
End Sub

Type BUTTON
	As String title
	As Integer x
	As Integer y
	As Integer w
	As Integer h
	As ulong c1
	As ulong c2
	As Integer a
End Type

' Button
Sub drawButton(btn as BUTTON)
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(238,223,204), bf ' Antique white
' Button outline, fixed color is gray
	Line (btn.x,btn.y)-(btn.x + btn.w,btn.y + 16),rgb(190,190,190), b  'Gray
	Draw String (btn.x+4,btn.y+4),btn.title,btn.c1
End Sub

Post Reply