Barcode read?

Just starting out? Need help? Post your questions and find answers here.
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Barcode read?

Post by stmdbe2019 »

1. I have Rugged tablet which has a barcode reader connected. The port is COM3.
Image

2. When i do following test it works to connect on COM3:

Code: Select all

  If OpenSerialPort(0, "COM3", 300, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)
    Debug "Success - But how do i read any packets here???????????? to keep it simple reader??????????"
  Else
    Debug "Failed"
  EndIf
3. To read barcode i tried two sample code but both failing to read any packets

Image

4. Failed samples

Fail 1:

Code: Select all

; English forum: http://www.purebasic.fr/english/viewtopic.php?t=7427
; Author: clipper
; Date: 03. September 2003
; OS: Windows
; Demo: No


; GPS How to receive NMEA Data over a serial-port with MVCom
; ----------------------------------------------------------
; Simple Example to receive NMEA-Sentences over Com1 with PureBasic and the
; MVCom Library of Marc Vitry http://www.purearea.net/pb/download/userlibs/MVCOM_LIBRARY.zip
; by Thomas Ott - tom@medi-a.de
; Information about the NMEA-Protocoll http://www.nmea.de/nmea0183datensaetze.html


#FRAME_1 = 0
#TEXT_1 = 1
#TEXT_2 = 2
#FRAME_2 = 3
#BUTTON_1 = 4
#BUTTON_2 = 5
#LISTVIEW_1=6
#Window_0=0

Global HCom.l
Global NMEA.s
ComPortSettings.s="COM3:4800,N,8,1"

