Advertisement
ksoltan

mcpc_serial.py

Dec 11th, 2020
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import sys, time
  2. import serial
  3.  
  4. def convertCmdToBytes(str_cmd):
  5.   if(str_cmd[-1] != '\r'):
  6.     str_cmd += '\r' # Cmd MUST end in carriage return
  7.   cmd_bytearray = bytes(str_cmd, 'utf-8')
  8.   # Return array of individual byte literals
  9.   individual_bytes = [cmd_bytearray[i:i+1] for i in range(len(cmd_bytearray))]
  10.   return individual_bytes
  11.  
  12. try:
  13.   baudrate = 230400
  14.   ser = serial.Serial('COM5', baudrate=baudrate, timeout=1)
  15.   # cmd = 'usbid\r'
  16.   cmd = 'read\r'
  17.   for b in convertCmdToBytes(cmd):
  18.     ser.write(b)
  19.     time.sleep(0.001) # Delay between sending consecutive bytes
  20.   line = ser.readline()
  21.   print(line)
  22.  
  23. except KeyboardInterrupt:
  24.   print('bye')
  25.   sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement