OpenCores
URL https://opencores.org/ocsvn/aes-128-ecb-encoder/aes-128-ecb-encoder/trunk

Subversion Repositories aes-128-ecb-encoder

[/] [aes-128-ecb-encoder/] [trunk/] [soft/] [serialExchange.py] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 vv_gulyaev
"""
2
 *  Simple python script
3
 *    Functions:
4
 *        - Send hardcoded data(key and plainText) and commands via UART
5
 *        - Receive and output ciphered data
6
 *
7
 *  Copyright 2020 by Vyacheslav Gulyaev <v.gulyaev181@gmail.com>
8
 *
9
 *  Licensed under GNU General Public License 3.0 or later.
10
 *  Some rights reserved. See COPYING, AUTHORS.
11
 *
12
 * @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
13
"""
14
 
15
 
16
import serial
17
import serial.tools.list_ports
18
from time import sleep
19
 
20
SERIAL_PORT_PATH = '/dev/ttyUSB0'
21
SERIAL_PORT_SPEED = 38400
22
 
23
 
24
CMD_SET_KEY  = 0xF0
25
CMD_SET_DATA = 0xE1
26
 
27
PAUSE_SEC = 0.1
28
 
29
ser = None
30
receivedData = None
31
 
32
 
33
def main():
34
    ports = serial.tools.list_ports.comports(include_links=False)
35
    for port in ports :
36
        print(port.device)
37
 
38
    ser = serial.Serial(SERIAL_PORT_PATH, SERIAL_PORT_SPEED, timeout = 1)
39
 
40
    key       = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]
41
    plainText = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]
42
 
43
    print ('Sendeded CMD_SET_KEY:', format(CMD_SET_KEY, '02x'))
44
    print ('Sending Key         :', '  '.join(format(byte, '02x') for byte in key))
45
    ser.write(chr(CMD_SET_KEY))
46
    sleep(PAUSE_SEC)
47
    for byte_key in key:
48
        ser.write(chr(byte_key))
49
 
50
    print ('Sendeded CMD_SET_DATA:', format(CMD_SET_DATA, '02x'))
51
    print ('Sending plainText:', '  '.join(format(byte, '02x') for byte in plainText))
52
    ser.write(chr(CMD_SET_DATA))
53
    sleep(PAUSE_SEC)
54
    for byte_plainText in plainText:
55
        ser.write(chr(byte_plainText))
56
 
57
    print ('Waiting ciphered data...')
58
    received_ok = False;
59
    while (received_ok==False):
60
        receivedData = ser.readline()
61
        if (len(receivedData)>0):
62
            print('Data:', '  '.join(format(ord(byte), '02x') for byte in receivedData))
63
            received_ok = True
64
 
65
 
66
 
67
if __name__ == "__main__":
68
    main()
69
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.