question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe

See original GitHub issue

Hi All,

I am trying to, a) connect to a signal generator (Agilent N5182A MXG) that supports ASCII range(256) using TCPIP. b) set a signal waveform to the signal generator

Unfortunately I am unable to set the waveform. Apparently the error shows that it supports only ASCII range(128) but not the extended ASCII range(129-256). The ‘0xfe’ comes in the extended ASCII set.

Have anyone faced this problem? Is there a syntax to specify the encoding method pyvisa should use?

The error,

SK@PC:~/ubuntu/pyvisa$ python sg_pyvisa_simple.py
Traceback (most recent call last):
  File "sg_pyvisa_simple.py", line 89, in <module>
    inst_sg.write(set_waveform)
  File "/opt/anaconda2/lib/python2.7/site-packages/pyvisa/resources/messagebased.py", line 202, in write
    if message.endswith(term):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 94: ordinal not in range(128)

Attaching a) The script that produced the above error

import visa
import numpy
import itertools
import pylab as pl
import math

#######################################
# Variables
#######################################

IP_SG = '10.10.100.131'
siggen_filename ='myCombSig'
num_tone=16
num_smpl_tone=32 

#######################################
# Initialization
#######################################

rm = visa.ResourceManager('@py')

#######################################
# Helper Functions
#######################################

def df_open_connection(rm, ip):
	inst = rm.open_resource('TCPIP::'+ ip + '::INSTR')
	return inst

def df_close_connection(inst):
	inst.close()

def df_my_range(start, end, step):
	while start <= end:
		yield start
		start += step

def df_Comb_sig_gen(num_tone, num_smpl_tone):
	comb_real = []
	comb_imag = []
	comb_cmpx = []
	M = num_tone * num_smpl_tone
	N = 0
	C = num_tone
	k = pl.frange(float(-(C - 1)) / 2, float((C - 1)) / 2, 1)
	for n in df_my_range(-N + 1, M + N - 2, 1):
		cc = numpy.exp(2j * math.pi * k * numpy.array(n) / (C))
		comb_cmpx.append(numpy.sum(cc))  

	return (comb_cmpx)

def df_set_waveform(comb_signal, filename):

	scale  = numpy.amax([numpy.amax(comb_signal.real),numpy.amax(comb_signal.imag)])

	comb_signal_real_scale = numpy.array(comb_signal.real)/scale
	comb_signal_imag_scale = numpy.array(comb_signal.imag)/scale

	i = comb_signal_real_scale
	q = comb_signal_imag_scale

	i_dac = map(lambda x: int((x+32767) % 32767), i)
	q_dac = map(lambda x: int((x+32767) % 32767), q)

	iq_str = ""

	for i_val, q_val in itertools.izip(i_dac, q_dac):
		iq_str += chr((i_val >> 8) & 0x00FF) #32766 (0x7FFE) -> 7F
		iq_str += chr(i_val & 0x00FF)		 #32766 (0x7FFE) -> FE, error when writing FE
		iq_str += chr((q_val >> 8) & 0x00FF)
		iq_str += chr(q_val & 0x00FF)

	scpi_command = ':MEM:DATA "WFM1:{}",#{}{}{}\n'.format(filename.upper(), len(str(len(iq_str))), str(len(iq_str)), iq_str)

	return scpi_command

#################################################
# Action: To Set a Waveform on Signal Generator
#################################################

inst_sg = df_open_connection(rm, IP_SG)
inst_sg.write(':FREQ 1.8825 GHz; POWer -30DBM')

comb_signal = df_Comb_sig_gen(float(num_tone),float(num_smpl_tone))	
comb_signal = numpy.array(comb_signal)

set_waveform = df_set_waveform(comb_signal, siggen_filename)

inst_sg.write(set_waveform)

df_close_connection(inst_sg)`

b) The output of “python -m visa info”

Machine Details:
   Platform ID:    Linux-4.4.0-92-generic-x86_64-with-debian-stretch-sid
   Processor:      x86_64

Python:
   Implementation: CPython
   Executable:     /opt/anaconda2/bin/python
   Version:        2.7.12
   Compiler:       GCC 4.4.7 20120313 (Red Hat 4.4.7-1)
   Bits:           64bit
   Build:          Jul  2 2016 17:42:40 (#default)
   Unicode:        UCS4

PyVISA Version: 1.8

Backends:
   ni:
      Version: 1.8 (bundled with PyVISA)
      #1: /usr/lib/x86_64-linux-gnu/libvisa.so.0.0.0:
         found by: auto
         bitness: 64
         Could not get more info:
            VI_ERROR_NSUP_ATTR (-1073807331): The specified attribute is not defined or supported by the referenced object.
   py:
      Version: 0.3.dev0
      ASRL INSTR: Available via PySerial (3.4)
      TCPIP INSTR: Available 
      USB RAW: Available via PyUSB (1.0.2). Backend: N/A
      USB INSTR: Available via PyUSB (1.0.2). Backend: N/A
      GPIB INSTR:
         Please install linux-gpib to use this resource type.
         No module named gpib
      TCPIP SOCKET: Available 
   sim:
      Version: 0.3
      Spec version: 1.1

If any other logs need to be produced to study the above issue, kindly share the syntax and I shall revert with the appropriate logs. Thanks for your help.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MatthieuDartiailhcommented, Jan 25, 2018

At least your message get through now.

0reactions
svanan77commented, Jan 25, 2018

Ok, the below works.

inst_sg.write_binary_values(':MEM:DATA "WFM1:{}",'.format(filename.upper()), iq_str, datatype='c')

Thanks Matthieu for your time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix: "UnicodeDecodeError: 'ascii' codec can't decode ...
UnicodeDecodeError : 'ascii' codec can't decode byte generally happens when you try to convert a Python 2.x str that contains non-ASCII to a ......
Read more >
UnicodeDecodeError: 'ascii' codec can't decode byte
The Python "UnicodeDecodeError: 'ascii' codec can't decode byte in position" occurs when we use the ascii codec to decode bytes that were encoded...
Read more >
UnicodeDecodeError: 'ascii' codec can't decode byte - GitHub
Questions and Help I am facing this issue , When I run the VQA colab notebook it works perfectly well , but when...
Read more >
UnicodeDecodeError: 'ascii' codec can't decode byte - Intellipaat
1 Answer. To fix “UnicodeDecodeError you can use the following piece of code this is the default encoding of python is utf8. After...
Read more >
Solving Unicode Problems in Python 2.7 - Azavea
UnicodeDecodeError : 'ascii' codec can't decode byte 0xd1 in position 1: ordinal not in range(128) (Why is this so hard??) · 1. str...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found