Project

General

Profile

Feature #10505 » test.py

Bogdan N., 04/17/2023 12:44 AM

 
1
import struct
2
import serial
3
import time
4
def print_hex(data):
5
    for i in range(0, len(data), 8):
6
        print(" ".join(["%02x" % b for b in data[i:i+8]]))
7

    
8
def print_hex_ascii(data):
9
    for i in range(0, len(data), 8):
10
        print(" ".join(["%02x" % b for b in data[i:i+8]]), "\t", "".join([chr(b) if 32 <= b < 127 else "." for b in data[i:i+8]]))
11

    
12
def _init(ser):
13
    magic = bytearray([0x50, 0xbb, 0xff, 0x20, 0x12, 0x07, 0x25])
14
    ser.write(magic)
15
    time.sleep(0.02)
16

    
17
    ack = ser.read(1)
18
    if ack != b"\x06":
19
        if ack:
20
            print("wrong ack", repr(ack))
21
            exit()
22

    
23
    ser.write(b"\x02")
24

    
25
    response = b""
26
    for i in range(1, 13):
27
        byte = ser.read(1)
28
        response += byte
29
        if byte == b"\xDD":
30
            break
31

    
32
    if len(response) in [8, 12]:
33
        print("Valid response, got this:")
34
        print(response)
35
        if len(response) == 12:
36
            ident = (bytes([response[0], response[3], response[5]]) + response[7:])
37
        else:
38
            ident = response
39
    else:
40
        # bad response
41
        msg = "Unexpected response:"
42
        msg += " ".join(["%02x" % b for b in response])
43
        print(msg)
44

    
45

    
46
    print("Sending ACK")
47
    ser.write(b"\x06")
48
    ack = ser.read(1)
49
    if ack != b"\x06":
50
        print("Radio refused clone")
51
        exit()
52
    
53
    return ident
54

    
55
def _read_block(radio, start, size, first_command=False):
56
    msg = struct.pack(">BHB", ord("S"), start, size)
57
    radio.write(msg)
58

    
59
    if first_command is False:
60
        ack = radio.read(1)
61
        if ack != b"\x06":
62
            print("Radio refused to send second block 0x%04x" % start)
63
            exit()
64

    
65
    answer = radio.read(4)
66

    
67
    if len(answer) != 4:
68
        print("Radio refused to send block 0x%04x" % start)
69
        exit()
70

    
71
    cmd, addr, length = struct.unpack(">BHB", answer)
72

    
73
    if cmd != ord("X") or addr != start or length != size:
74
        print("Invalid answer for block 0x%04x:" % start)
75
        print("CMD: %s  ADDR: %04x  SIZE: %02x" % (cmd, addr, length))
76
        exit()
77

    
78
    chunk = radio.read(size)
79

    
80
    if not chunk:
81
        print("Radio did not send block 0x%04x" % start)
82
        exit()
83
    elif len(chunk) != size:
84
        print("Chunk length was 0x%04i" % len(chunk))
85
        exit()
86

    
87
    radio.write(b"\x06")
88
    #time.sleep(0.01)
89
    return chunk
90

    
91
ser = serial.Serial('COM3', 9600, bytesize=8, parity='N', stopbits=1, timeout=None)
92

    
93
_init(ser)
94

    
95

    
96
'''
97
block = _read_block(ser,  0x1EC0, 0x40, True)
98
print_hex_ascii(block)
99
print()
100
block = _read_block(ser,  0x1E80, 0x40, False)
101
print_hex_ascii(block)
102
print()
103
block = _read_block(ser,  0x1EC0, 0x40, False)
104
print_hex_ascii(block)
105
print()
106
'''
107

    
108

    
109
print("=====================================================")
110
_read_block(ser,  0x0000, 0x40, True)
111

    
112
for i in range(0x0000, 0x2000, 0x40):
113
    block1 = _read_block(ser, i, 0x40, False)
114
    dummy = _read_block(ser,  0x0000, 0x40, False)
115
    block2 = _read_block(ser, i, 0x40, False)
116
    print("\nBlock: %04x" % i)
117
    print_hex_ascii(block1)
118

    
119
    # compare block1 and block2
120
    if block1 != block2:
121
        print("\n========================================")
122
        print("--> Block 1 and 2 are different!")
123
        print("\nBlock 1:")
124
        print_hex_ascii(block1)
125
        print("\nBlock 2:")
126
        print_hex_ascii(block2)
127
        print("\nDummy:")
128
        print_hex_ascii(dummy)
129
        print("========================================")
130

    
131
ser.close()
(9-9/41)