storage.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import serial
  2. from serial import SerialException
  3. import _thread
  4. import struct
  5. import numpy as np
  6. import math
  7. import time
  8. from matplotlib import pyplot as plt
  9. ser = None
  10. portName="COM6"
  11. while not ser:
  12. try:
  13. ser = serial.Serial(portName, 115200)
  14. except serial.SerialException:
  15. print(portName, " not available")
  16. portName="COM" + input("COM")
  17. print("Connected to ", ser.name)
  18. def read():
  19. while 1:
  20. if ser.inWaiting()>0:
  21. print(ser.read(ser.inWaiting()).decode("utf-8"))
  22. time.sleep(.1)
  23. _thread.start_new_thread( read, () )
  24. def write(val):
  25. ser.write((int(val)&0xFF).to_bytes(1, byteorder='big', signed=False))
  26. def writeInt(val):
  27. ser.write((int(val)&0xFFFF).to_bytes(2, byteorder='big', signed=False))
  28. def writeLng(val):
  29. ser.write((int(val)&0xFFFFFFFF).to_bytes(4, byteorder='big', signed=False))
  30. ser.write(b'~t~c"')
  31. for i in range(64):
  32. ser.write(b'~b')
  33. write(i)
  34. writeLng(i*65536/64)
  35. writeLng(65536/64)
  36. time.sleep(0.2)
  37. """
  38. time.sleep(.5)
  39. for i in range(65535):
  40. for k in range(4):
  41. ser.write(b'~a')
  42. for n in range(4):
  43. for m in range(16):
  44. write(i+32+n*8-m*3)
  45. time.sleep(0.015 / 4)
  46. ser.write(b'~y')
  47. write(k)
  48. time.sleep(0.02)
  49. ser.write(b'~z')
  50. writeInt(i)
  51. time.sleep(0.02)
  52. ser.write(b'~s')
  53. # close stream
  54. """