Advertisement
metalx1000

Python Serial Connection - ODBII

Oct 6th, 2014
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import time
  3. import serial
  4.  
  5. # configure the serial connections (the parameters differs on the device you are connecting to)
  6. ser = serial.Serial(
  7. port='/dev/ttyUSB0',
  8. baudrate=38400,
  9. parity=serial.PARITY_ODD,
  10. stopbits=serial.STOPBITS_TWO,
  11. bytesize=serial.SEVENBITS
  12. )
  13.  
  14. #ser.open()
  15. #ser.isOpen()
  16.  
  17. print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
  18.  
  19. input=1
  20. while 1 :
  21. # get keyboard input
  22. input = raw_input(">> ")
  23. # Python 3 users
  24. # input = input(">> ")
  25. if input == 'exit':
  26. ser.close()
  27. exit()
  28. else:
  29. # send the character to the device
  30. # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
  31. ser.write(input + '\r\n')
  32. out = ''
  33. # let's wait one second before reading output (let's give device time to answer)
  34. time.sleep(1)
  35. while ser.inWaiting() > 0:
  36. out += ser.read(1)
  37.  
  38. if out != '':
  39. print ">>" + out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement