Advertisement
Guest User

Untitled

a guest
May 6th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import serial
  2. import struct
  3. import binascii
  4.  
  5. #Function to Initialize the Serial Port
  6. def init_serial():
  7.  
  8. global ser #Must be declared in Each Function
  9. ser = serial.Serial()
  10. ser.baudrate = 9600
  11.  
  12. ser.port = '/dev/ttyACM0' #com port of avr
  13.  
  14. ser.timeout = 10
  15. ser.open() #Opens SerialPort
  16. # print port open or closed
  17. if ser.isOpen():
  18. print 'Open: ' + ser.portstr
  19.  
  20. init_serial()
  21. values = (1)
  22. s = struct.Struct('I')
  23. packed_data = s.pack(*values)
  24.  
  25. ser.write(packed_data) #Writes to the SerialPort
  26.  
  27. while 1:
  28.  
  29. bytes = ser.readline() #Read from Serial Port
  30. packed_data = binascii.unhexlify(bytes)
  31. s = struct.Struct('I')
  32. unpacked_data = s.unpack(packed_data)
  33. print 'You sent: ', unpacked_data #Print What is Read from Port
  34.  
  35. #Ctrl+C to Close Python Window
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement