A Gambas Newbie questions

Post your Gambas programming questions here.
AndyGable
Posts: 69
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

A Gambas Newbie questions

Post by AndyGable » Friday 04th December 2020 11:41pm

Hi All,

is it possible to make a Screenless application in Gambas? I ask as I have a lot of Point of sale Printers that I support and I was thinking about creating a "driver" app for each printer.

If it is possible can i use Socket in Gambas so the apps can talk to each other?

Also last question can I convert BASIC code to Gambas?

Code: Select all

Sub StandardCashDrawer

   Dim As String A, B, keyprs
   Dim aint 				As Integer
   Dim t 					As Double
   Dim errflag 			As Integer 
   Dim timeropen 			As Double 
   
   Do
        a = Input$(LOC(PoSPrinter),1) 'clears data from serial buffer
        a = ""
        
        If JournalType = "Paper" Then 	
        		Print #PoSPrinter, ESC ; "="; Chr(1); ' Select Printer ONLY
        EndIf
        
        Print #PoSPrinter, Chr(29);Chr(114);Chr$(2); ' get the status from the printer
        errflag = 0
        t = Timer
        
        If cashdrwopen <> 9999 Then timeropen = Timer
        
        While Loc(PoSPrinter) = 0
            
            If Timer - t > TimeOutLimit Then
                cashdrwopen = -1
                Exit Do
            End If
            Sleep 1
        Wend        
        
      A = Input$(PoSPrinter,1)
		Aint = Val("&H" & Hex(Asc(A),2))


        If (Aint And 1) = 0 Then ' Cash Drawer open
            a = ""
            CashDrwOpen = 9999
            
            If CashDrawerAudioAlarm ="Yes" Then
	            If Timer - timeropen > 30 Then
	            	Sound(c1, dur)
		    			font.fontindex = 3 'small font           		
	       	    	font.backcolor = Black
						font.forecolor = white
						font.drawstring (," C L O S E    D R A W E R ",200,360)                     
	            End If
            End If
        End If

        If (Aint And 1) = 1 Then ' Cash Drawer closed
            Exit Do
        End If
        Sleep 100
    Loop
    
	   If CashDrwOpen = -1 Then
	      'comm timeout
	      Locate 1,1
	      Print "No response from cash drawer"
	   Else
	   	If CashDrawerFucntion = "SaleScreen" Then
				dailyCash -= ChangeDue 		'update the cash figure with the change out
				DailyCustomerCount += 1		'add 1 to customer count
					
				If MSSavedValue > 0 And PrintMSSummary = "No" Then YouSavedTodayPrint
		
				Select Case PrintReciptOption
					Case "Allways"
						SelectEndOFRecipitPrint
					
					Case "AtClose"
						PrintRecipit ' Prints recpit when the cash drawer closes
				End Select
					CreateTimeSaleFile
					DisplayMovingMessgae
					ResertDataFigures
	   	End If
	    		
	    	If CashDrawerFucntion = "NoSale" Then
	    		DailyNosale += 1
	    		DisplayMovingMessgae
				ResertDataFigures
	    	End If
	    	
	    	If CashDrawerFucntion = "SafeDrop" Then
				ResertDataFigures
	    	End If
	   End If
End Sub

User avatar
stevedee
Posts: 431
Joined: Monday 20th March 2017 6:06pm

Re: A Gambas Newbie questions

Post by stevedee » Saturday 05th December 2020 8:47am

AndyGable wrote:
Friday 04th December 2020 11:41pm
...is it possible to make a Screenless application in Gambas? I ask as I have a lot of Point of sale Printers that I support and I was thinking about creating a "driver" app for each printer. ...
Yes, when you start Gambas and create a new Project Type, the first option is: Command-line application

You basically work with a Public Sub Main() rather than a Public Sub Form_Open() routine.

Here is a simple example:-
' Gambas module file

hTimer As Timer
fTime As Float
 
Public Sub Application_Signal(x As Integer)
 
  Print "Program stopped after " & fTime & " seconds"
  Quit
End
  
Public Sub Main()
  
  hTimer = New Timer As "IntTimer"
  Print "Press [Ctrl] + " & Chr(92) & " to stop"  '<ctrl><\>
  Signal[Signal.SIGQUIT].Catch
  hTimer.Delay = 500
  hTimer.Start
End
  
Public Sub IntTimer_Timer()
 
  Print Rand(0, 100)
  fTime += 0.5
End
You may also be interested in my old post: https://captainbodgit.blogspot.com/2017 ... mming.html

You can even do Gambas command line programming without using the IDE, by using Gambas Script: http://captainbodgit.blogspot.com/2019/ ... cript.html
Last edited by stevedee on Saturday 05th December 2020 9:14am, edited 1 time in total.

User avatar
stevedee
Posts: 431
Joined: Monday 20th March 2017 6:06pm

Re: A Gambas Newbie questions

Post by stevedee » Saturday 05th December 2020 9:03am

AndyGable wrote:
Friday 04th December 2020 11:41pm
...If it is possible can i use Socket in Gambas so the apps can talk to each other?...
I think its better to keep things simple, so long as the solution meets the requirements.

For example, lets suppose App1 reads a barcode and needs to transfer it to App2, and that we are happy as long as this takes less than (say) 500ms.
One simple solution might be for App1 to write the barcode as text into a file called CurrentCode.txt
The App2 program would then need to check for the presence of the CurrentCode.txt file periodically (at least <500ms). If/when it finds the file, it would extract the barcode string and delete the file. This is very simple to understand, test & debug.

I hope this helps.

I have a sockets example, but it may not be directly relevant to your needs: http://captainbodgit.blogspot.com/2016/ ... ambas.html

User avatar
cogier
Site Admin
Posts: 696
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: A Gambas Newbie questions

Post by cogier » Saturday 05th December 2020 10:41am

Regarding your code there are quite a few changes to make. I don't understand all the 'printing' parts of the code but here are a few tips:-

Sub StandardCashDrawer change to Sub StandardCashDrawer()
Dim As String A, B, keyprs change to Dim A, B, keyprs As String
Dim t As Double change to Dim t As Float
End sub change to End
Exit Do change to Break Note that Break will get you out of any loop type.

Sleep 100 with stop the program for 100 seconds! Sleep Wait

AndyGable
Posts: 69
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: A Gambas Newbie questions

Post by AndyGable » Saturday 05th December 2020 2:11pm

cogier wrote:
Saturday 05th December 2020 10:41am
Regarding your code there are quite a few changes to make. I don't understand all the 'printing' parts of the code but here are a few tips:-

Sub StandardCashDrawer change to Sub StandardCashDrawer()
Dim As String A, B, keyprs change to Dim A, B, keyprs As String
Dim t As Double change to Dim t As Float
End sub change to End
Exit Do change to Break Note that Break will get you out of any loop type.

Sleep 100 with stop the program for 100 seconds! Sleep Wait
Hi Cogier,

the Print #PoSPrinter, ESC ; command send commands to the Printer to get the status of the cash drawer. In Windows Ican use OPoS to control the cash drawer but in Linux nothing like that exists (well they have JavaPoS but that is WAY to complicated)

AndyGable
Posts: 69
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: A Gambas Newbie questions

Post by AndyGable » Saturday 05th December 2020 2:18pm

stevedee wrote:
Saturday 05th December 2020 9:03am
AndyGable wrote:
Friday 04th December 2020 11:41pm
...If it is possible can i use Socket in Gambas so the apps can talk to each other?...
I think its better to keep things simple, so long as the solution meets the requirements.

For example, lets suppose App1 reads a barcode and needs to transfer it to App2, and that we are happy as long as this takes less than (say) 500ms.
One simple solution might be for App1 to write the barcode as text into a file called CurrentCode.txt
The App2 program would then need to check for the presence of the CurrentCode.txt file periodically (at least <500ms). If/when it finds the file, it would extract the barcode string and delete the file. This is very simple to understand, test & debug.

I hope this helps.

I have a sockets example, but it may not be directly relevant to your needs: http://captainbodgit.blogspot.com/2016/ ... ambas.html
I have tried using text files before but they are not very reliable and with some of the information that would be shared between the apps I
would have to encrypted it.

