Welches meinst du? Das py steht doch mehrmals im Thread.
Aber hier für dich nochmal:
#!/usr/bin/python3
# Brute-force the PIN of a smartmeter with a USB-IR-read-write-head.
# Install package pyserial with:
# python3 -m pip install pyserial
import datetime
import time
import serial
# parameters for serial port
COM_PORT = '/dev/ttyUSB0'
BAUDRATE = 9600
str100ms = b'\x00' * int(BAUDRATE * 0.01)
def make_pulse():
# write data that needs 100 ms to transmit.
ser.write(str100ms)
# wait 100 ms until transmitted and 100 ms as pause.
time.sleep(0.2)
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 (-1) 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 num in range(last_number + 1, 10000):
# convert the number to a string and pad it with leading zeros
number = format(num, '04d')
print('Testing PIN=' + number + '...')
# flash the LED to start a new number with the display test.
make_pulse()
# flash the LED to go to PIN input.
make_pulse()
# iterate through the digits of the number
for digit in number:
# blink the LED for the appropriate number of times
for i in range(int(digit)):
make_pulse()
# pause for 3 seconds between digits
time.sleep(3.1)
# 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
# write the number to the file "number.txt" with the current date and time
write_number_to_file("number.txt", number, datetime.datetime.now())
# close serial port
ser.close()