I’am preparing two scripts: one with class to communicate with DC Power Supply, and second to manage the communication.
dcom.py:
import socket
class supply:
def __init__(self, IP, PORT, BUFFER_SIZE, TIMEOUT_SECONDS):
self.IP = IP
self.PORT = PORT
self.BUFFER_SIZE = BUFFER_SIZE
self.TIMEOUT_SECONDS = TIMEOUT_SECONDS
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def opensocket(self):
supply.connection.connect((supply.IP, supply.PORT))
supply.connection.settimeout(supply.TIMEOUT_SECONDS)
def sendAndReceiveCommand(self, msg):
msg = msg + 'n'
supply.connection.sendall(msg.encode('UTF-8'))
return supply.connection.recv(supply.BUFFER_SIZE).decode('UTF-8').rstrip()
def sendCommand(self, msg):
msg = msg + 'n'
supply.connection.sendall(msg.encode('UTF-8'))
def initialize(self):
return supply.sendAndReceiveCommand('*IDN?')
def readVoltage(self):
return supply.sendAndReceiveCommand('SOUR:VOLT?')
def readCurrent(self):
return supply.sendAndReceiveCommand('SOUR:CUR?')
main_comm.py:
import dcom
SUPPLY_IP = '10.41.32.2'
SUPPLY_PORT = 8462
BUFFER_SIZE = 128
TIMEOUT_SECONDS = 10
supply = dcom.supply(SUPPLY_IP, SUPPLY_PORT, BUFFER_SIZE, TIMEOUT_SECONDS)
print(supply.connection)
supply.opensocket()
When I run the main_comm.py the result is:
<socket.socket fd=400, family=2, type=1, proto=0>
Traceback (most recent call last):
File “C:UsersRyszardDocumentsWroblewskiWDC_supply_commmain_comm.py”, line 11, in <module>
supply.opensocket()
File “C:UsersRyszardDocumentsWroblewskiWDC_supply_commdcom.py”, line 15, in opensocket
supply.connection.connect((supply.IP, supply.PORT))
^^^^^^^^^^^^^^^^^
AttributeError: type object ‘supply’ has no attribute ‘connection’
As you can see, first I can print the object attribute “connection” value, what I suppose means that this attribute exists. However, later I get an error that this object has no such attribute.
Do you have any idea how to fix it?
Didik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2