otime-testing/utilities/mtr_sim.py

77 lines
2.3 KiB
Python
Raw Normal View History

2022-04-09 21:51:47 +00:00
import argparse
import serial
import otime
from rich import inspect
def main():
global package_n
package_n = 0
argparser = argparse.ArgumentParser(
description=("Acts as a live mtr reading data line by line from a log file and sending them over serial"))
argparser.add_argument('port', help='Serial port identifier')
argparser.add_argument(
'-f', '--file',
help=(
"Mtr log file to use"))
argparser.add_argument(
'-v', '--verbose', action='store_true', help='Verbose output')
args = argparser.parse_args()
port = serial.Serial(port=args.port, baudrate=9600)
event = otime.event(0, 'NoName')
event.import_mtr_file(args.file)
for card in event.card_punches:
input('Press enter to send next card')
if args.verbose == True:
inspect(card)
data = card_to_bytes(card)
port.write(data)
def card_to_bytes(card):
global package_n
package_n += 1
msg = b'\xFF\xFF\xFF\xFF\xE6\x4D\x9C\x3E'
year = int(str(card.read_time.year)[2:4])
month = card.read_time.month
day = card.read_time.day
hour = card.read_time.hour
minute = card.read_time.minute
second = card.read_time.second
msg += year.to_bytes(1, byteorder='little')
msg += month.to_bytes(1, byteorder='little')
msg += day.to_bytes(1, byteorder='little')
msg += hour.to_bytes(1, byteorder='little')
msg += minute.to_bytes(1, byteorder='little')
msg += second.to_bytes(1, byteorder='little')
#ms
msg += b'\x00\x00'
msg += package_n.to_bytes(4, byteorder='little')
msg += card.card.to_bytes(3, byteorder='little')
# Gidder ikke å finne ut hva Producweek Producyear er
msg += b'\x00\x00'
# Hopper overogså over ECardHeadSum
msg += b'\x00'
for n in range(50):
try:
control = card.controls[n]
split = card.splits[n]
except:
control = 0
split = 0
msg += control.to_bytes(1, byteorder='little')
msg += split.to_bytes(2, byteorder='little')
# 56 byte string
msg += int(0).to_bytes(56, byteorder='little')
# Checksum
csum = sum(msg) % 256
msg += csum.to_bytes(1, byteorder='little')
msg += b'\x00'
return msg
if __name__ == '__main__':
main()