The error you're encountering is due to the fact that the value returned from the DMM is a string, and you're trying to format it as a float using the %.7f format specifier. To resolve this issue, you need to convert the string to a float before formatting it.
Here's an updated version of your code that converts the DMM value to a decimal number and formats it as a float:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import pyvisa
import time
rm = pyvisa.ResourceManager()
ps = rm.open_resource('ASRL8::INSTR')
ps.baud_rate = 115200
dmm = rm.open_resource('ASRL10::INSTR')
dmm.baud_rate = 115200
ps.write('VSET:1.0?')
ps.write('OUT:1')
ps.write('IOUT:1?')
time.sleep(3)
val = ps.query('IOUT?')
print("PS Current Reading:", val)
dmm.write('CONF:VOLTage:DC')
time.sleep(4)
number = float(dmm.query("MEAS?")) # Convert the DMM value to a float
print(number)
print("%.7f" % number) # Format the float value with 7 decimal places
rm.close()
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
By converting the DMM value to a float using float(dmm.query("MEAS?")), you can then format it as a float using the %.7f format specifier without encountering the TypeError.
Make sure that the DMM value you're receiving is a valid numerical value in scientific notation. If the DMM value contains any non-numeric characters or special characters, it may cause issues when converting it to a float.
Here's an updated version of your code that converts the DMM value to a decimal number and formats it as a float:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import pyvisa
import time
rm = pyvisa.ResourceManager()
ps = rm.open_resource('ASRL8::INSTR')
ps.baud_rate = 115200
dmm = rm.open_resource('ASRL10::INSTR')
dmm.baud_rate = 115200
ps.write('VSET:1.0?')
ps.write('OUT:1')
ps.write('IOUT:1?')
time.sleep(3)
val = ps.query('IOUT?')
print("PS Current Reading:", val)
dmm.write('CONF:VOLTage:DC')
time.sleep(4)
number = float(dmm.query("MEAS?")) # Convert the DMM value to a float
print(number)
print("%.7f" % number) # Format the float value with 7 decimal places
rm.close()
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
By converting the DMM value to a float using float(dmm.query("MEAS?")), you can then format it as a float using the %.7f format specifier without encountering the TypeError.
Make sure that the DMM value you're receiving is a valid numerical value in scientific notation. If the DMM value contains any non-numeric characters or special characters, it may cause issues when converting it to a float.
buran write Jun-06-2023, 06:20 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.