Example 3 - Send Command

examples/de2120_ex3_send_command.py
  1#!/usr/bin/env python
  2#-----------------------------------------------------------------------------
  3# de2120_ex3_send_command.py
  4#------------------------------------------------------------------------
  5#
  6# Written by Priyanka Makin @ SparkFun Electronics, April 2021
  7#
  8# This example demonstrates how to use the "send_command()" method to send 
  9# arbitrary serial commands to the barcode reader. It also demonstrates the "CIDENA"
 10# or "Code ID Enable" function, which includes the barcode type when transmitting the
 11# decoded string.
 12#
 13# send_command() takes two strings as arguments, concatenate them, adds the command
 14# prefix "^_^" and the command suffix "." and then transmits the command to the module.
 15# For example, to enable matrix 2 of 5 scanning, which is done using the command
 16# "^_^M25ENA1." you would make the call "my_scanner.send_command("M25ENA", 1)"
 17#
 18# While it is valid to call "my_scanner.send_command("M25ENA1")", the former method
 19# is preferred in many cases.
 20# 
 21# NOTE: you must put the module into COM mode by scanning the PORVIC barcode 
 22# in the datasheet. This will put the module in the correct mode to receive 
 23# and transmit serial.
 24#
 25# This package has been developed on a Raspberry Pi 4. Connect the DE2120 Barcode
 26# Scanner Breakout directly to your Pi using a USB-C cable
 27#  
 28# Do you like this library? Help support SparkFun. Buy a board!
 29#
 30#==================================================================================
 31# Copyright (c) 2021 SparkFun Electronics
 32#
 33# Permission is hereby granted, free of charge, to any person obtaining a copy 
 34# of this software and associated documentation files (the "Software"), to deal 
 35# in the Software without restriction, including without limitation the rights 
 36# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 37# copies of the Software, and to permit persons to whom the Software is 
 38# furnished to do so, subject to the following conditions:
 39#
 40# The above copyright notice and this permission notice shall be included in all 
 41# copies or substantial portions of the Software.
 42#
 43# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 44# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 45# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 46# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 47# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 48# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 49# SOFTWARE.
 50#==================================================================================
 51# Example 3
 52
 53from __future__ import print_function
 54import de2120_barcode_scanner
 55import time
 56import sys
 57
 58def run_example():
 59
 60    print("\nSparkFun DE2120 Barcode Scanner Breakout Example 3")
 61    my_scanner = de2120_barcode_scanner.DE2120BarcodeScanner()
 62
 63    if my_scanner.begin() == False:
 64        print("\nThe Barcode Scanner module isn't connected correctly to the system. Please check wiring", \
 65            file=sys.stderr)
 66        return
 67    print("\nScanner ready!")
 68
 69    print("\n")
 70    print("\nTransmit Code ID with Barcode? (y/n)")
 71    print("\n---------------------------------------------")
 72
 73    val = input("\nType 'y' or 'n' or scan a barcode: ")
 74
 75    if val == 'y':
 76        print("\nCode ID will be displayed on scan")
 77        my_scanner.send_command("CIDENA", "1")
 78    elif val == 'n':
 79        print("\nCode ID will NOT be displayed on scan")
 80        my_scanner.send_command("CIDENA", "0")
 81    else:
 82        print("\nCommand not recognized")
 83            
 84    scan_buffer = ""
 85    
 86    while True:
 87        scan_buffer = my_scanner.read_barcode()
 88        if scan_buffer:
 89            print("\nCode found: ")
 90            print("\n" + str(scan_buffer))
 91            scan_buffer = ""
 92        
 93        time.sleep(0.02)
 94
 95
 96if __name__ == '__main__':
 97    try: 
 98        run_example()
 99    except (KeyboardInterrupt, SystemExit) as exErr:
100        print("\nEnding Example 3")
101        sys.exit(0)