# srv_util_01_v164.py # LX-824HV Testing v1.4 tlfong01 2020sep20hkt1159 # 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 testRepeatWriteBytes(serialPort, writeBytes, waitSeconds, repeatCount): print(' Begin testRepeatWriteOneByte(), ...') repeatWriteBytes(serialPort, writeBytes, waitSeconds, repeatCount) print(' End testRepeatWriteOneByte().') 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 # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** ''' # srv_util_01_v142.py # LX-824HV Testing v0.3 tlfong01 2020sep13hkt1334 # SendRequestVin = UU<0x01><0x03><0x1b><0xe0> = /x55/x55/x01/x03/x1b/xe0 # RecvReplyVin = UU<0x01><0x05><0x1b><0x03><0x14><0xc7> = /x55/x55/x01/x05/x1b/x03/x14/xc7 # /xcc/x13 # = /xb1/x13 # = /xac/x13 # 0x13ac = 5036 # 0x13cc = 5068 # 0x13b1 = 5041 # 0x1403 = 5123 # bytes written = b'UU\x01\x03\x1b\xe0' # bytes read = b'UU\x01\x03\x1b\xe0UU\x01\x05\x1b\xfc\x13\xcf' # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** def convertTwoHexBytesToDec(twoHexBytes): lowHexByte = twoHexBytes[0:1] highHexByte = twoHexBytes[1:2] print('Low hex byte =', lowHexByte) print('High hex byte =', highHexByte) return def testConvertTwoHexBytesToDec0x13a1(): convertTwoHexBytesToDec(b'\x11\x22') return def bytesObjectTest(): bytesObject = b'\x11\x22\x33\x44\x55' #bytesObject = b'12345' print(bytesObject) bytes = bytesObject[2:3] print(bytes) intNum = int(bytes) print(intNum) bytesObject = b'abc123' bytes = bytesObject[2:3] print(bytesObject) print(bytes) #byte1 = bytesObject[0:1] #byte2 = bytesObject[1:2] #print(byte1, byte2) #bytesList = [bytesObject[i:i+2] for i in range(0, len(bytesObject))] return def convertBytesObjectToBytesArray(bytesObject): bytesList = [bytesObject[i:i+1] for i in range(0, len(bytesObject))] return bytesList def test01(): print(' Begin test01(), ...') bytesObject = b'UU\x01\x03\x1b\xe0UU\x01\x05\x1b\xfc\x13\xcf' bytesList = convertBytesObjectToBytesArray(bytesObject) print(' bytes object =', bytesObject) byteListRequestPart = [bytesObject[i] for i in (0, 1, 2, 3, 4, 5)] byteListReplyPart = [bytesObject[i] for i in (6, 7, 8, 9, 10, 11, 12, 13)] byteListRequestPartInHex = [hex(bytesObject[i]) for i in (0, 1, 2, 3, 4, 5)] byteListReplyPartInHex = [hex(bytesObject[i]) for i in (6, 7, 8, 9, 10, 11, 12, 13)] print(' byte list request part =', byteListRequestPart) print(' byte list reply part =', byteListReplyPart) print(' byte list request part in hex =', byteListRequestPartInHex) print(' byte list reply part in hex =', byteListReplyPartInHex) print(' End test01().') return # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** ''' # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** # ### Old tests 2020sep15hkt2219 # 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) ''' ### 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, ... ''' # ********** ********** ********** ********** ********** ********** ********** # ********** ********** ********** ********** ********** ********** ********** # ### Main ### # ### Old Tests 2020sep15hkt2222 ### # setSerialPortBaudRate(serialPort0, 115200) # serialPortLoopBack(serialPort0, b'xyz123\r\n') # serialPortLoopBack(serialPort0, b'\x55\x55\x01\x03\x1b\xe0') # requestVin = b'\x55\x55\x01\x03\x1b\xe0' # repeatTimes = 10000000 # testRepeatSerialPortLoopback(serialPort0, requestVin, repeatTimes) # Testing Rpi send/receive LeWanSoul request/reply bytes to/from LX-824HV # ### New Tests ### def repeatRequestLx824HvVin(): requestVin = b'\x55\x55\x01\x03\x1b\xe0' repeatTimes = 10000000 testRepeatSerialPortLoopback(serialPort0, requestVin, repeatTimes) return setSerialPortBaudRate(serialPort0, 115200) serialPortLoopBack(serialPort0, b'xyz123\r\n') serialPortLoopBack(serialPort0, b'\x55\x55\x01\x03\x1b\xe0') repeatRequestLx824HvVin() # End of program # ### Sample OUtput tlfong01 2020seo20hkt1432 ### ''' >>> %Run srv_util_01_v163.py Begin setSerialPortBaudRate(), ... Serial port = /dev/serial0, Buad rate = 115200 End setSerialPortBaudRate(), ... Begin serialPortLoopBack(), ... bytes written = b'xyz123\r\n' bytes read = b'xyz123\r\n' End serialPortLoopBack() Begin serialPortLoopBack(), ... bytes written = b'UU\x01\x03\x1b\xe0' bytes read = b'UU\x01\x03\x1b\xe0UU\x01\x05\x1bJ\x14\x80' End serialPortLoopBack() Begin testRepeatSerialPortLoopback(), ... Begin repeat serialPortLoopBack() repeating 10000000 times, C to abort, ... Traceback (most recent call last): File "/home/pi/Programs/programs/srv_util_01_v163.py", line 245, in repeatRequestLx824HvVin() File "/home/pi/Programs/programs/srv_util_01_v163.py", line 239, in repeatRequestLx824HvVin testRepeatSerialPortLoopback(serialPort0, requestVin, repeatTimes) File "/home/pi/Programs/programs/srv_util_01_v163.py", line 97, in testRepeatSerialPortLoopback repeatSerialPortLoopback(serialPort, writeBytes, repeatTimes) File "/home/pi/Programs/programs/srv_util_01_v163.py", line 84, in repeatSerialPortLoopback readBytes = serialPort.read(maxBytes) File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 483, in read ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout.time_left()) KeyboardInterrupt: Execution interrupted >>> # ### End of sample output ### '''