# srv_util_01_v153.py # LX-824HV Testing v1.0 tlfong01 2020sep15hkt2133 # Contents # Serial UART LoopBack Program v0.62 (Minimal) # Serial UART LoopBack Program v0.98 (Multiple ports USB and on board UYART Ports) # HiWonder serial bus servo LX-824HV LewanSoul command testing (2020sep15) # Serial UART LoopBack Program v0.99 - https://penzu.com/p/f2cb232e # serial_loopback_test_v062.py tlfong01 2020jun03hkt1447 # Rpi4B buster (r2020feb13), python 3.7.3 (r2019dec20), thonny v3.2.7 (r2020jan22) from time import sleep import serial serialPort0 = serial.Serial(port = '/dev/serial0', baudrate = 9600, parity = serial.PARITY_NONE, stopbits = serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS, timeout= 1) def setSerialPortBaudRate(serialPort, baudrate): print(' Begin setSerialPortBaudRate(), ...') print(' Serial port =', (serialPort0.name + ','), 'Buad rate =', baudrate) serialPort.baudrate = baudrate print(' End setSerialPortBaudRate(), ...') print('') return def serialPortWriteBytes(serialPort, writeBytes): serialPort.write(writeBytes) return def serialPortReadBytes(serialPort, maxBytesLength): readBytes = serialPort.read(maxBytesLength) return readBytes def serialPortWriteWaitReadBytes(serialPort, writeBytes, maxBytesLength, waitSeconds): serialPort.flushInput() serialPort.flushOutput() serialPort.write(writeBytes) sleep(waitSeconds) readBytes = serialPortReadBytes(serialPort, maxBytesLength) print(' bytes written = ', writeBytes) print(' bytes read = ', readBytes) return readBytes def repeatWriteBytes(serialPort, writeBytes, waitSeconds, repeatCount): print(' Begin repeatWriteOneByte(), ...') for i in range(repeatCount): serialPortWriteBytes(serialPort, writeBytes) sleep(waitSeconds) print(' End repeatWriteOneByte().') return def serialPortLoopBack(serialPort, writeBytes): print(' Begin serialPortLoopBack(), ...') maxBytesLength = 64 waitSeconds = 0.0 serialPortWriteWaitReadBytes(serialPort, writeBytes, maxBytesLength, waitSeconds) print(' End serialPortLoopBack()\n') return def repeatSerialPortLoopback(serialPort, writeBytes, repeatTimes): print(' Begin repeat serialPortLoopBack()') print(' repeating', repeatTimes, 'times, C to abort, ...') maxBytes = 64 waitSeconds = 0.0 serialPort.flushInput() serialPort.flushOutput() for count in range(repeatTimes): serialPort.write(writeBytes) # sleep(waitSeconds) readBytes = serialPort.read(maxBytes) print(' End repeat serialPortLoopBack()') return def testRepeatSerialPortLoopback(serialPort, writeBytes, repeatTimes): print(' Begin testRepeatSerialPortLoopback(), ...') print(' Serial port =', (serialPort0.name + ' ')) print(' WriteBytes =', writeBytes) print(' RepeatTimes =', repeatTimes) print(' Now repeating', repeatTimes, 'times, C to abort, ...\n') repeatSerialPortLoopback(serialPort, writeBytes, repeatTimes) print(' End testRepeatSerialPortLoopback(), ...\n') return # ### Main ### # Set serial serialPort0 115200bd setSerialPortBaudRate(serialPort0, 115200) # Loopback bytes object b'xyz123\r\n' serialPortLoopBack(serialPort0, b'xyz123\r\n') # Loopback reuest Vin command b'\x55\x55\x01\x03\x1b\xe0' serialPortLoopBack(serialPort0, b'\x55\x55\x01\x03\x1b\xe0') # Testing Rpi send/receive LeWanSoul request/reply bytes to/from LX-824HV requestVin = b'\x55\x55\x01\x03\x1b\xe0' repeatTimes = 10000000 testRepeatSerialPortLoopback(serialPort0, requestVin, repeatTimes) # End of program ''' ### Sample output tlfong01 2020sep15hkt2132 ### Begin serialPortLoopBack(), ... bytes written = b'UU\x01\x03\x1b\xe0' bytes read = b'UU\x01\x03\x1b\xe0UU\x01\x05\x1bS\x14w' End serialPortLoopBack() Begin testRepeatSerialPortLoopback(), ... Serial port = /dev/serial0 WriteBytes = b'UU\x01\x03\x1b\xe0' RepeatTimes = 10000000 Now repeating 10000000 times, C to abort, ... Begin repeat serialPortLoopBack() repeating 10000000 times, C to abort, ... ''' # End