Pyserial again

G

Grant Edwards

Ok you write that it close :Because the "ser" object is never
used after that point, so it get's garbage collected and
deleted.
Yes.

But for example if i make one button with the same caracteristic of the
previous com port:

def OnButton1Button(self, event):

ser = serial.Serial(0)

But with
a = textCtrl1GetValue() # i put here some different string
to send
ser.write (a)
b = ser.readline()
textCtrl2SetValue(b)

I have no idea what you mean by that. POST THE CODE. With
both button handlers.

I'm GUESSING (since you won't post enough code for me to
actually figure it otu) that "ser" is local to your button
handlers and it's going out of scope and getting deleted when
the button handler returns.

Do you understand what I wrote above?
why at any read the serial close, is possible to prevent the
closure of the port?

Yes. Make sure the "ser" object doesn't get deleted.
 
D

Dennis Lee Bieber

def OnButton1Button(self, event):

ser = serial.Serial(0)

Well, that's the first useful bit of information. EVERY TIME you
activate that button, you are creating a NEW serial port connection.
And, when the handler exits, "ser" is garbage collected, because it
is local only to the button event handler. It doesn't matter that you
hadn't closed the port -- there is no "name" left that can be used to
manipulate the port, so the Python runtime says it is garbage and can be
shut down/deleted.


I have no idea what GUI system you are using so I'll skip that part,
but... Everything EXCEPT the "ser.readline()" needs to be done OUTSIDE
of the GUI logic. "ser" either needs to be made a global, or a component
of "self".


def onButton1Button(self, event):
global ser
data = ser.readline()
print data # for debug

....

if __name__ == "__main__":
ser = serial.Serial(port=0, baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.EIGHTBITS,
timeout=1)
ser.setRTS(0)
ser.setDTR(0)

#now set up and start the GUI mainloop

ser.close()
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top