-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparison_test.py
81 lines (64 loc) · 2.25 KB
/
comparison_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
import unittest
import serial
import serial.tools.list_ports
import hashlib
import numpy as np
import re
#
# Test that the P2000T Cartridge Reader can read the BASIC cartridge.
#
class TestStringMethods(unittest.TestCase):
def test_serial_interface(self):
port = self.find_port()
self.assertTrue(port)
ser = serial.Serial(port,
115200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1.5) # open serial port
if not ser.isOpen():
ser.open()
original = self.read_rom('original.bin')
checksum = hashlib.md5(original)
assert(checksum.hexdigest() == '2191811aa64f8e7f273ce0f462374728')
# check that the correct board is attached
ser.write(b'READINFO')
rsp = ser.read(8)
rsp = ser.read(16)
print(rsp)
# read 16 kb from cartridge
capture = bytearray()
for i in range(0,4):
ser.write(b'RB%06X' % (i*4096))
print('Reading bank %i' % i)
rsp = ser.read(8)
rsp = ser.read(0x1000)
capture.extend(rsp)
ser.close()
np.testing.assert_equal(capture, original)
def read_rom(self, filename):
"""
Read rom file
"""
f = open(filename, 'rb')
data = bytearray(f.read())
data.extend([0x00] * (0x4000 - len(data)))
f.close()
return data
def find_port(self):
"""
Find the correct port by looping over all ports and looking for one
that has an hardware id matching an Arduino Leonardo
"""
ports = serial.tools.list_ports.comports()
pattern = r'USB VID:PID=([0-9:]+) SER=.*'
for port,desc,hwid in ports:
match = re.match(pattern, hwid)
if match is not None:
if match.group(1) == '2341:8036':
return port
return False
if __name__ == '__main__':
unittest.main()