[Solved] Convert BASIC to Gambas
Posted
#1
(In Topic #735)
Enthusiast

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
Posted
Guru

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…
Key.Code says Chr(67) but that's just C
I think "\x03" might be your answer here
Posted
Enthusiast

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
Posted
Guru

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
Posted
Enthusiast

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.
Posted
Enthusiast

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)
Posted
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
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.
Posted
Enthusiast

Posted
Enthusiast

Posted
Guru

Posted
Enthusiast

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
1 guest and 0 members have just viewed this.



