Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- import time
- import RPi.GPIO as GPIO
- import serial
- # parameters for serial port
- COM_PORT = '/dev/ttyS0'
- BAUDRATE = 9600
- str100ms = b'\x00' * int(BAUDRATE * 0.01)
- # use the Broadcom pin numbering scheme
- GPIO.setmode(GPIO.BCM)
- # set up the GPIO pin to control the LED (replace X with the appropriate pin number)
- LED_PIN = 17
- GPIO.setup(LED_PIN, GPIO.OUT)
- def set_led_state(state):
- # Turn the LED on or off
- GPIO.output(LED_PIN, state)
- def get_message_length():
- old = 0
- ser.reset_input_buffer()
- time.sleep(0.1)
- new = ser.in_waiting
- if new > 0:
- # wait as long as data is received.
- # we only want complete data messages.
- while old < new:
- time.sleep(0.1)
- old = new
- new = ser.in_waiting
- old = 0
- new = 0
- ser.reset_input_buffer()
- # wait until data is received.
- while old == new:
- time.sleep(0.1)
- old = new
- new = ser.in_waiting
- # wait as long as data is received.
- while old < new:
- time.sleep(0.1)
- old = new
- new = ser.in_waiting
- return new
- def read_last_number_from_file(filename):
- # Read the last number from a file
- # return the initial number (0001) if the file does not exist
- try:
- with open(filename, 'r') as f:
- # read the last line of the file
- last_line = f.readlines()[-1]
- # split the line on the tab character
- date_str, number_str = last_line.split('\t')
- # return the number as an integer
- return int(number_str)
- except FileNotFoundError:
- return 1
- def write_number_to_file(filename, number_str, timestamp):
- # Write a number and timestamp to a file
- with open(filename, 'a') as f:
- # write the number and timestamp to the file
- f.write(f'{timestamp}\t{number_str}\n')
- # read the last number from the file "number.txt"
- last_number = read_last_number_from_file("number.txt")
- # open serial port
- ser = serial.Serial(COM_PORT, BAUDRATE, timeout=0.5, inter_byte_timeout=0.1)
- # get next data message length
- print('Waiting for reference message...')
- ref_msg_len = get_message_length()
- print('Reference message length is ' + str(ref_msg_len) + ' bytes')
- # iterate through the numbers from last_number + 1 to 9999
- for number in range(last_number + 1, 10000):
- # flash the LED for 1 second before starting a new number and also blink shortly to start input
- set_led_state(True)
- time.sleep(0.1)
- set_led_state(False)
- time.sleep(0.2)
- set_led_state(True)
- time.sleep(0.1)
- set_led_state(False)
- time.sleep(0.2)
- # convert the number to a string and pad it with leading zeros
- number_str = format(number, '04d')
- # iterate through the digits of the number
- for digit in number_str:
- # convert the digit to an integer
- digit_int = int(digit)
- # blink the LED for the appropriate number of times
- for i in range(digit_int):
- # turn the LED on
- set_led_state(True)
- # wait for 100ms
- time.sleep(0.1) # Impulsdauer Pin Zahl
- # turn the LED off
- set_led_state(False)
- # wait for 100ms
- time.sleep(0.2) #Pause zwischen den PIN Zahlen
- # pause for 1 second between digits
- time.sleep(4.0)
- # get next data message length
- msg_len = get_message_length()
- print('received ' + str(msg_len) + ' bytes')
- if msg_len > ref_msg_len:
- # message length got bigger because of new additional data = PIN was correct
- # output the PIN to the console
- number = 'PIN=' + number
- print(number)
- # write the PIN number to the file "number.txt" with the current date and time
- write_number_to_file("number.txt", number, datetime.datetime.now())
- break
- # output the number to the console
- print(number_str)
- # write the number to the file "number.txt" with the current date and time
- write_number_to_file("number.txt", number_str, datetime.datetime.now())
- # clean up the GPIO resources
- GPIO.cleanup()
- # close serial port
- ser.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement