Example One - Serial Scan

examples/de2120_ex1_serial_scan.py
 1#!/usr/bin/env python
 2#-----------------------------------------------------------------------------
 3# qde2120_ex1_serial_scan.py
 4#------------------------------------------------------------------------
 5#
 6# Written by Priyanka Makin @ SparkFun Electronics, April 2021
 7#
 8# This example demonstrates how to get the scanner connected and will output
 9# and barcode it sees.
10# 
11# NOTE: you must put the module into COM mode by scanning the PORVIC barcode 
12# in the datasheet. This will put the module in the correct mode to receive 
13# and transmit serial.
14#
15# This package has been developed on a Raspberry Pi 4. Connect the DE2120 Barcode
16# Scanner Breakout directly to your Pi using a USB-C cable
17#  
18# Do you like this library? Help support SparkFun. Buy a board!
19#
20#==================================================================================
21# Copyright (c) 2021 SparkFun Electronics
22#
23# Permission is hereby granted, free of charge, to any person obtaining a copy 
24# of this software and associated documentation files (the "Software"), to deal 
25# in the Software without restriction, including without limitation the rights 
26# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
27# copies of the Software, and to permit persons to whom the Software is 
28# furnished to do so, subject to the following conditions:
29#
30# The above copyright notice and this permission notice shall be included in all 
31# copies or substantial portions of the Software.
32#
33# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
34# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
35# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
36# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
37# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
38# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
39# SOFTWARE.
40#==================================================================================
41# Example 1
42
43from __future__ import print_function
44import de2120_barcode_scanner
45import time
46import sys
47
48def run_example():
49
50    print("\nSparkFun DE2120 Barcode Scanner Breakout Example 1")
51    my_scanner = de2120_barcode_scanner.DE2120BarcodeScanner()
52
53    if my_scanner.begin() == False:
54        print("\nThe Barcode Scanner module isn't connected correctly to the system. Please check wiring", \
55            file=sys.stderr)
56        return
57    print("\nScanner ready!")
58
59    scan_buffer = ""
60    
61    while True:
62        scan_buffer = my_scanner.read_barcode()
63        if scan_buffer:
64            print("\nCode found: " + str(scan_buffer))
65            scan_buffer = ""
66        
67        time.sleep(0.02)
68    
69if __name__ == '__main__':
70    try:
71        run_example()
72    except(KeyboardInterrupt, SystemExit) as exErr:
73        print("\nEnding Example 1")
74        sys.exit(0)