Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
qutech
python-TaborDriver
Commits
913021a7
Commit
913021a7
authored
Jan 04, 2017
by
Simon Sebastian Humpohl
Browse files
Encode str to ascii before sending.
Start raising exceptions. Behaviour in case of error needs more unification
parent
342d7451
Changes
2
Hide whitespace changes
Inline
Side-by-side
pyte/__init__.py
View file @
913021a7
...
...
@@ -5,4 +5,4 @@ if StrictVersion(pyvisa_version) >= StrictVersion('1.6'):
from
.pyte16
import
*
__all__
=
pyte16
.
__all__
else
:
raise
ImportError
(
'Uns
o
pported pyvisa version: {} (should be an "easy" fix)'
.
format
(
pyvisa_version
))
raise
ImportError
(
'Uns
u
pported pyvisa version: {} (should be an "easy" fix)'
.
format
(
pyvisa_version
))
pyte/pyte16.py
View file @
913021a7
...
...
@@ -14,7 +14,16 @@ __all__ = ['open_session', 'prompt_msg','make_bin_dat_header', 'get_visa_err_des
'write_raw_bin_dat'
,
'send_cmd'
,
'build_sine_wave'
,
'build_triangle_wave'
,
'build_square_wave'
,
'download_binary_data'
,
'download_binary_file'
,
'download_arbcon_wav_file'
,
'download_segment_lengths'
,
'download_sequencer_table'
,
'download_adv_seq_table'
,
'download_fast_pattern_table'
,
'download_linear_pattern_table'
,
'make_combined_wave'
]
'download_linear_pattern_table'
,
'make_combined_wave'
,
'PyteException'
]
class
PyteException
(
Exception
):
def
__init__
(
self
,
return_code
,
context
=
None
):
self
.
return_code
=
return_code
self
.
context
=
context
def
__repr__
(
self
):
return
'PyteExeption: '
+
get_visa_err_desc
(
self
.
return_code
)
+
'
\n
'
+
self
.
context
def
_list_udp_awg_instruments
():
'''
...
...
@@ -250,9 +259,9 @@ def _init_vi_inst(vi, timeout_msec=10000, read_buff_size_bytes=4096, write_buff_
if
vi
is
not
None
:
vi
.
timeout
=
int
(
timeout_msec
)
vi
.
visalib
.
set_buffer
(
vi
.
session
,
vc
.
VI_READ_BUF
,
int
(
read_buff_size_bytes
))
vi
.
__dict__
[
'read_buff_size'
]
=
read_buff_size_bytes
setattr
(
vi
,
'read_buff_size'
,
read_buff_size_bytes
)
vi
.
visalib
.
set_buffer
(
vi
.
session
,
vc
.
VI_WRITE_BUF
,
int
(
write_buff_size_bytes
))
vi
.
__dict__
[
'write_buff_size'
]
=
write_buff_size_bytes
setattr
(
vi
,
'write_buff_size'
,
write_buff_size_bytes
)
vi
.
read_termination
=
'
\n
'
vi
.
write_termination
=
'
\n
'
intf_type
=
vi
.
get_visa_attribute
(
vc
.
VI_ATTR_INTF_TYPE
)
...
...
@@ -370,6 +379,7 @@ def get_visa_err_desc(err_code):
return
desc
def
write_raw_string
(
vi
,
wr_str
):
'''Write raw string to device (no termination character is added)
...
...
@@ -377,27 +387,14 @@ def write_raw_string(vi, wr_str):
:param wr_str: the string to write.
:returns: written-bytes count.
'''
ret_count
=
0
count
=
0
try
:
ret_count
=
len
(
wr_str
)
p_dat
=
ctypes
.
cast
(
wr_str
,
ctypes
.
POINTER
(
ctypes
.
c_byte
))
ul_sz
=
ctypes
.
c_ulong
(
ret_count
)
p_ret
=
ctypes
.
cast
(
count
,
ctypes
.
POINTER
(
ctypes
.
c_ulong
))
err_code
=
vi
.
visalib
.
viWrite
(
vi
.
session
,
p_dat
,
ul_sz
,
p_ret
)
if
err_code
<
0
:
err_desc
=
get_visa_err_desc
(
err_code
)
wrn_msg
=
'write_raw_string(wr_str="{0}")={1} ({2})'
.
format
(
wr_str
,
err_code
,
err_desc
)
warnings
.
warn
(
wrn_msg
)
elif
count
<
0
:
ret_count
=
count
except
:
ret_count
=
min
(
count
,
-
1
)
wrn_msg
=
'write_raw_string(wr_str="{0}") failed
\n
{1}'
.
format
(
wr_str
,
sys
.
exc_info
())
warnings
.
warn
(
wrn_msg
)
return
ret_count
bin_data
=
wr_str
.
encode
(
'ascii'
)
if
isinstance
(
wr_str
,
str
)
else
wr_str
try
:
return
write_raw_bin_dat
(
vi
,
bin_data
,
len
(
bin_data
))
except
PyteException
as
exception
:
exception
.
context
+=
'
\n
write_raw_string(vi={vi}, wr_str={wr_str})'
.
format
(
vi
=
vi
,
wr_str
=
wr_str
)
raise
exception
def
write_raw_bin_dat
(
vi
,
bin_dat
,
dat_size
,
max_chunk_size
=
1024
):
"""Write raw binary data to device.
...
...
@@ -410,44 +407,23 @@ def write_raw_bin_dat(vi, bin_dat, dat_size, max_chunk_size = 1024):
:param max_chunk_size: maximal chunk-size (in bytes).
:returns: written-bytes count.
"""
ret_count
=
0
err_code
=
0
wr_offs
=
0
count
=
0
try
:
if
isinstance
(
bin_dat
,
np
.
ndarray
):
p_dat
=
bin_dat
.
ctypes
.
data_as
(
ctypes
.
POINTER
(
ctypes
.
c_byte
))
else
:
p_dat
=
ctypes
.
cast
(
bin_dat
,
ctypes
.
POINTER
(
ctypes
.
c_byte
))
p_cnt
=
ctypes
.
cast
(
count
,
ctypes
.
POINTER
(
ctypes
.
c_ulong
))
if
dat_size
<=
max_chunk_size
:
ul_sz
=
ctypes
.
c_ulong
(
dat_size
)
err_code
=
vi
.
visalib
.
viWrite
(
vi
.
session
,
p_dat
,
ul_sz
,
p_cnt
)
ret_count
=
dat_size
else
:
while
wr_offs
<
dat_size
:
chunk_sz
=
min
(
max_chunk_size
,
dat_size
-
wr_offs
)
ul_sz
=
ctypes
.
c_ulong
(
chunk_sz
)
ptr
=
ctypes
.
cast
(
ctypes
.
addressof
(
p_dat
.
contents
)
+
wr_offs
,
ctypes
.
POINTER
(
ctypes
.
c_byte
))
err_code
=
vi
.
visalib
.
viWrite
(
vi
.
session
,
ptr
,
ul_sz
,
p_cnt
)
if
count
<
0
or
err_code
<
0
:
ret_count
=
min
(
count
,
-
1
)
break
ret_count
=
ret_count
+
chunk_sz
wr_offs
=
wr_offs
+
chunk_sz
if
count
<
0
or
err_code
<
0
:
err_desc
=
get_visa_err_desc
(
err_code
)
wrn_msg
=
'write_raw_bin_dat(dat_size={0})={1}, wr_offs={2}, err_code={3} ({4})'
.
format
(
dat_size
,
count
,
err_code
,
err_desc
)
except
:
ret_count
=
min
(
ret_count
,
-
1
)
wrn_msg
=
'write_raw_bin_dat(dat_size={0}) failed
\n
{1}'
.
format
(
dat_size
,
sys
.
exc_info
())
warnings
.
warn
(
wrn_msg
)
return
ret_count
bin_dat
=
bin_dat
.
tobytes
()
if
isinstance
(
bin_dat
,
np
.
ndarray
)
else
bytes
(
bin_dat
)
assert
(
len
(
bin_dat
)
>=
dat_size
)
written_bytes
=
0
while
written_bytes
<
dat_size
:
to_send
=
min
(
max_chunk_size
,
dat_size
-
written_bytes
)
sent
,
status_code
=
vi
.
write_raw
(
bin_dat
[
written_bytes
:
written_bytes
+
to_send
])
if
status_code
<
0
:
raise
PyteException
(
status_code
,
'write_raw_bin_dat(vi={vi}, bin_dat={bin_dat}, dat_size={dat_size},
\
max_chunk_size={max_chunk_size}) already wrote {count} bytes'
.
format
(
vi
=
vi
,
bin_dat
=
bin_dat
,
dat_size
=
dat_size
,
count
=
written_bytes
,
max_chunk_size
=
max_chunk_size
))
assert
(
sent
==
to_send
)
written_bytes
+=
sent
return
written_bytes
def
send_cmd
(
vi
,
cmd_str
,
paranoia_level
=
1
):
'''Send (SCPI) Command to Instrument
...
...
@@ -489,7 +465,7 @@ def _pre_download_binary_data(vi, bin_dat_size=None):
max_chunk_size
=
4096
try
:
max_chunk_size
=
vi
.
__dict__
.
get
(
'write_buff_size'
,
default
=
max_chunk_size
)
max_chunk_size
=
vi
.
write_buff_size
if
hasattr
(
vi
,
'write_buff_size'
)
else
max_chunk_size
intf_type
=
vi
.
get_visa_attribute
(
vc
.
VI_ATTR_INTF_TYPE
)
if
intf_type
==
vc
.
VI_INTF_GPIB
:
_
=
vi
.
write
(
"*OPC?"
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment