'PySerial, How can I access a Port that is already open?
I'm trying to control a relay module on an arduino via a pyython App.
I created a Class Relays as it goes :
class Relays():
def __init__(self, port="COM5", baudrate = 9600, parity=serial.PARITY_NONE, stopbit=serial.STOPBITS_ONE, bytesize = serial.EIGHTBITS):
# self.commandlist=['?AV1\r','RT,ON\r','?RT\r']
self.port=port
self.baud=baudrate
self.parity=parity
self.stopbit=stopbit
self.bytesize=bytesize
self.ValveList = [['1','A'],['2','B'],['3','C'],['4','D'],['5','E'],['6','F'],['7','G'],['8','H']]
def open_port(self):
try :
self.ser=serial.Serial(port=self.port,baudrate=self.baud)
except:
pass
def close_port(self):
self.ser.close()
print('port is close')
def Open_valve(self, n_valve):
self.open_port()
consign= self.ValveList[n_valve-1][0]#
self.ser.write(consign.encode('utf8'))
So basically, in my app, I create an instance of the relay class : R=Relays() And this instance will be used by multiple other function to call the methods "Open_valve and "Close_valve"
My problem is when I want to create another instance of Relays() or when I restart my script (which means I need to recreate an instantce of Relays), the port is open so I cannot open it again.
I need to keep it open because if I don't, the relays will bee powered off, so I cannont close the port after each call of function. or use the syntax
with serial.Serial('COM5',9600) as ser:
Is there a way to find the serial port that is already open an use it ?
Thank you in advance, for your help !
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
