Don't like ads? PRO users don't see any ads ;-)
Guest

Petr, Steve, QsB

By: a guest on Apr 26th, 2012  |  syntax: Python  |  size: 1.24 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import serial
  2. import sys
  3. import time
  4.                        
  5. def testEncoder2():
  6.     try:
  7.         s = serial.serial_for_url('COM4', baudrate=230400, parity=serial.PARITY_NONE,
  8.                 rtscts=False, dsrdtr=False, xonxoff=False, timeout=1)
  9.     except AttributeError:
  10.         # happens when the installed pyserial is older than 2.5. use the
  11.         # Serial class directly then.
  12.         s = serial.Serial('COM4', 230400, parity=serial.PARITY_NONE, rtscts=False,
  13.                           xonxoff=False, timeout=1)
  14.     except serial.SerialException as (errStr):
  15.         sys.stderr.write("Couldn't open the port %r: %s\n" %
  16.                          ('COM4', errStr))
  17.    
  18.     s.write("W151\r\n")
  19.    
  20.     s.write("W0B1\r\n")
  21.    
  22.     s.write("W0C0\r\n")
  23.    
  24.     s.write("W0240\r\n")
  25.    
  26.     time.sleep(0.1)
  27.    
  28.     s.readline()
  29.    
  30.     s.write("S05\r\n")
  31.    
  32.     while True:
  33.         if (s.inWaiting() > 0):
  34.             break
  35.     response = s.readline()
  36.     print response
  37.    
  38.     while True:
  39.         time.sleep(0.3)
  40.         if (s.inWaiting() > 0):
  41.             response = s.readline()
  42.             print response
  43.        
  44.     s.write("W150B\r\n")
  45.    
  46.     s.close()    
  47.                          
  48. testEncoder2()