Advertisement
Guest User

SML

a guest
Nov 27th, 2023
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. import datetime
  2. import time
  3. import RPi.GPIO as GPIO
  4. import serial
  5.  
  6. # parameters for serial port
  7. COM_PORT = '/dev/ttyS0'
  8. BAUDRATE = 9600
  9. str100ms = b'\x00' * int(BAUDRATE * 0.01)
  10.  
  11. # use the Broadcom pin numbering scheme
  12. GPIO.setmode(GPIO.BCM)
  13.  
  14. # set up the GPIO pin to control the LED (replace X with the appropriate pin number)
  15. LED_PIN = 17
  16. GPIO.setup(LED_PIN, GPIO.OUT)
  17.  
  18. def set_led_state(state):
  19.     # Turn the LED on or off
  20.     GPIO.output(LED_PIN, state)
  21.  
  22. def get_message_length():
  23.     old = 0
  24.  
  25.     ser.reset_input_buffer()
  26.     time.sleep(0.1)
  27.     new = ser.in_waiting
  28.     if new > 0:
  29.         # wait as long as data is received.
  30.         # we only want complete data messages.
  31.         while old < new:
  32.             time.sleep(0.1)
  33.             old = new
  34.             new = ser.in_waiting
  35.         old = 0
  36.         new = 0
  37.         ser.reset_input_buffer()
  38.  
  39.     # wait until data is received.
  40.     while old == new:
  41.         time.sleep(0.1)
  42.         old = new
  43.         new = ser.in_waiting
  44.  
  45.     # wait as long as data is received.
  46.     while old < new:
  47.         time.sleep(0.1)
  48.         old = new
  49.         new = ser.in_waiting
  50.  
  51.     return new
  52.  
  53. def read_last_number_from_file(filename):
  54.     # Read the last number from a file
  55.     # return the initial number (0001) if the file does not exist
  56.     try:
  57.         with open(filename, 'r') as f:
  58.             # read the last line of the file
  59.             last_line = f.readlines()[-1]
  60.             # split the line on the tab character
  61.             date_str, number_str = last_line.split('\t')
  62.             # return the number as an integer
  63.             return int(number_str)
  64.     except FileNotFoundError:
  65.         return 1
  66.  
  67. def write_number_to_file(filename, number_str, timestamp):
  68.     # Write a number and timestamp to a file
  69.     with open(filename, 'a') as f:
  70.         # write the number and timestamp to the file
  71.         f.write(f'{timestamp}\t{number_str}\n')
  72.  
  73. # read the last number from the file "number.txt"
  74. last_number = read_last_number_from_file("number.txt")
  75.  
  76.  
  77.  
  78. # open serial port
  79. ser = serial.Serial(COM_PORT, BAUDRATE, timeout=0.5, inter_byte_timeout=0.1)
  80.  
  81. # get next data message length
  82. print('Waiting for reference message...')
  83. ref_msg_len = get_message_length()
  84. print('Reference message length is ' + str(ref_msg_len) + ' bytes')
  85.  
  86.  
  87. # iterate through the numbers from last_number + 1 to 9999
  88. for number in range(last_number + 1, 10000):
  89.     # flash the LED for 1 second before starting a new number and also blink shortly to start input
  90.     set_led_state(True)
  91.     time.sleep(0.1)
  92.     set_led_state(False)
  93.     time.sleep(0.2)
  94.     set_led_state(True)
  95.     time.sleep(0.1)
  96.     set_led_state(False)
  97.     time.sleep(0.2)
  98.     # convert the number to a string and pad it with leading zeros
  99.     number_str = format(number, '04d')
  100.  
  101.     # iterate through the digits of the number
  102.     for digit in number_str:
  103.         # convert the digit to an integer
  104.         digit_int = int(digit)
  105.  
  106.         # blink the LED for the appropriate number of times
  107.         for i in range(digit_int):
  108.             # turn the LED on
  109.             set_led_state(True)
  110.             # wait for 100ms
  111.             time.sleep(0.1)    #  Impulsdauer Pin Zahl
  112.             # turn the LED off
  113.             set_led_state(False)
  114.             # wait for 100ms
  115.             time.sleep(0.2)  #Pause zwischen den PIN Zahlen
  116.  
  117.         # pause for 1 second between digits
  118.         time.sleep(4.0)
  119.  
  120.  
  121.     # get next data message length
  122.     msg_len = get_message_length()
  123.     print('received ' + str(msg_len) + ' bytes')
  124.     if msg_len > ref_msg_len:
  125.         # message length got bigger because of new additional data = PIN was correct
  126.         # output the PIN to the console
  127.         number = 'PIN=' + number
  128.         print(number)
  129.         # write the PIN number to the file "number.txt" with the current date and time
  130.         write_number_to_file("number.txt", number, datetime.datetime.now())
  131.         break
  132.  
  133.     # output the number to the console
  134.     print(number_str)
  135.     # write the number to the file "number.txt" with the current date and time
  136.     write_number_to_file("number.txt", number_str, datetime.datetime.now())
  137.  
  138. # clean up the GPIO resources
  139. GPIO.cleanup()
  140.  
  141.  
  142. # close serial port
  143. ser.close()
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement