Using pyserial to interface with a power supply - python

I am using a RS-232 to USB converter to connect my Agilent E3640A to my laptop. I am trying to return the manufacture's name using the command "*IDN" as stated in their manual. When i try to read the line, I get an empty string. I am also just having trouble putting the device in remote mode. Here is the link to their manual : https://all-guidesbox.com/model/agilent-technologies/e3640a.html
It seems that my power supply is not properly connected to my PC according to Keysight Connection Helper. It produces this error code:
! VI_ERROR_TMO: A timeout occurred
Visa ErrorCode: 0xBFFF0015 (-1073807339)
! Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
My Code:
ser = serial.Serial()
ser.timeout = 20
ser.baudrate = 9600
ser.port = 'COM8'
print('Connected to ' + ser.name)
ser.open()
ser.reset_input_buffer()
ser.reset_output_buffer()
#ser.write('SYST:INT{RS232}'.encode('utf-8'))
ser.write(b'SYST:REM')
ser.flush()
ser.write('*IDN?'.encode('utf-8'))
ser.flush()
print(ser.readline())
Actual Output:
Connected to COM8
b''
Desired Output:
Connected to COM8
‘‘Agilent Technologies,E3640A,0,X.X-Y.Y-Z.Z’’

Related

Python, Data Received from RS232 COM1 port is returning b'\x05' and b'\x04'

I am doing an instrument integration where the instrument name is Horiba ES60. I am using an RS232 port for communicating for PC and instrument.
I have tested the PC and instrument connection through the Advance Serial Port logger and am getting the monitor result.
I have confirmed that the instrument setting and my script setting are the same.
I have written a simple script in python to read the port data. below is the script.
import time
import serial
sSerialPort = serial.Serial(port = "COM1", baudrate=9600,
bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE)
sSerialString = "" # Used to hold data coming over UART
print("Connected to : ",sSerialPort.name)
while(True):
# Wait until there is data waiting in the serial buffer
if(sSerialPort.in_waiting > 0):
# Read data out of the buffer until a carraige return / new line is found
sSerialString = sSerialPort.readline()
# Print the contents of the serial data
print(sSerialString)
Output:
b'\x05'
b'\x04'
The expected output is different and I am getting the above one.
Can someone please help to understand what's going wrong?
how to deal with port data in python.

Correctly connect to serial Python to avoid ftser2k.sys blue screen

So I'm using the pyserial module to manage connection between my PC and a device.
My code is:
port = 'COM3'
baud = 38400
while is_serial_not_connected:
try:
ser = serial.Serial(port, baud, timeout=None)
is_serial_not_connected = False
print("Serial Connected")
except serial.serialutil.SerialException:
logging.error("Can't find serial")
logging.info("We'll wait other 3 seconds")
time.sleep(3)
if not ser.isOpen():
ser.open()
ser.reset_input_buffer()
ser.reset_output_buffer()
Now, my device continue to stream data via the serial port and if I connect the device to my pc before or after launching my Python software Windows 10 goes in blue screen with error ftser2k.sys: PAGE_FAULT_IN_NONPAGED_AREA
If I have my software running, with the serial cable connected and I start the communication I got no error
I think the issue is that while I'm closing my Python software, the serial driver doesn't really close the communication and when I try to re-launch the software, it automatically starts receiving data saturating some sort of buffers
PS I'm using an USB-RS485-WE-1800-BT cable

Cannot write or read serial port using Laird USB dongle - Python

I cannot read or write data to the serial port using
import serial
ser = serial.Serial('COM4', 115200, timeout=0.5, parity=serial.PARITY_EVEN, rtscts=1) # open serial port
print(ser.portstr) # check which port was really used
ser.write(str.encode("hello")) # write a string
data = ser.readline() # read line from serial
print(data.decode('utf-8')) # print line / decoded
ser.close() # close port
I am using a Laird USB dongle which is a bluetooth USB adapter, right now it just stores a program that prints"Hello".
The output only returns "COM 4", I am expecting to see "Hello" also in the output.
I'm unsure if this is code related or could it be a hardware issue? Any insight is greatly appreciated!

serial communication with USB device using 'GNET' protocol

I have a USB device with the following specification. Page 22 describes the GNET protocol that should be used to interact with the device.
The connection is fine but the device just doesn't give me any response, so I think I am not sending the correct data to it, maybe missing the handshake?
From Specification
Support TTY (TELE TYPE) OPERATION - Use TTY to send commands and messages
Use ASCII value for each field and use Separator "," between two
Fields.
connect_and_send.py
import serial
port = "COM3"
baud = 9600
ser = serial.Serial(port, baud, timeout=1)
if ser.isOpen():
print(ser.name + ' is open...')
# STX, N, CR
to_send = b'\x02\x4e\x0d'
print "Sending {}".format(to_send)
ser.write(to_send)
out = ser.read()
print('Receiving...'+out)
COM3 is the correct port:
Any help and guidance would be greatly appreciated.
You havenever​ to use \x4e, this is the Negative Acknowledge from the device.
Try
to_send = b'\x02F\x0d'
to get Firmware Version
I contacted the supplier in the end and the issue was setting to wrong baud rate. Changing from 9600 to 19200 resolved the problem.

PySerial FileNotFoundError

I'm trying to use PySerial to accept inputs from an RFID Reader. As per the answers here: I've tried using WinObj and found something odd: there is no COM3 port in the GLOBAL??? folder pointing to something "more driver specific." However, when I run the command python -m serial.tools.list_ports, it does throw up COM3. When I try a simple program like:
import serial
ser = serial.Serial()
ser.port = 2
print(ser)
ser.open()
I get the following output:
Serial<id=0x45e8198, open=False>(port='COM3', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
serial.serialutil.SerialException: could not open port 'COM3': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)
So, I know that PySerial is looking for my reader in the right place, and, according to two different sources (the device manager and the command line), the device is registering. And yet I'm still getting this error. What is going on? I'm using Python 3.3 on Windows 8.1.
EDIT: That error is actually what I get from python's command line. The one I get from making and running a program like the one above is:
AttributeError: 'function' object has no attribute 'Serial.'
I'd appreciate thoughts on that too.
First thing I would check is what you have for com ports attached and what's currently in use:
import serial.tools.list_ports
import sys
list = serial.tools.list_ports.comports()
connected = []
for element in list:
connected.append(element.device)
print("Connected COM ports: " + str(connected))
# compliments of https://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python#14224477
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
# !attention assumes pyserial 3.x
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
print("Availible COM Ports: " + str(result))
Then, make sure you are calling the serial port constructor with the parameters you want:
ser = serial.Serial(
port="com2", # assumes pyserial 3.x, for 2.x use integer values
baudrate=19200,
bytesize=8,
parity="E", # options are: {N,E,O,S,M}
stopbits=1,
timeout=0.05)
When you call "serial.Serial()" without any parameters and then add the port ID, I'm not entirely sure what it's going to do, I've always explicitly referenced the port I want to use there.
Your issue lies in the fact that the serial object is looking for a string "COMXX" or else it won't work. I don't know if it need to be capitalized or not.
make sure you configure it like this.
serial.Serial(port = "COM2")

Categories