title: Connecting via USB/Serial port to Newport CONEX-PP Motion Controller in Python
body: <p>I'm having trouble getting my Windows 7 laptop to talk to a <a href="http://www.newport.com/Conex-PP-CONEX-PP-Intelligent-Stepper-Motor-Contr/1042123/1033/info.aspx" rel="nofollow">Newport CONEX-PP motion controller</a>.  I've tried python (Spyder/Anaconda) and a serial port streaming program called Termite and in either case the results are the same: no response from the device.  The end goal is to communicate with the controller using python.</p>
  
  <p>The controller connects to my computer via a USB cable they sold me that is explicitly for use with this device.  The connector has a pair of lights that blink when the device receives data (red) or sends data (green).  There is also a packaged GUI program that comes with the device that seems to work fine.  I haven't tried every button, the ones I have tried have the expected result.</p>
  
  <p>The documentation for accessing this device is next to non-existant.  The CD in the box has one way to connect to it and the webpage linked above has a different way.  The first way (CD from the box) creates a hierarchy of modules that ends in a module it does not recognize (this is a code snippet provided by Newport):</p>
  
  <pre><code>import sys  
  sys.path.append(r'C:\Newport\MotionControl\CONEX-PP\Bin')  
  import clr  
  clr.AddReference("Newport.CONEXPP.CommandInterface")  
  from CommandInterfaceConexPP import *  
  import System  
  instrument="COM5"  
  print 'Instrument Key=&gt;', instrument  
  myPP = ConexPP()  
  ret = myPP.OpenInstrument(instrument)  
  print 'OpenInstrument =&gt; ', ret  
  result, response, errString = myPP.SR_Get(1)
  </code></pre>
  
  <p>That last line returns:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):</p>
    
    <p>File "&lt; ipython-input-2-5d824f156d8f >", line 2, in 
       result, response, errString = myPP.SR_Get(1)</p>
    
    <p>TypeError: No method matches given arguments</p>
  </blockquote>
  
  <p>I'm guessing this is because the various module references are screwy in some way.  But I don't know, I'm relatively new to python and the only time I have used it for serial communication the example files provided by the vendor simply worked.</p>
  
  <p>The second way to communicate with the controller is via the visa module (the CONEX_SMC_common module imports the visa module):</p>
  
  <pre><code>import sys  
  sys.path.append(r'C:\Newport\NewportPython')  
  class CONEX(CONEXSMC): def __init__(self):  
      super(CONEX,self).__init__() device_key = 'com5'  
      self.connect=self.rm.open_resource(device_key, baud_rate=57600, timeout=2000, data_bits=8, write_termination='\r\n',read_termination='\r\n')  
  mine.connect.read()  
  </code></pre>
  
  <p>That last mine.connect.read() command returns:</p>
  
  <blockquote>
    <p>VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.</p>
  </blockquote>
  
  <p>If, instead, I write to the port <code>mine.connect.write('VE')</code> the light on the connector flashes red as if it received some data and returns:</p>
  
  <blockquote>
    <p>(4L, &lt; StatusCode.success: 0 >)</p>
  </blockquote>
  
  <p>If I ask for the dictionary of the "mine" object <code>mine.__dict__</code>, I get:</p>
  
  <blockquote>
    <p>{'connect': &lt;'SerialInstrument'(u'ASRL5::INSTR')>,
    'device_key': u'ASRL5::INSTR',
    'list_of_devices': (u'ASRL5::INSTR',),
    'rm': )>}</p>
  </blockquote>
  
  <p>The ASRL5::INSTR resource for VISA is at least related to the controller, because when I unplug the device from the laptop it disappears and the GUI program will stop working.</p>
  
  <p>Maybe there is something simple I'm missing here.  I have NI VISA installed and I'm not just running with the DLL that comes from the website.  Oh, I found a Github question / answer <a href="https://github.com/hgrecco/pyvisa/issues/176" rel="nofollow">with this exact problem</a> but the end result makes no sense, the thread is closed after hgrecco tells him to use "open_resource" which is precisely what I am using.</p>
  
  <p>Results with Termite are the same, I can apparently connect to the controller and get the light to flash red, but it never responds, either through Termite or by performing the requested action.</p>
  
  <p>I've tried pySerial too:</p>
  
  <pre><code>import serial  
  ser = serial.Serial('com5')  
  ser.write('VE\r\n')  
  ser.read()  
  </code></pre>
  
  <p>Python just waits there forever, I assume because I haven't set a timeout limit.</p>
  
  <p>So, if anyone has any experience with this particular motion controller, Newport devices or with serial port communication in general and can shed some light on this problem I'd much appreciate it.  After about 7 hours on this I'm out of ideas.</p>