I have had a look at your example and for what I need that looks like it will be perfect (sending printer commands to the printer app etc)

AndyGable
Posts: 69
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: A Gambas Newbie questions

Post by AndyGable » Saturday 05th December 2020 2:19pm

stevedee wrote:
Saturday 05th December 2020 8:47am
AndyGable wrote:
Friday 04th December 2020 11:41pm
...is it possible to make a Screenless application in Gambas? I ask as I have a lot of Point of sale Printers that I support and I was thinking about creating a "driver" app for each printer. ...
Yes, when you start Gambas and create a new Project Type, the first option is: Command-line application

You basically work with a Public Sub Main() rather than a Public Sub Form_Open() routine.

Here is a simple example:-
' Gambas module file

hTimer As Timer
fTime As Float
 
Public Sub Application_Signal(x As Integer)
 
  Print "Program stopped after " & fTime & " seconds"
  Quit
End
  
Public Sub Main()
  
  hTimer = New Timer As "IntTimer"
  Print "Press [Ctrl] + " & Chr(92) & " to stop"  '<ctrl><\>
  Signal[Signal.SIGQUIT].Catch
  hTimer.Delay = 500
  hTimer.Start
End
  
Public Sub IntTimer_Timer()
 
  Print Rand(0, 100)
  fTime += 0.5
End
You may also be interested in my old post: https://captainbodgit.blogspot.com/2017 ... mming.html

You can even do Gambas command line programming without using the IDE, by using Gambas Script: http://captainbodgit.blogspot.com/2019/ ... cript.html
Great thank you

does that mean I can start the program with the ./ProgamName & command?

User avatar
stevedee
Posts: 431
Joined: Monday 20th March 2017 6:06pm

Re: A Gambas Newbie questions

Post by stevedee » Saturday 05th December 2020 2:48pm

AndyGable wrote:
Saturday 05th December 2020 2:19pm
...does that mean I can start the program with the ./ProgamName & command?
Well, for my example I could use:-

./cliSignal.gambas

...if terminal is running in the directory that holds the file.

What is: & command?

AndyGable
Posts: 69
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: A Gambas Newbie questions

Post by AndyGable » Sunday 06th December 2020 4:33am

According to google the & command will start the program but will not make it take focus (so basically it will load the program into the background)

What I would like to do is start my application and have it then load other apps as it needs then
for example it would show on the screen


Welcome to PoS
Starting Epson Printer App...... (and then it would start the epson driver application)
or

Starting IBM Printer App...... (and then it would start the IBM driver application as IBM use different commands for thier printers)

The idea is the drivers would use the same commands from the main software (PoS) but will send to the printers the required commands for that given hardware

User avatar
BruceSteers
Posts: 478
Joined: Thursday 23rd July 2020 5:20pm

Re: A Gambas Newbie questions

Post by BruceSteers » Sunday 06th December 2020 5:42am

AndyGable wrote:
Friday 04th December 2020 11:41pm
If it is possible can i use Socket in Gambas so the apps can talk to each other?
A Pipe would be better and simpler.
https://gambaswiki.org/wiki/lang/pipe

But yes a socket or pipe is a way to make apps communicate to each other.

Here i posted a simple pipe example..
https://forum.gambas.one/viewtopic.php?f=4&t=924
once the app is open you can type ...
echo "hi there" >>/tmp/FIFO1
and the app will pick it up.

with a pipe a program can watch a file for input and in your other app you can use echo to send text to the pipe file and the app watching it can react.

Here's a more complicated example that opens a pipe file when it first runs, then any other commands to run the app while it's already open cause the newly run app to send it's args to the pipe and quit, then the first open app gets the args and reacts as it would when first run.
I call it the "single instance" example
https://forum.gambas.one/viewtopic.php?f=13&t=943

AndyGable wrote:
Friday 04th December 2020 11:41pm
Also last question can I convert BASIC code to Gambas?
You can but there are not apps to do it, it does not take long to do it by hand.
It won't take you long to learn the little differences.

Happy gambassing
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply