#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright 2012-2017 Frédéric Magniette, Miguel Rubio-Roy
# This file is part of Pyrame.
#
# Pyrame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrame is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrame. If not, see <http://www.gnu.org/licenses/>
from re import sub,search
import base64
import pools,vicp
ly_lt344_pool=pools.pool()
links={}
# Available channels
valid_channels=["M1","M2","M3","M4","C1","C2","C3","C4","TA","TB","TC","TD"]
# Functions
[docs]def init_ly_lt344(ly_lt344_id,dso_hostname):
"""Initialize ly_lt344 DSO. *dso_hostname* is the IP address or resolvable network name"""
global links
# Initialize TCP link
link=vicp.device()
link.deviceAddress=dso_hostname
if not link.connect():
return 0,"Error initializing TCP link <- %s"%(link.LastErrorMsg)
# Turn on error logging
if not link.write("CHLP EO\n",True,True):
link.disconnect()
return 0,"Error setting up error logging <- VICP error"
# Turn on headers in responses
if not link.write("CHDR SHORT\n"):
return 0,"Error turning on headers in responses <- VICP error"
ly_lt344_pool.new(ly_lt344_id)
links[ly_lt344_id]=link
return 1,"ok"
[docs]def deinit_ly_lt344(ly_lt344_id):
"Deregister an ly_lt344 from the pool"
global links
try:
ly_lt344=ly_lt344_pool.get(ly_lt344_id)
except Exception as e:
return 1,str(e)
# Deinitialize TCP link
if not links[ly_lt344_id].disconnect():
return 0,"Error deinitializing TCP link %s <- VICP error"%(links[ly_lt344_id].LastErrorMsg)
# Remove ly_lt344 from the pool
ly_lt344_pool.remove(ly_lt344_id)
return 1,"ok"
[docs]def get_data_ly_lt344(ly_lt344_id,sparsing,channel):
"""Get waveform of *channel*. *channel* can be 'C1', 'C2', 'C3', 'C4', 'M1', 'M2', 'M3', 'M4', 'TA', 'TB', 'TC' or 'TD', or a comma-separated list of channels. e.g.: C1,C2,TA. Data is sent back in base64. The *sparsing* parameter defines the interval between data points. *sparsing* =4 gets every 4th datapoint."""
global links
try:
ly_lt344=ly_lt344_pool.get(ly_lt344_id)
except Exception as e:
return 0,str(e)
# Expand channels
channels=list(set(channel.split(",")))
# Verify consistency of parameters
for channel in channels:
if channel not in valid_channels:
return 0,"invalid channel %s"%(channel)
# Send the command
if not links[ly_lt344_id].write("",True,True):
return 0,"Error flushing queue <- VICP error"
if not links[ly_lt344_id].write("*CLS; INE 1; *SRE 1\n"):
return 0,"Error asking for SRQ <- VICP error"
retcode,res=links[ly_lt344_id].serialPoll()
if retcode == 1 and res == '1':
output=""
for channel in channels:
# Turn off headers in responses
if not links[ly_lt344_id].write("CHDR OFF\n"):
return 0,"Error turning off headers in responses <- VICP error"
# Set datapoints to be recovered
if not links[ly_lt344_id].write("WFSU SP,{0},NP,0,SN,0,FP,0\n".format(sparsing)):
return 0,"Error turning off headers in responses <- VICP error"
# Get waveform
retcode,res=links[ly_lt344_id].wrnrd("{0}:INSP? \"SIMPLE\",FLOAT\n".format(channel))
if retcode == 0:
return 0,"Error getting the data <- %s"%(res)
# Clean up
wave=sub(r'[ \n\r]+','\n',res).strip("\"\n")
# Get wave descriptor
retcode,res=links[ly_lt344_id].wrnrd("{0}:INSP? \"WAVEDESC\"\n".format(channel))
if retcode == 0:
return 0,"Error getting the vertical scale <- %s"%(res)
wavedesc=res
# Extract vertical gain
v_gain=float(search(r"VERTICAL_GAIN *: *([0-9\.e+-]+)",wavedesc).expand(r"\1"))
# Extract vertical offset
v_offset=float(search(r"VERTICAL_OFFSET *: *([0-9\.e+-]+)",wavedesc).expand(r"\1"))
# Extract horizontal step
h_step=float(search(r"HORIZ_INTERVAL *: *([0-9\.e+-]+)",wavedesc).expand(r"\1"))
# Extract horizontal offset
h_offset=float(search(r"HORIZ_OFFSET *: *([0-9\.e+-]+)",wavedesc).expand(r"\1"))
# Extract sparsing factor
sparsing=float(search(r"SPARSING_FACTOR *: *([0-9\.e+-]+)",wavedesc).expand(r"\1"))
# Turn on headers in responses again
if not links[ly_lt344_id].write("CHDR SHORT\n"):
return 0,"Error turning on headers in responses <- VICP error"
# Rescale and add abscises
x=0
y=0
wave=wave.split("\n")
for i in range(len(wave)):
x=i*h_step*sparsing + h_offset
y=float(wave[i])
#y=float(wave[i])*v_gain - v_offset
output+="%f %f\n"%(x,y)
# Separate channels with two newlines
output+="\n\n"
return 1,base64.b64encode(output)
return 0,"Error getting ready state from DSO (serialPoll) <- %s"%(links[ly_lt344_id].LastErrorMsg)
[docs]def clear_sweep_ly_lt344(ly_lt344_id):
"Clear sweep on DSO"
global links
try:
ly_lt344=ly_lt344_pool.get(ly_lt344_id)
except Exception as e:
return 0,str(e)
if not links[ly_lt344_id].write("CLSW\n"):
return 0,"Error clearing sweep <- %s"%(res)
return 1,"ok"
[docs]def get_error_queue_ly_lt344(ly_lt344_id):
"Read error queue"
global links
try:
ly_lt344=ly_lt344_pool.get(ly_lt344_id)
except Exception as e:
return 0,str(e)
return get_error_queue(links[ly_lt344_id])
def get_error_queue(link):
retcode,res=link.wrnrd("CHL? CLR\n")
return retcode,res.replace("\n","").replace("\r","")