Srxon05

Python zeljo konju\

Nov 28th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. import serial
  2.  
  3. connected = False
  4.  
  5. locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3','/dev/ttyACM0']
  6.  
  7. for device in locations:
  8.     try:
  9.         print "Trying...",device
  10.         ser = serial.Serial(device, 9600)
  11.         break
  12.     except:
  13.         print "Failed to connect on",device
  14. while not connected:
  15.     serin = ser.read()
  16.     connected = True
  17.  
  18. ## open text file to store the current  
  19. text_file = open("Merenje.txt", 'r+')
  20. ## read serial data from arduino and
  21. ## write it to the text file 'position.txt'
  22. while 1:
  23.     if ser.inWaiting():
  24.         x=ser.read()
  25.         print(x)
  26.         text_file.write(x)
  27.         if x=="\n":
  28.              ## text_file.seek(0)
  29.              text_file.truncate()
  30.         text_file.flush()
  31. text_file.close()
  32. ser.close()
Add Comment
Please, Sign In to add comment