[Solved] Convert BASIC to Gambas

Post

Posted
Rating:
#1 (In Topic #735)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
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

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
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
^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…

Code (gambas)

  1. Public Sub Form_KeyPress()
  2.  
  3.   Debug Key.Text
  4.   Debug Key.code
  5.   If Key.Text = "^C" Then
  6.     Debug "found ^C"
  7.     Else If Key.Text = "\x03" Then
  8.       Debug "found \\x03"
  9.  
  10.  

Key.Code says Chr(67) but that's just C
I think "\x03" might be your answer here
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Bruce,

I forgot to mention the Scanner is a RS232 device.

This is the unedited viersion from my BASIC application

Code

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
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
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)
/lang/asc - Gambas Documentation

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
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 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)
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’

AndyGable said

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.
Online now: No Back to the top

Post

Posted
Rating:
#8
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
So is there away to only accept numbers from the scanner?
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
I use this to eliminate what I do not want for a keypress:

Code (gambas)

  1. 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
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’

AndyGable said

So is there away to only accept numbers from the scanner?

Try this instead…

Code (gambas)

  1. For x As Integer = 0 To Len(a) - 1
  2.     If Asc(a[x]) < 48 Then Continue
  3.  
  4.     If Asc(a[x]) > 57 Then Continue
  5.     datareceived += result
  6.  

(A guardian angel watching over us gave me that code :) )
Online now: No Back to the top

Post

Posted
Rating:
#11
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone

I have sorted out the first part of my scanner / scale module the barcode number is being sent perfecvtly everytime now

What I did was this

Code

Sleep 0.025
        Try Read #RS232Scanner, Global.ScannerData, Lof(RS232Scanner)
    
    If Error Then
        Print "Error With Incoming Serial Data"
    Endif
    
    If Len(Global.ScannerData) > 2 Then
        Print "Raw Barcode Number : " & CStr(Global.ScannerData)
        CleanUpBarcodeData(CStr(Global.ScannerData))
    Endif
End
 

and then to send the barcode number to the PoS terminal i used

Code

Private Sub CleanUpBarcodeData(BarcodeNumber As String)
    Dim TempNumber As String     

    If Global.Scanner_RemoveChrs > 0 Then
        TempNumber = Mid$(BarcodeNumber, Global.Scanner_RemoveChrs, Len(BarcodeNumber))
    End If

    
    Print "Processed barcode Number : " & Trim(TempNumber)
    
    Global.AddToListBox("Barcode Number sent to PoS :" & TempNumber)
End

And this works every time :) I just need to work out how to send the request to the Scanner to send the weight back
Online now: No Back to the top
1 guest and 0 members have just viewed this.