Procedure TimerProc(WindowID.l, nIDEvent.l, uElapse.l, lpTimerFunc.l)
  If HCom > 0
    Car.s = "."
    While ComInputBufferCount(HCom) > 0
      If ComInput(HCom,Car)
        If Car=Chr(10)
          AddGadgetItem(#LISTVIEW_1, -1,NMEA)
          If Left(NMEA,6)= "$GPGLL"
            SetGadgetText(#TEXT_1,StringField(NMEA,2,",")+StringField(NMEA,3,","))
            SetGadgetText(#TEXT_2,StringField(NMEA,4,",")+StringField(NMEA,5,","))
          EndIf
          NMEA=""
        Else
          NMEA=NMEA+Car
        EndIf
      EndIf
    Wend
  EndIf
EndProcedure

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 435, 369, 300,130, "NMEA",#PB_Window_SystemMenu | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      Frame3DGadget(#FRAME_1, 0, 0, 100, 20, "", #PB_Frame3D_Single)
      TextGadget(#TEXT_1, 3, 3, 90, 15, "", #PB_Text_Center)
      TextGadget(#TEXT_2, 103, 3, 90, 15, "", #PB_Text_Center)
      Frame3DGadget(#FRAME_2, 101, 0, 100, 20, "", #PB_Frame3D_Single)
      ButtonGadget(#BUTTON_1, 201, 0, 50, 20, "Start")
      ButtonGadget(#BUTTON_2, 250, 0, 50, 20, "Clear")
      ListViewGadget(#LISTVIEW_1, 0, 20, 300, 110)
      
    EndIf
  EndIf
EndProcedure

Open_Window_0()
SetTimer_(WindowID(#Window_0),1,100,@TimerProc())
Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_Gadget
    GadgetID = EventGadget()
    Select GadgetID
    Case #BUTTON_1
      If HCom > 0
        ComClose(HCom)
        HCom=0
        SetGadgetText(#BUTTON_1,"Start")
      Else
        HCom=ComOpen(ComPortSettings,0,256,0)
        If HCom=-1
          MessageRequester("NMEA","Error opening the ComPort",0)
        Else
          SetGadgetText(#BUTTON_1,"Stop")
        EndIf
      EndIf
    Case #BUTTON_2
      ClearGadgetItemList(#LISTVIEW_1)
    EndSelect
  EndIf
Until Event = #PB_Event_CloseWindow

KillTimer_(WindowID(#Window_0),1)
If HCom > 0
  ComClose(HCom)
EndIf
End

; IDE Options = PureBasic v4.00 (Windows - x86)
; Folding = -
; EnableXP
Fail 2:

Code: Select all

#Version = "1.01"

Enumeration
  #Output1
  #Output2
  #Output3
  #Logging
  #Button
  #Time
EndEnumeration

Procedure Logging(T1$, T2$, T3$)
  
  Protected File.i, Line$
  
  File = OpenFile(#PB_Any, ReplaceString(ProgramFilename(), ".exe", ".csv"))
  If File
    FileSeek(File, Lof(File))
    Line$ = #DQUOTE$ + FormatDate("%yyyy%mm%dd %yy%ii%ss", Date()) + #DQUOTE$ + ";"
    Line$ + #DQUOTE$ + T1$ + #DQUOTE$ + ";"
    Line$ + #DQUOTE$ + T2$ + #DQUOTE$ + ";"
    Line$ + #DQUOTE$ + T3$ + #DQUOTE$
    WriteStringN(File, Line$)
  EndIf
  
EndProcedure


Com = OpenSerialPort(#PB_Any, "COM3", 1200, #PB_SerialPort_NoParity, 7, 2, #PB_SerialPort_NoHandshake, 64, 64)
If Not Com
  MessageRequester("Error", "Can not open COM port!")
  End
EndIf

OpenWindow(0, 0, 0, 190, 100, "MeterReader V" + #Version, #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TextGadget(#Output1, 10, 10, 40, 20, "", #PB_Text_Border|#PB_Text_Center)
TextGadget(#Output2, 60, 10, 60, 20, "", #PB_Text_Border|#PB_Text_Center)
TextGadget(#Output3, 130, 10, 40, 20, "", #PB_Text_Border|#PB_Text_Center)
CheckBoxGadget(#Logging, 120, 50, 60, 20, "Logging")
ButtonGadget(#Button, 10, 50, 60, 20, "Start")
SpinGadget(#Time, 80, 50, 30, 20, 1, 60, #PB_Spin_Numeric)
SetGadgetState(#Time, 1)

CreateStatusBar(0, WindowID(0))
AddStatusBarField(50)

Buffer$ = Space(14)

Exit = #False
Repeat
  Event = WaitWindowEvent(100)
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
        WriteSerialPortString(Com, "D")
        StatusBarText(0, 0, "read", #PB_StatusBar_Center)
        If FirstTime
          RemoveWindowTimer(0, 1)
          AddWindowTimer(0, 1, GetGadgetState(#Time) * 1000)
          FirstTime = #False
        EndIf
      EndIf
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Button
          If GetGadgetText(#Button) = "Start"
            SetGadgetText(#Button, "Stop")
            DisableGadget(#Time, #True)
            SetGadgetText(#Output1, "")
            SetGadgetText(#Output2, "")
            SetGadgetText(#Output3, "")
            AddWindowTimer(0, 1, 1)
            FirstTime = #True
          Else
            RemoveWindowTimer(0, 1)
            SetGadgetText(#Button, "Start")
            DisableGadget(#Time, #False)
            StatusBarText(0, 0, "")
          EndIf
        Case #Time
      EndSelect
    Case #PB_Event_CloseWindow
      Exit = #True
  EndSelect
  
  If AvailableSerialPortInput(Com) >= 14
    ReadSerialPortData(Com, @Buffer$, 14)
    SetGadgetText(#Output1, Trim(Mid(Buffer$, 1, 3)))
    SetGadgetText(#Output2, Trim(Mid(Buffer$, 4, 6)))
    SetGadgetText(#Output3, Trim(Mid(Buffer$, 10, 4)))
    If GetGadgetState(#Logging)
      Logging(GetGadgetText(#Output1), GetGadgetText(#Output2), GetGadgetText(#Output3))
    EndIf
    StatusBarText(0, 0, "")
  EndIf
  
Until Exit

CloseSerialPort(Com)
CloseWindow(0)
-----
Registered PureBasic Coder.
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: Barcode read?

Post by Lord »

shamun wrote:...
Fail 1:

Code: Select all

; English forum: http://www.purebasic.fr/english/viewtopic.php?t=7427
; Author: clipper
; Date: 03. September 2003
; OS: Windows
; Demo: No


; GPS How to receive NMEA Data over a serial-port with MVCom
; ----------------------------------------------------------
; Simple Example to receive NMEA-Sentences over Com1 with PureBasic and the
; MVCom Library of Marc Vitry http://www.purearea.net/pb/download/userlibs/MVCOM_LIBRARY.zip
; by Thomas Ott - tom@medi-a.de
; Information about the NMEA-Protocoll http://www.nmea.de/nmea0183datensaetze.html

...
...
Did you pay attention to this comment:
The mentioned lib is needed.
Image
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: Barcode read?

Post by stmdbe2019 »

YES - still i do not get feedback from it. I tried Google chrome and there i have same issue:
Can anyone tell me what is going on? why i do not get any single bit/bytes ?

Image
-----
Registered PureBasic Coder.
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Barcode read?

Post by mk-soft »

Without external libs...

The comport is processed in the thread and therefore always runs in the background.
If new data (string) arrives completely, the ReceiveCB is called. The recognition that the string is complete is defined by an end character.
The comport and the end character are defined in the procedure InitComport
\EndOfText = #CRLF$ ; #LF$ , #CR$, etc

Code: Select all

;-TOP

; Comment : Comport Manager Over Thread and Callback
; Author  : mk-soft
; Version : v0.05
; Created : 26.01.2018
; Updated : 03.04.2018

; *****************************************************************************

CompilerIf #PB_Compiler_Thread = 0
  CompilerError "Use Option Threadsafe!"
CompilerEndIf
  
Prototype ProtoReceiveCB(Text.s)
Prototype ProtoStatusCB(Status, *ComData)

Enumeration
  #ComStatus_Nothing
  #ComStatus_OpenPort
  #ComStatus_ClosePort
  #ComStatus_ErrorOpenPort
  #ComStatus_ErrorSend
  #ComStatus_ErrorReceive
  #ComStatus_ErrorDataSize
EndEnumeration

Structure udtComData
  ; Header
  ThreadID.i
  Exit.i
  Status.i
  ; Port Data
  ComID.i
  Port.s
  Baud.i
  Parity.i
  DataBit.i
  StopBit.i
  Handshake.i
  BufferSize.i
  ; End Of Text
  EndOfText.s
  ; Send Data
  SendSignal.i
  SendCount.i
  SendText.s
  SendError.i
  ; Receive data
  ReceiveCount.i
  ReceiveText.s
  ReceiveError.i
  ; Callback
  *StatusCB.ProtoStatusCB
  *ReceiveCB.ProtoReceiveCB
EndStructure

Procedure thComport(*ComData.udtComData)
  
  Protected *Send, *Receive, SendText.s, SendLen, ReceiveText.s, ReceiveLen, Pos
  
  With *ComData
    ; Startup
    \Status = 1
    \SendCount = 0
    \ReceiveCount = 0
    \ComID = OpenSerialPort(#PB_Any, \Port, \Baud, \Parity, \DataBit, \StopBit, \Handshake, \BufferSize, \BufferSize)
    If \ComID
      \Status = 2
    Else
      If \StatusCB
        \StatusCB(#ComStatus_ErrorOpenPort, *ComData)
      EndIf
      \Status = 0
      ProcedureReturn 0
    EndIf
    If \StatusCB
      \StatusCB(#ComStatus_OpenPort, *ComData)
    EndIf
    *Send = AllocateMemory(\BufferSize)
    *Receive = AllocateMemory(\BufferSize)
    ; Loop
    Repeat
      If \SendSignal
        SendText = \SendText + \EndOfText
        SendLen = StringByteLength(SendText, #PB_Ascii)
        If SendLen <= \BufferSize
          PokeS(*Send, SendText, SendLen, #PB_Ascii)
          If WriteSerialPortData(\ComID, *Send, SendLen) = 0
            \SendError = SerialPortError(\ComID)
            If \StatusCB
              \StatusCB(#ComStatus_ErrorSend, *ComData)
            EndIf
          Else
            \SendError = 0
            \SendCount + 1
          EndIf
        Else
          If \StatusCB
            \StatusCB(#ComStatus_ErrorDataSize, *ComData)
          EndIf
        EndIf
        \SendSignal = 0
      EndIf
      ReceiveLen = AvailableSerialPortInput(\ComID)
      If ReceiveLen
        ReceiveLen = ReadSerialPortData(\ComID, *Receive, ReceiveLen)
        If ReceiveLen = 0
          \ReceiveError = SerialPortError(\ComID)
          If \StatusCB
            \StatusCB(#ComStatus_ErrorReceive, *ComData)
          EndIf
        Else
          \ReceiveError = 0
        EndIf
        ReceiveText + PeekS(*Receive, ReceiveLen, #PB_Ascii)
        Repeat
          pos = FindString(ReceiveText, \EndOfText, 1, #PB_String_NoCase)
          If pos
            \ReceiveText = Left(ReceiveText, pos - 1)
            ReceiveText = Mid(ReceiveText, pos + Len(\EndOfText))
            \ReceiveCount + 1
            If \ReceiveCB
              \ReceiveCB(\ReceiveText)
            EndIf
          EndIf
        Until pos = 0
      EndIf
      Delay(10)
    Until \Exit
    ; Shutdown
    CloseSerialPort(\ComID)
    If \StatusCB
      \StatusCB(#ComStatus_ClosePort, *ComData)
    EndIf
    FreeMemory(*Send)
    FreeMemory(*Receive)
    \Status = 0
    \ComID = 0
    \Exit = 0
    ProcedureReturn 1
  EndWith
  
EndProcedure

; *****************************************************************************

; Threaded String Helper
Procedure AllocateString(String.s)
  Protected *mem
  *mem = AllocateMemory(StringByteLength(String) + SizeOf(Character))
  If *mem
    PokeS(*mem, String)
  EndIf
  ProcedureReturn *mem
EndProcedure

Procedure.s FreeString(*Mem)
  Protected result.s
  If *Mem
    result = PeekS(*Mem)
    FreeMemory(*Mem)
  EndIf
  ProcedureReturn result
EndProcedure

; *****************************************************************************

CompilerIf #PB_Compiler_IsMainFile
  
  Global ComData.udtComData
  
  Enumeration EventCustomValue #PB_Event_FirstCustomValue
    #My_Event_NewData
    #My_Event_NewState
  EndEnumeration
  
  ; ---------------------------------------------------------------------------
  
  Procedure ReceiveCB(Text.s)
    PostEvent(#My_Event_NewData, 0, 0, 0, AllocateString(Text))
  EndProcedure
  
  Procedure MyEventNewDataCB()
    Protected Text.s
    Text = FreeString(EventData())
    AddGadgetItem(0, -1, Text)
    SetGadgetState(0, CountGadgetItems(0) - 1)
    SetGadgetState(0, -1)
  EndProcedure
  
  BindEvent(#My_Event_NewData, @MyEventNewDataCB())
  
  ; ---------------------------------------------------------------------------
  
  Procedure StatusCB(Status, *ComData.udtComData)
    PostEvent(#My_Event_NewState, 0, 0, Status, *ComData)
  EndProcedure
  
  Procedure MyEventNewStateCB()
    Protected Text.s, Status, *ComData.udtComData
    Status = EventType()
    *ComData = EventData()
    Select Status
      Case #ComStatus_OpenPort
        Text = "ComStatus: Open Port " + *ComData\Port
      Case #ComStatus_ClosePort
        Text = "ComStatus: Close Port " + *ComData\Port
      Case #ComStatus_ErrorOpenPort
        Text = "ComError: Open Port " + *ComData\Port
      Case #ComStatus_ErrorSend
        Text = "ComError Send: Port " + *ComData\Port + " - ErrorCode " + *ComData\SendError
      Case #ComStatus_ErrorReceive
        Text = "ComError Receive: Port " + *ComData\Port + " - ErrorCode " + *ComData\ReceiveError
    EndSelect
    If Bool(Text)
      StatusBarText(0, 0, Text)
    EndIf
  EndProcedure
  
  BindEvent(#My_Event_NewState, @MyEventNewStateCB())
  
  ; ---------------------------------------------------------------------------
  
  Procedure InitComport()
    With ComData
      If \Status
        ProcedureReturn 2 ; Always running
      EndIf
      \Port = "COM1"
      \Baud = 9000
      \Parity = #PB_SerialPort_NoParity
      \DataBit = 8
      \StopBit = 1
      \Handshake = #PB_SerialPort_NoHandshake
      \BufferSize = 2048
      \EndOfText = #CRLF$
      \StatusCB = @StatusCB()
      \ReceiveCB = @ReceiveCB()
      \ThreadID = CreateThread(@thComport(), ComData)
      If Not \ThreadID
        StatusBarText(0, 0, "Comport " + \Port + ": Error Create Thread")
        ProcedureReturn 0 ; Error create thread
      Else
        ProcedureReturn 1 ; ok
      EndIf
    EndWith
  EndProcedure
  
  Procedure Main()
    Protected Event
    If OpenWindow(0, #PB_Ignore, #PB_Ignore, 800, 600, " Test Comport", #PB_Window_SystemMenu)
      CreateStatusBar(0, WindowID(0))
      AddStatusBarField(#PB_Ignore)
      ListViewGadget(0, 0, 0, 800, 540)
      StringGadget(1, 5, 545, 590, 25, "")
      ButtonGadget(2, 605, 545, 90, 25, "Send")
      ButtonGadget(3, 695, 545, 90, 25, "On/Off")
      AddKeyboardShortcut(0, #PB_Shortcut_Return, 1000)
      Repeat
        Event = WaitWindowEvent()
        Select Event
          Case #PB_Event_CloseWindow
            If ComData\Status
              MessageRequester("Info", "Comport Is Open!", #PB_MessageRequester_Warning)
            Else
              Break
            EndIf
          Case #PB_Event_Gadget
            Select EventGadget()
              Case 2
                If ComData\Status = 2 And ComData\SendSignal = 0
                  ComData\SendText = GetGadgetText(1)
                  ComData\SendSignal = 1
                EndIf
              Case 3
                If ComData\Status
                  ComData\Exit = 1
                Else
                  InitComport()
                EndIf
            EndSelect
          Case #PB_Event_Menu
            Select EventMenu()
              Case 1000
                If GetActiveGadget() = 1
                  PostEvent(#PB_Event_Gadget, 0, 2, #PB_EventType_LeftClick)
                EndIf
            EndSelect
        EndSelect
      ForEver
    EndIf
  EndProcedure : Main()
  
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: Barcode read?

Post by stmdbe2019 »

@mk-soft: getting error

Image

// comment out the line fails too
Image
-----
Registered PureBasic Coder.
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Barcode read?

Post by mk-soft »

Yes,

Before starting the code set compiler option thread safe...

Menu -> Compiler -> Compiler Options... -> Create threadsafe executable

P.S.
Its real comport ?. Show device manager on windows 10
Last edited by mk-soft on Fri May 25, 2018 11:29 am, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: Barcode read?

Post by stmdbe2019 »

@mk-soft:
1. Applied changes
2. But still failing to connect at COM3

Image
Image
-----
Registered PureBasic Coder.
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Barcode read?

Post by mk-soft »

Its real comport? Show device manager on windows 10
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

Re: Barcode read?

Post by stmdbe2019 »

it Works!

Image
-----
Registered PureBasic Coder.
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Barcode read?

Post by mk-soft »

Very fine...

don't forget 'EndOfText' is automatic added to SendText...
Perhaps you must change this.

Code: Select all

...
; Loop
    Repeat
      If \SendSignal
        SendText = \SendText + \EndOfText
...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply