|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +__author__ = "ArkEdge Space Inc." |
| 4 | + |
| 5 | +import struct |
| 6 | +import serial |
| 7 | +import time |
| 8 | +import math |
| 9 | +import binascii |
| 10 | +import pprint |
| 11 | + |
| 12 | + |
| 13 | +def get_baudrate_list(): |
| 14 | + return [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 11520000, 1500000, 20000] |
| 15 | + |
| 16 | + |
| 17 | +def byte2int(i_byte, i_endian = 'big'): |
| 18 | + return int.from_bytes(i_byte, i_endian) |
| 19 | + |
| 20 | +def int2byte(i_int, i_endian = 'big'): |
| 21 | + n_octet = math.ceil(math.log2(i_int)/8.0) |
| 22 | + print ("n_octet: %d" % n_octet) |
| 23 | + return i_int.to_bytes(n_octet, i_endian) |
| 24 | + |
| 25 | +''' |
| 26 | +array conversion: int -> byte |
| 27 | + array is list in python |
| 28 | +''' |
| 29 | +def convArrayInt2Byte(array_int): |
| 30 | + array_byte = [] |
| 31 | + for data_i in array_int: |
| 32 | + array_byte.append(bytes([0xff & data_i])) |
| 33 | + return array_byte |
| 34 | + |
| 35 | +''' |
| 36 | +array conversion: byte -> int |
| 37 | + array is list in python |
| 38 | +''' |
| 39 | +def convArrayByte2Int(array_byte): |
| 40 | + array_int = [] |
| 41 | + for _b in array_byte: |
| 42 | + _x = struct.unpack('1B', _b) |
| 43 | + array_int.append(_x[0]) |
| 44 | + return array_int |
| 45 | + |
| 46 | +def readFileBin(filename = '__dump__.img'): |
| 47 | + with open(filename, mode='rb') as f: |
| 48 | + _content = f.read() # list of Integers, not Bytes ! |
| 49 | + print ('file: %s, size: %d-bytes' % (filename, len(_content))) |
| 50 | + |
| 51 | + # conversion: int -> byte |
| 52 | + _array_bytes = [] |
| 53 | + for _ci in _content: |
| 54 | + _array_bytes.append(bytes([_ci])) |
| 55 | + |
| 56 | + return _array_bytes |
| 57 | + |
| 58 | +def writeFileBin(filename = 'tmp.img', data = []): |
| 59 | + with open(filename, mode='wb') as f: |
| 60 | + for cb in data: |
| 61 | + f.write(cb) |
| 62 | + |
| 63 | +byte_order = 'big' |
| 64 | + |
| 65 | +uart_settings = { |
| 66 | + 'dev': '/dev/ttyUSB0', |
| 67 | + 'baudrate': 115200, #9600, # 9600, 115200 |
| 68 | + 'timeout': 10, |
| 69 | + } |
| 70 | + |
| 71 | +class uart: |
| 72 | + |
| 73 | + def __init__(self, dev=uart_settings['dev'], baudrate=uart_settings['baudrate'], timeout=uart_settings['timeout'], byte_order=byte_order): |
| 74 | + self.dev = dev |
| 75 | + self.baudrate = baudrate |
| 76 | + self.timeout = timeout |
| 77 | + self.byte_order = byte_order |
| 78 | + |
| 79 | + def open(self): |
| 80 | + self.uart_device = serial.Serial(self.dev, self.baudrate, timeout=self.timeout) |
| 81 | + |
| 82 | + def close(self): |
| 83 | + self.uart_device.close() |
| 84 | + |
| 85 | + ''' |
| 86 | + tx: transfer 1 octet |
| 87 | + ''' |
| 88 | + def tx(self, x_int): |
| 89 | + _x_byte = bytes([0xFF & x_int]) |
| 90 | + return self.uart_device.write(_x_byte) |
| 91 | + |
| 92 | + |
| 93 | + ''' |
| 94 | + rx_primitive: receive 1 octet |
| 95 | + ''' |
| 96 | + def rx_primitive(self): |
| 97 | + |
| 98 | + rx_byte = self.uart_device.read(1) |
| 99 | + |
| 100 | + ret = { |
| 101 | + 'byte': rx_byte, |
| 102 | + 'int': 0, |
| 103 | + 'isValid': False, |
| 104 | + } |
| 105 | + |
| 106 | + if len(rx_byte) > 0: |
| 107 | + rx_int = struct.unpack('1B', rx_byte)[0] |
| 108 | + ret = { |
| 109 | + 'byte': rx_byte, |
| 110 | + 'int': rx_int, |
| 111 | + 'is_valid': True, |
| 112 | + } |
| 113 | + |
| 114 | + return ret |
| 115 | + |
| 116 | + ''' |
| 117 | + rx: receive N octets forerver |
| 118 | + ''' |
| 119 | + def rx(self): |
| 120 | + |
| 121 | + while True: |
| 122 | + rx_data = self.rx_primitive() |
| 123 | + if True == rx_data['is_valid']: |
| 124 | + yield rx_data['int'] |
| 125 | + |
| 126 | + def __testTx(self, data=0x6162636465666768696a6b6c6d6e6f707172737475767778797a): |
| 127 | + _eot = 0x04 |
| 128 | + data = data << 16 | 0x0D0A # <CR><LF> |
| 129 | + data = 0x6465 |
| 130 | + data_byte = int2byte(data, byte_order) |
| 131 | + self.uart_device.write(data_byte) |
| 132 | + if False: |
| 133 | + print("0x%x" % byte2int(data_byte, byte_order)) |
| 134 | + time.sleep(1.0) |
| 135 | + |
| 136 | + def tx_file(self, filename = '/tmp/tran.img'): |
| 137 | + data_tx = readFileBin(filename = filename) |
| 138 | + |
| 139 | + n_byte = len(data_tx) |
| 140 | + progress_tick = int(math.ceil(n_byte / 20.0)) |
| 141 | + |
| 142 | + t0 = time.time() |
| 143 | + for i, _b in enumerate(data_tx): |
| 144 | + self.uart_device.write(_b) |
| 145 | + if 0 == (i % progress_tick): |
| 146 | + print('=>', end='', flush=True) |
| 147 | + t = time.time() - t0 |
| 148 | + bw = n_byte/t |
| 149 | + print("") |
| 150 | + print(": End UART-Tx") |
| 151 | + print("--------------------------------") |
| 152 | + print("BandWidth: %f KByte/sec" % (bw/1024.)) |
| 153 | + print("--------------------------------") |
| 154 | + return { |
| 155 | + 'n_byte': n_byte, |
| 156 | + 'wallclocktime': t, |
| 157 | + 'bandwidth': bw, |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + def rx_file(self, filename = '/tmp/recv.img', n_byte = 1048576): |
| 162 | + progress_tick = int(math.ceil(n_byte / 20.0)) |
| 163 | + data_rx = [] |
| 164 | + data_rx_byte = [] |
| 165 | + for i in range(n_byte): |
| 166 | + rx_byte = self.uart_device.read(1) |
| 167 | + rx_int = struct.unpack('1B', rx_byte)[0] |
| 168 | + data_rx.append(rx_int) |
| 169 | + data_rx_byte.append(rx_byte) |
| 170 | + |
| 171 | + if 0 == i % progress_tick: |
| 172 | + print ('=>', end = '', flush = True) |
| 173 | + |
| 174 | + writeFileBin(filename = filename, data = data_rx_byte) |
| 175 | + |
| 176 | + print("\n: End UART-Rx") |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + def diag(self): |
| 181 | + pprint.pprint(vars(self)) |
| 182 | + return vars(self) |
0 commit comments