Pyserial again

L

luca72

Hello

I have solve the problem of configuration with pyserial but i have
another question.
The serial port close every time i make the read and re-open when i do
i new write.
In my script there is not write close , why it close ?

Thaks Luca
 
G

Grant Edwards

I have solve the problem of configuration with pyserial but i
have another question. The serial port close every time i make
the read and re-open when i do i new write.

I doubt it.
In my script there is not write close, why it close ?

How on earth would we know what's wrong unless you post a small
example program that exhibits the problem?

If you want a WAG, here's one: the serial port object going
out-of-scope and getting garbage collected.

Please read this:

http://www.catb.org/~esr/faqs/smart-questions.html
 
P

Peter Hansen

luca72 said:
I have solve the problem of configuration with pyserial but i have
another question.
The serial port close every time i make the read and re-open when i do
i new write.

Please describe what you are observing that makes you say that the port
is closing.
In my script there is not write close , why it close ?

Shall we guess again?

-Peter
 
L

luca72

Hello here is a part of code, the values for settings comes from combo
box:

here i take the ATR of the smartcard inserted in the reader, and than i
store the read data in a textctrl.
Sniffing the data with serial port monitor i see that the answer is ok
but after the answer the port close, why?
Another point is the textctrl: it store only the first byte and not the
other why?, the byte are like 22 and all are readed

Thaks

ser = serial.Serial(porta)
ser.baudrate = (atr)
ser.parity = (parity)
ser.stopbits = (stop)
ser.bytesize = (size)
ser.setRTS(level = 0)
ser.setDTR(level = 0)
ser.timeout = (time)
a = ser.readline()
a = self.textCtrl3.SetValue(a + ' ')
self.lettura()
ser.setBaudrate(baud)
 
G

Grant Edwards

Hello here is a part of code,

No.

Wrong.

The "part of code" you posted tells us nothing. We can not
help you if you keep posting vague descriptions of what you
imagine is wrong and small, out-of-context chunks of irrelevant
code.

************************************************************
* *
* READ THIS: *
* *
* http://www.catb.org/~esr/faqs/smart-questions.html *
* *
************************************************************

You must post a small program that we can run. The whole
program. Not just random chunks that have nothing to do with
the problem.

You must tell us what you want that program to do.

You must tell us what you observe the program doing that you
think is wrong.
the values for settings comes from combo box:

here i take the ATR of the smartcard inserted in the reader,

I don't know what "ATR" is. I probably don't care.
and than i store the read data in a textctrl.

That's got nothing to do with pyserial.
Sniffing the data with serial port monitor i see that the
answer is ok but after the answer the port close, why?

What makes you think the port closed?
Another point is the textctrl: it store only the first byte
and not the other why?, the byte are like 22 and all are
readed

No clue. I don't know what "textctrl" is or what it's supposed
to do.

Your descriptions are useless. The code you post is irrelevant.

We're trying to help you. If you refuse to cooperate, there
isn't much we can do.

If you keep this up, the people who could help you will give up
and stop reading your posts.

************************************************************
* *
* READ THIS: *
* *
* http://www.catb.org/~esr/faqs/smart-questions.html *
* *
************************************************************
 
L

Luca

and than i store the read data in a textctrl.

That's got nothing to do with pyserial.

Ok i know but for this reason i put :

Another point is the textctrl: it store only the first byte and not
the
other why?, the byte are like 22 and all are readed
-------------------------------------------------------------------------------------------------------------------------------
No clue. I don't know what "textctrl" is or what it's supposed
to do.

a = ser.readline()
a = self.textCtrl3.SetValue(a + ' ')

For my bad understanding is clear

-------------------------------------------------------------------------------------------------------------------------------
What makes you think the port closed?

Sniffing the data with serial port monitor i see that the
answer is ok but after the answer the port close, why?

The serial port monitor write com port close.

----------------------------------------------------------------------------------------------------------


Dear Grant

As you know i'm not a programmer, but i try to do this in my free time.
Probably you have reason to write:

The code you post is irrelevant.

But i try to do my best as in my possibility.

I think that in any case you can't run my code if you haven't the same
device that i have.

So i don't want that you loose time with my stupid question, i'm sure
you have a lot of better things to do that help a beginner like me.

In any case thanks for your help, and you cordility.

Best Regards

Luca
 
S

Steve Holden

Luca said:
That's got nothing to do with pyserial.

Ok i know but for this reason i put :

Another point is the textctrl: it store only the first byte and not
the
other why?, the byte are like 22 and all are readed
-------------------------------------------------------------------------------------------------------------------------------
No clue. I don't know what "textctrl" is or what it's supposed
to do.

a = ser.readline()
a = self.textCtrl3.SetValue(a + ' ')

For my bad understanding is clear

-------------------------------------------------------------------------------------------------------------------------------
What makes you think the port closed?

Sniffing the data with serial port monitor i see that the



The serial port monitor write com port close.

----------------------------------------------------------------------------------------------------------


Dear Grant

As you know i'm not a programmer, but i try to do this in my free time.
Probably you have reason to write:

The code you post is irrelevant.

But i try to do my best as in my possibility.

I think that in any case you can't run my code if you haven't the same
device that i have.

So i don't want that you loose time with my stupid question, i'm sure
you have a lot of better things to do that help a beginner like me.

In any case thanks for your help, and you cordility.

Best Regards

Luca
Luca:

Grant was just pointing out that we can't tell you what's wrong with
your code if we can't see it.

Nobody expects you to be an experienced programmer. We just aren't
understanding you at the moment. You say "the port closes", but this
doesn't really have any standard meaning - can you not send data any more?

Please be patient and answer our questions. We *are* trying to help!

regards
Steve
 
G

Grant Edwards

a = ser.readline()
a = self.textCtrl3.SetValue(a + ' ')

For my bad understanding is clear

Please, please, quote articles so that we can tell who said
what. I've attempted to re-quote things to make it clear.

If textctrl isn't doing what you think it should, you need to

1) Create a _small_ program that demonstrates the problem.

2) POST THAT PROGRAM in a thread with a the subject line that
is likely to attract the attention of somebody who knows
something about "textctrl".
The serial port monitor write com port close.

OK. Good. That's useful information. I'm assuming that by
"serial port monitor" you mean something like the tool from
sysinternals? It's important that you include details like that.

In the previous post, I thought you meant you were watching the
serial data stream on the cable with a protocol analyzer or
with another PC. I didn't understand how you would know the
port had been closed by watching the data on the cable. You
said you were sniffing the _data_, and you can't tell by
looking at _data_ that the port has been closed -- so I had to
ask how you knew the port had been closed.

Now, if you'll just post the Python code we can probably tell
you what's wrong.

Since you haven't shown us the code, all we can do is guess.

I've already made my guess: the serial port object is going
out-of-scope and getting garbage collected. If you don't know
what that means, let us know, and we'll explain it in more
detail.
As you know i'm not a programmer, but i try to do this in my
free time.

That's fine. We're all willing to help, but you have to work
with us. The best/fastest way to solve your problem is to
write a small program that demonstrates the problem and post
it. Then we can look at it and tell you what's wrong.

You could post your entire program, but the more code you post,
less likely it is that people are going to sort through it to
figure out what's wrong.

If you can post a 15-20 line program that doesn't do what you
think it should, I guarantee you'll get a quick answer
explaining what you've done wrong.

If you post a 1500 line program, nobody is going to read it.

It's theoretically possible to post a precise enough natural
language (e.g. English) description of the problem such that
somebody might be able to figure out what's wrong. But,
writing a description that is precise enough is very difficult
even for somebody who is good at Python and a native English
speaker.

Its much easier to write a small program that demonstrates the
problem.
Probably you have reason to write:

The code you post is irrelevant.

But i try to do my best as in my possibility.

If you want us to tell you what's wrong with your code, you
_have_to_show_it_to_us_. It's usually pointless for us to try
to guess.

There is a big problem with just posting a few lines of code
out of a larger program: unless you know what's wrong, you
don't know which lines of code to post. If you don't know what
the problem is, you're probably posting the wrong chunk of
code.
I think that in any case you can't run my code if you haven't
the same device that i have.

Even if we can't run the code, we can still look at it.

Ideally, you should create a small program that demonstrates
your problem. Make it as small as you can. Leave out anything
that isn't related to the problem you're trying to solve.

If you refuse to post code that demonstrates your problem,
there's very little chance we'll correctly guess what you've
written.
So i don't want that you loose time with my stupid question,
i'm sure you have a lot of better things to do that help a
beginner like me.

We were all beginners at one time. Teaching people is rarely a
waste of time unless they don't want to be taught. But you
have to help us solve your problem.
 
P

Petr Jakes

Grant and Steve,
I am wowed and amazed how supportive and helpful you (and other people
in this group as well of course) are.
Thanks.
(sorry for OT)
Petr Jakes
 
L

luca72

Here is my code ; why after the readline the port close?

ser = serial.Serial(0)
ser.baudrate = 9600
ser.parity = serial.PARITY_ODD
ser.stopbits = serial.STOPBITS_TWO
ser.bytesize =serial.EIGHTBITS
ser.setRTS(level = 0)
ser.setDTR(level = 0)
ser.timeout = 1

ser.readline()

Why after this the port close.

Regards Luca
 
P

Peter Hansen

