how to check a string for a given character

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

how to check a string for a given character

Post by AndyGable » Tuesday 17th August 2021 3:01pm

Hi Everyone,

Is it possible to check a string for a given character (example ^C) as this tells my software to do something

I am at the moment using

Code: Select all

if Loz = "^C" then
but sometimes the device i am connected to send the full data like

Code: Select all

b05018374320766^C
I am trying to get data from a Serial Barcode reader and 99% of the time it works (but for some reason I am getting data in 2 passes not sure why)

This is my code I am using to get serial data from the scanner if anyone out there can see where i have gone wrong I would most appreciate being told so i know for the future

Code: Select all

' Gambas class file

Public RS232Scanner As SerialPort
Public TTimer As Timer

Public ScannerData As String

Public Sub Form_Open()

    If Args.Count > 1 Then 
        Select Case Lower(Args[1])
            Case "/setup", "/Setup"
                
                
            Case "/hiden", "Hiden"
                FMain.FullScreen = False
                FMain.Visible = False

            Case ""
                FMain.Center
                FMain.Resizable = False

        End Select
    End If
    
    
    
    
    RS232Scanner = New SerialPort As "RS232Scanner"
    TTimer = New Timer As "TTimer"
    TTimer.Delay = 500

    Global.SystemBootUp
    
    'Set up the Serial Device so it should start talking
    With RS232Scanner
        .PortName = Global.Scanner_SerialPortName
        .Speed = Global.Scanner_SerialBandRate
        
    Select Case Global.Scanner_SerialDataParity
         Case "none", "None"
             .Parity = SerialPort.None
             
         Case "odd", "Odd"   
             .Parity = SerialPort.Odd
             
         Case "even", "Even"
             .Parity = SerialPort.Even
     End Select
     
     Select Case Global.Scanner_SerialDataBits
         Case "5"
             .DataBits = SerialPort.Bits5
             
         Case "6"
             .DataBits = SerialPort.Bits6

         Case "7"
             .DataBits = SerialPort.bits7
             
         Case "8"
             .DataBits = SerialPort.bits8
     End Select
         
     Select Case Global.Scanner_SerialStopBits
         Case "1"
             .StopBits = SerialPort.bits1
             
         Case "2"
             .StopBits = SerialPort.Bits2

     End Select
         
     Select Case Global.Scanner_SerialFlowControl
         Case "none", "None"
             .FlowControl = SerialPort.None
         
         Case "hardware", "Hardware"
             .FlowControl = SerialPort.hardware
         
         Case "software", "Software"
             .FlowControl = SerialPort.Software
         
         Case "both", "Both"
             .FlowControl = SerialPort.both
     End Select
    End With 
    
    Try RS232Scanner.Open(3)
        If Error Then 
            Global.AddToListBox("Sorry Unable to connect to Scanner\nPlease check settings and try again")    
        Else 
            If RS232Scanner.Status = Net.Active Then 
                TTimer.Start()
                Global.AddToListBox("     DTR :" & RS232Scanner.DTR)
                Global.AddToListBox("     DSR :" & RS232Scanner.DSR)
                Global.AddToListBox("Blocking :" & RS232Scanner.Blocking)
                Global.AddToListBox("     CTS :" & RS232Scanner.CTS)
                Global.AddToListBox("     RTS :" & RS232Scanner.RTS)
            End If
        End If
End

Private Sub Form_Close()    
    
    If RS232Scanner.Status = Net.Active Then 
        RS232Scanner.Close
        FMain.Close
    End If
End

Public Sub RS232Scanner_Read()
    Dim i As Integer = 0

    Dim Loz As String = Read #RS232Scanner, Lof(RS232Scanner)

    Print Loz
    
    If Loz = "^C" Then
        ScannerData = ""
    Else
        ScannerData &= Loz
    Endif
End

Public Sub TTimer_Timer()
    Dim TempStore As String = ScannerData

    ScannerData = Mid$(TempStore, 4, Len(TempStore))


    If FMain.ListBox1.Count = 13 Then FMain.ListBox1.Clear
    
    If RS232Scanner.Status = Net.Active Then
        If ScannerData <> "" Then
            Global.AddToListBox("Barcode Number from Scanner :" & ScannerData)
            ScannerData = ""
        End If
    Endif
End

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

Re: how to check a string for a given character

Post by cogier » Tuesday 17th August 2021 3:19pm

You can use InStr to find the characters: -
If InStr(Loz, "^C") Then ScannerData = "" Else ScannerData &= Loz

Have you thought of getting a USB Barcode gun, they are not expensive? These guns just give you text and a [CR] (NewLine) so you only have to check for that to know you have all that was read.

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

Re: how to check a string for a given character

Post by AndyGable » Tuesday 17th August 2021 4:08pm

cogier wrote:
Tuesday 17th August 2021 3:19pm
You can use InStr to find the characters: -
If InStr(Loz, "^C") Then ScannerData = "" Else ScannerData &= Loz

Have you thought of getting a USB Barcode gun, they are not expensive? These guns just give you text and a [CR] (NewLine) so you only have to check for that to know you have all that was read.
Would love to use a simple usb scanner cogier but my customers normally own supermarkets so i have to use the high volume scanners as well as having integrated scales (that is the next challenge working out how to send the commands to the scanner and then get the weight back) I know the commands to send but I am not sure how to pool the weight


what is annoying if I use Hyperterminal the scanner sends the barcode in one line but when i scan it on my app it appers in the console like

^B0850542
67009264
^C

am I doing something wrong?

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

Re: how to check a string for a given character

Post by cogier » Tuesday 17th August 2021 5:08pm

Try running this: -
Public Sub Form_Open()

  Dim sText As String = "^B0850542\n67009264\n^C"

  Print sText

  sText = Replace(sText, "\n", "")

  Print sText

End
I suspect there are line feeds of some sort in the mix. If this does not help, can you attach a saved copy of the text, so we can break down each character to find out what is there.

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

Re: how to check a string for a given character

Post by AndyGable » Friday 20th August 2021 2:03pm

I found this code that I was using in my DOS BASIC program

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
DO you think It could be converted to work in Gambas?

Post Reply