Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2021
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import serial
  2. from binascii import unhexlify
  3.  
  4. arduino = serial.Serial(port='COM4', baudrate=115200, bytesize=8)
  5.  
  6. doCapture = False
  7.  
  8. while not doCapture:
  9.     doCapture = input("Start capture? ") == "A"
  10.  
  11. buffer = []
  12. arduino.write(bytes("A", 'utf-8'))
  13.  
  14. prevByte = b""
  15. currByte = b""
  16.  
  17. line = arduino.readline().strip()
  18. while True:
  19.     if len(line) == 1:
  20.         currByte = unhexlify(b'0' + line)
  21.         if (prevByte == b"\xff" and currByte == b"\xd8"):
  22.             buffer = [b"\xff", b"\xd8"]
  23.             print("found a new start")
  24.         else:
  25.             buffer.append(currByte)
  26.  
  27.     if len(line) == 2:
  28.         currByte = unhexlify(line)
  29.         if (prevByte == b"\xff" and currByte == b"\xd8"):
  30.             buffer = [b"\xff", b"\xd8"]
  31.             print("found a new start")
  32.         else:
  33.             buffer.append(currByte)
  34.  
  35.     if line == b"End read" or (prevByte == b"\xff" and currByte == b"\xd9"):
  36.         break
  37.  
  38.     line = arduino.readline().strip()
  39.     prevByte = currByte
  40.  
  41.  
  42. data = b"".join(buffer)
  43. print(data)
  44. with open("C:/Users/Hammad/Documents/GitHub/SBudniC/cameraPrintBytesTest/test.jpg", "wb") as f:
  45.     f.write(data)
  46.  
  47. arduino.close()
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement