Serial Communication not working

For issues with communication ports, protocols, etc.
Gablea
Posts: 850
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Serial Communication not working

Postby Gablea » Jan 26, 2017 17:59

Hi everyone,
I have slight problem I can not seem to get any inforamtion from my serial scanner (outputs data in ascii)

This is the code I use to establish a connection to the scanner

Code: Select all

Public Sub EstablishScannerConnection
   BarcodeReader = 3
   Dim ComSettings_Scanner As String = "COM " & ScannerPort & ",cs0,ds0,cd0,rs"
   
   If DebugMode = 1 Then
       Open Cons For Output As #DebugConsole
         Print #DebugConsole, "Serial Settingas for Scanner"
         Print #DebugConsole, ComSettings_Scanner
      Close #DebugConsole
   End If

   Dim r1                            As Integer = Open(ComSettings_Scanner For Random As #BarcodeReader)

   If r1 <> 0 Then
       If DebugMode = 1 Then
          Open Cons For Output As #DebugConsole
            Print #DebugConsole, "Serial Setting for Scanner"
            Print #DebugConsole, "Error opening serial link "; r1
         Close #DebugConsole
      End If
   Else
       If DebugMode = 1 Then
          Open Cons For Output As #DebugConsole
            Print #DebugConsole, "Serial Setting for Scanner"
            Print #DebugConsole, "Serial Scanner opened with no problems"
         Close #DebugConsole
      End If
   End If
End Sub


I get in the debug window

COM COM3:9600,N,8,1,cs0,ds0,cd0,rs
Serial Setting for Scanner
Serial Scanner opened with no problems


and this is the code I have come up with to get the data from the Serial scanner

Code: Select all

Private Sub SelectSerialScanner
   
   If LOC(BarcodeReader) = 0 Then
      Open Cons For Output As #DebugConsole
         Print #DebugConsole, "Data From Scanner"
         Print #DebugConsole, "barcode Port number : "; BarcodeReader
         Print #DebugConsole, "No Data Coming in from scanner"
      Close #DebugConsole
   Else
      Open Cons For Output As #DebugConsole
         Print #DebugConsole, "Data From Scanner"
         Print #DebugConsole, "barcode Port number : "; BarcodeReader
         Print #DebugConsole, "Receving data from scanner"
      Close #DebugConsole
   End If
   
   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

Private Sub ScannerFucntionSub(ByVal BarcodeNumber As String)
   datareceived = ""
          Buffer = ""
          
   Select Case ScannerFucntion
      Case "SaleScreen"
         SubTotalPressed = 0
         FindProduct(BarcodeNumber)
               
      Case "Salescreen_PriceCheck"
         FindProduct_PriceCheck(BarcodeNumber, "SaleScreen")
            
      Case "SignedOff_PriceCheck"
         FindProduct_PriceCheck(BarcodeNumber, "SignedOff_PriceCheck")
            
      Case "GiftCard"

   End Select
end Sub


But I get nothing the debug window reports nothing is being sent so what have i done wrong?
MrSwiss
Posts: 2022
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Serial Communication not working

Postby MrSwiss » Jan 27, 2017 11:01

Gablea wrote:But I get nothing the debug window reports nothing is being sent so what have i done wrong?
Barcode Scanners are "funny animals", as far as it concerns Serial Comms: think of them, being
similar to the Keyboard. The read Code is transformed (as defined by the Scanners Setup) and
straight away sent to the PC. This means: the currently "active" Program receives it.

Simple way to check a Scanner: open Notepad, scan something, currently open file receives the
Data, sent by the Scanner. (You'll never have to query it! But the Program running, should at all
times be able, to receive ... whatever the Scanner is sending!)

A Barcode Scanner could be called: a push Device.
Unlike other Serial Devices, that have to be pulled (even if it's from a Buffer).
Gablea
Posts: 850
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Serial Communication not working

Postby Gablea » Jan 27, 2017 17:01

My barcode readers are RS232 devices and will ONLY work if they are polled like the printers.

I have set the comport settings right on the machine as I can get data from it in the Windows Hyper terminal.

IS my code right? I would love to move it into a separate threat so the scanner is always being poled but I am not sure as how i do that

I have access to a small program that would pole the scanner and output the data into text files but I would need to modify it so I could
tell it where to place the files (problem is it is in C and I would not have a clue as how to do that)

BUT I would rather deal with the scanner directly inside my program. This use to work but recently it has stop working and before you all say COM is broken I have a Serial Printer and that works fine.

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: No registered users and 1 guest