luca72 said:
Here is my code ; why after the readline the port close?

What is the line of code before the first one shown here?
ser = serial.Serial(0)
ser.baudrate = 9600
ser.parity = serial.PARITY_ODD
ser.stopbits = serial.STOPBITS_TWO
ser.bytesize =serial.EIGHTBITS
ser.setRTS(level = 0)
ser.setDTR(level = 0)
ser.timeout = 1

ser.readline()

Why after this the port close.

What's the next line of code after ser.readline()?

-Peter
 
L

luca72

def OnButton1Button(self, event):

ser = serial.Serial(0)
ser.baudrate = 9600
ser.parity = serial.PARITY_ODD
ser.stopbits = serial.STOPBITS_TWO
ser.bytesize =serial.EIGHTBITS
ser.setRTS(level = 0)
ser.setDTR(level = 0)
ser.timeout = 1
ser.readline()

no code before

Regards

Luca
 
G

Grant Edwards

Here is my code; why after the readline the port close?

ser = serial.Serial(0)
ser.baudrate = 9600
ser.parity = serial.PARITY_ODD
ser.stopbits = serial.STOPBITS_TWO
ser.bytesize =serial.EIGHTBITS
ser.setRTS(level = 0)
ser.setDTR(level = 0)
ser.timeout = 1

ser.readline()

Why after this the port close.

Because the "ser" object is never used after that point, so it
get's garbage collected and deleted.
 
S

Steve Holden

Grant said:
Because the "ser" object is never used after that point, so it
get's garbage collected and deleted.
????

The "ser" name is still bound to the serial object, surely?

regards
Steve
 
G

Grant Edwards

????

The "ser" name is still bound to the serial object, surely?

I don't see any references to ser after the call to ser.readline().

Do you? ;)

I was trying to make the point [again] that it's impossible to
to tell what's going on because the OP refuses to post enough
of his code to allow meaningful analysis.

He's been told at least three times in as many postings that he
needs to post a complete example that demonstrates the problem
he's trying to solve, but he refuses. I've explained multiple
times that the "excerpts" he's posting aren't sufficient. He
persists in posting small snippets of mostly-irrelevant code
with no context.

I give up.

I have a feeling I'm just annoying the mule at this point.
 
L

luca72

Hello Grant and Hello Steve.

Pls. don't kill me, but if you try the code above written you see that
after the read the port close.
My question is only: why it close without the close command?
 
P

Petr Jakes

1)
Peter Hansen was asking you for the code AFTER the "ser.readline()" not
BEFORE!! the code you have originally posted here

2)
the best way how to examine if your code works is to try to write some
simplest code as possible first (not buttons, not GUI etc..., just a
simple code)
(all this suggestions were already mentioned in one of your previous
postings, see http://tinyurl.com/zz3s5)

Regarding to your questions in this posting I think you can try
something like:

import serial
ser=serial.Serial(0, 9600, 8, "E", stopbits=2, timeout=1)
while 1:
print s.readline()

In your original code you have posted here, you read one line from the
serial port only. It means the code reads byte by byte from the serial
port till the "end of line" byte ('\n') is recognized. Than the reading
stops and after that the whole code is finished because no
other code lines after the ser.readline(). If no data present on the
serial port for longer than "timeout" defined, it will stop reading
serial port and the code finishes as well.

3) please note, you can define serial port as following as well which
is ,IMHO, more readable:

ser=serial.Serial(0, 9600, 8, "E", stopbits=2, timeout=1)

(it was recommended to you in above mentioned posting as well)

You can find plenty of examples on http://pyserial.sourceforge.net/ in
the "Examples" paragraph. Try to study and search before you ask.

HTH

Petr Jakes
 
L

luca72

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

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)
ser.baudrate = 9600
ser.parity = serial.PARITY_ODD
ser.stopbits = serial.STOPBITS_TWO
ser.bytesize =serial.EIGHTBITS
ser.setRTS(level = 0)
ser.setDTR(level = 0)
ser.timeout = 1
But with
a = textCtrl1GetValue() # i put here some different string
to send
ser.write (a)
b = ser.readline()
textCtrl2SetValue(b)

why at any read the serial close, is possible to prevent the closure of
the port?
 
L

luca72

Peter Hansen was asking you for the code AFTER the "ser.readline()"
not
BEFORE!! the code you have originally posted here

OK IS MY MISTAKE; EXCUSE.
I have read the example but if i ask is bucause i don't or maybe i
don't understand the example

Regaards

Luca
 
G

Grant Edwards

Hello Grant and Hello Steve.

Pls. don't kill me, but if you try the code above written you see that
after the read the port close.
My question is only: why it close without the close command?

I've answered that question TWICE.

The ser object is getting garbage collected. That closes the
port.
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top