[Solved] Convert BASIC to Gambas

Post your Gambas programming questions here.
AndyGable
Posts: 349
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

[Solved] Convert BASIC to Gambas

Post by AndyGable »

Hi Everyone

I was wondering if Someone could spot where I have gone wrong

what I want to do is convert a small BASIC code snip to Gambas (as this worked perfectly for handling barcode scanner input on my old FreeBASIC version of my Point of sale system)

This is the Code that I have come up with so far

Code: Select all

Dim ScannerPrefix As String = "^C"
Dim Buffer As String = Null

Do While Lof(RS232Scanner) > 0 
      Buffer = ""
      
      buffer = Read #RS232Scanner, 1

      If buffer <> Chr(Val(ScannerPrefix)) Then 
         Dim a As String = buffer
            Dim result As String
               For x As Integer = 0 To Len(a) - 1
                  If InStr(Chr(a[x]), any "0123456789" Or Chr(Val(ScannerPrefix))) Then
                     result += Chr(a[x])
                  End If 
               Next x
            datareceived += result
      End If

      If buffer = Chr(Val(ScannerPrefix)) Then      'tells NPOS the scanner is done
        If RemoveNumber > 0 Then
            Dim Temp As String = Mid(datareceived, RemoveNumber, Len(datareceived))
                datareceived = ""
                datareceived = Temp
	      End If
            ScannerFucntionSub(datareceived)
            datareceived = ""      
      End If
   Loop
At the moment i am getting a Error on the line that holds InStr(

I thought this was going to something easy to convert but I have been trying to work this out for 2 hours now any help would be most welcomed
Last edited by AndyGable on Saturday 4th September 2021 8:03pm, edited 1 time in total.
User avatar
BruceSteers
Posts: 1505
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Convert BASIC to Gambas

Post by BruceSteers »

^C I think means Ctrl-C
I don't think you can look for the String "^C" as the ^ char works differently

I have a Function that checks a KeyPress for Ctrl-C

If you Print Key.Text it prints "^C" but...
If Key.Text = "^C" does not work

If Key.Text = "\x03" does work though

try something like this
Dim ScannerPrefix As String = "\x03"

if buffer <> "\x03"
should check for Ctrl-C

Try this in a test Form and press Ctrl-C to see what i mean...
Public Sub Form_KeyPress()

  Debug Key.Text
  Debug Key.code
  If Key.Text = "^C" Then 
    Debug "found ^C"
    Else If Key.Text = "\x03" Then 
      Debug "found \\x03"
  Endif
End

Key.Code says Chr(67) but that's just C
I think "\x03" might be your answer here
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 349
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Convert BASIC to Gambas

Post by AndyGable »

Hi Bruce,

I forgot to mention the Scanner is a RS232 device.

This is the unedited viersion from my BASIC application

Code: Select all

Private Sub SelectSerialScanner
   Do While LOC(BarcodeReader) > 0 
      Buffer = ""
	      
      buffer = Input(1,BarcodeReader)
      	      
      If buffer <> Chr(Val(ScannerPrefix)) Then 
         dim a as string = buffer
            dim result as string
               For x as integer = 0 to len(a) -1
                  If instr(chr(a[x]),any "0123456789" & Chr(Val(ScannerPrefix))) then
                     result += chr(a[x])
                  End If 
               Next x
	         datareceived += result
      End If
	
		'If DebugMode = 1 Then
 			Open Cons For Output As #DebugConsole
				Print #DebugConsole, "Data From Scanner"
				Print #DebugConsole, datareceived
			Close #DebugConsole
		'End If
	
      If buffer = Chr(Val(ScannerPrefix)) Then      'tells NPOS the scanner is done
	      If RemoveNumber > 0 then
				Dim Temp as string = mid(datareceived, RemoveNumber, len(datareceived))
					datareceived = ""
					datareceived = Temp
	      End If
			ScannerFucntionSub(datareceived)
			datareceived = ""	      
      End If
   Loop 		
End Sub
User avatar
BruceSteers
Posts: 1505
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Convert BASIC to Gambas

Post by BruceSteers »

Well BASIC is BASIC , Gambas is Gambas

Chr(Val(ScannerPrefix))
That's not right for starters

Val is for converting string data like "1.02" into Values not finding the ascii value of text.

Try Asc(ScannerPrefix)
http://gambaswiki.org/wiki/lang/asc

It's possible BASIC interprets "^C" as Ctrl-C ascii value, gambas does not as i explained.

you problem is getting that String / char "^C" recognised.

Like i said if the console reports the key is "^C" then it's probably "\x03" (Ctrl-C) and using the string "^C" as a search parameter will not work.

I think it's..
If buffer <> "\x03" Then

Or maybe it has a Newline?
If RTrim(buffer) <> "\x03" Then
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 349
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Convert BASIC to Gambas

Post by AndyGable »

I have no idea what it is sending but on the console of the app I was working on it shows as a ^C on the screen (so i was trying to trap that)

I am in the process of finding the user manual for the scanner (I have it some where in my office) so I can re program it to send a chr(13) to the terminal.
AndyGable
Posts: 349
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Convert BASIC to Gambas

Post by AndyGable »

Well I have found the user manual and I have updated the suffix to be a TAB but I am still getting the ^C char show up with in the Gambas app

Is this Gambas it self getting the data from the Serial port as in the FeeBASIC version it works perfectly every time with the code I posted before.

I think i am getting confused lol.

I have also been sent a very small C program that takes the data from the Scanner and insets it into a file (i do not need that I want to send it via a pipe) but how could I convert this to Gambas (I have never done any C programming in my life)
User avatar
BruceSteers
Posts: 1505
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Convert BASIC to Gambas

Post by BruceSteers »

AndyGable wrote: Wednesday 1st September 2021 6:04pm Well I have found the user manual and I have updated the suffix to be a TAB but I am still getting the ^C char show up with in the Gambas app

Is this Gambas it self getting the data from the Serial port as in the FeeBASIC version it works perfectly every time with the code I posted before.

I think i am getting confused lol.

I have also been sent a very small C program that takes the data from the Scanner and insets it into a file (i do not need that I want to send it via a pipe) but how could I convert this to Gambas (I have never done any C programming in my life)
To convert from C to gambas basic you need to lean C ,,, and gambas basic ;) lol

hehe , but more seriously, don't run before you can walk fella, there's a lot to wrap your head around with gambas before you can look at most code excerpts and just write it in gambas.

you know I help with gambas developement ,
i keep making changes / additions and submitting the requests to Benoit and 9 times out of 10 all i do is show how little i know about gambas and much i have to learn.
But it's cool , i just accept it and smile, it is after all, true :)

Such is life eh , keep at it , keep reading the wiki , look for the "See Also" links to see other commands , the fact you were using Val() and not Asc() to get an ascii value of a string (and then use Chr() to just turn it back into a string) shows you're at the copy-n-paste and hope it works kind of level.
If only life were that simple :)

Sounds to me like all you need is to read the wiki a bit more (you should have found the Asc() function for string conversion, it's the first command listed in String Functions)

Click on the things you do not understand and also click the things you think you do as you may find gambas does things differently.
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 349
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Convert BASIC to Gambas

Post by AndyGable »

So is there away to only accept numbers from the scanner?
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: Convert BASIC to Gambas

Post by grayghost4 »

I use this to eliminate what I do not want for a keypress:
If Not IsDigit(Key.Text) And Key.Code <> Key.BackSpace And Key.Code <> Key.BackTab And Key.tab <> Key.code And Key.code <> Key.Left And Key.code <> Key.Right Then Stop Event
User avatar
BruceSteers
Posts: 1505
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Convert BASIC to Gambas

Post by BruceSteers »

AndyGable wrote: Wednesday 1st September 2021 8:09pm So is there away to only accept numbers from the scanner?
Try this instead...
For x As Integer = 0 To Len(a) - 1
    If Asc(a[x]) < 48 Then Continue

    If Asc(a[x]) > 57 Then Continue
    datareceived += result
Next x
(A guardian angel watching over us gave me that code :) )
If at first you don't succeed , try doing something differently.
BruceS
Post Reply