Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11844 > unrolled thread

How to make statements running in strictly sequential fashion like in an interactive shell

Started bylzlu123 <lzlu123@gmail.com>
First post2011-08-19 08:00 -0700
Last post2011-08-22 07:33 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  How to make statements running in strictly sequential fashion like in an interactive shell lzlu123 <lzlu123@gmail.com> - 2011-08-19 08:00 -0700
    Re: How to make statements running in strictly sequential fashion like in an interactive shell aspineux <aspineux@gmail.com> - 2011-08-19 15:38 -0700
      Re: How to make statements running in strictly sequential fashion like in an interactive shell lzlu123 <lzlu123@gmail.com> - 2011-08-22 07:31 -0700
    Re: How to make statements running in strictly sequential fashion like in an interactive shell Javier <nospam@nospam.com> - 2011-08-20 03:06 +0000
      Re: How to make statements running in strictly sequential fashion like in an interactive shell lzlu123 <lzlu123@gmail.com> - 2011-08-22 07:33 -0700

#11844 — How to make statements running in strictly sequential fashion like in an interactive shell

Fromlzlu123 <lzlu123@gmail.com>
Date2011-08-19 08:00 -0700
SubjectHow to make statements running in strictly sequential fashion like in an interactive shell
Message-ID<6a83366f-15e6-4cb3-9e63-27103ebf7422@b20g2000vbz.googlegroups.com>
I have an instrument that has a RS232 type serial comm port and I need
to connect to and control. I use Python 3.2 in Windows XP, plus
pySerial module. I have a problem when I execute a script consisting
of statements that open the comm port, configure it, write strings to
and receive strings from it. Thoese strings aer either commands
pertinent to the instrument (control) or responses from the instrument
(response).

When those statements are executed in a python interpreter
interactively (at >>>), I get what I expect and the results are good
and correct. However, when I execute the script, either being invoked
within the interpreter or run file, I don’t get what I want. The
statements in the script is the same as what I use in the interactive
interpreter.

Why do I get the strange behavior and how can I change the script to
make it to behave like in interactive interpreter?

----------------------script-----------------------
def read(comport):

    wrt_str=b'movt 3000'+b'\r\n'
    ret_str=comport.write(wrt_str)

    wrt_str=b'scan'+b'\r\n'
    ret_str=comport.write(wrt_str)

    rsp_str=comport.readlines() #########1

    wrt_str=b'hllo'+b'\r\n'
    ret_str=comport.write(wrt_str)

    rsp_str=comport.readlines()#########2
----------------------------------------------------------

The problem is with the lines above with #######. In interactive mode,
there is about 1 second delay at #1, and 9 seonds delay at #2. I get
correct responses there. However, if I execute the script above, there
is no delay at all and I get incorrect results (garbage). I set the
read timeout to 0 in comm port set up, as

comport.timeout=0
So the comport should be in blocking mode if it waits for the end of
line or end of file.

I tried many things, like exec (execfile in 2.7), but at no avail.

I have an update to the original post I made a few days ago. I think I
know what the problem is and want to know if anyone has a solution:

After putting "print" and "time.sleep(delay)" after every statement, I
found when the script is running, it appears going around the pyserial
statement, such as "comport.write(..)" or "comport.readlines(...)"
while the pyserial command is executing (appearing as waiting and
busying doing some thing, you know serial port is slow). So for
example, when I exec all statements in a python interactive shell, I
am not able to type and run a new statement if the previous one is not
returned. Let's ay, if comport.readlines() is not returning, I can't
type and run the next comport.write(...) statemtn. However, in a
script that is running, if the comport.readlines() is busy reading,
the next statement is running, if the next statement happens to be a
comport.write() which will abort the reading.

Is there any way to force the python script to behave like running
exactly sequentially?

[toc] | [next] | [standalone]


#11891

Fromaspineux <aspineux@gmail.com>
Date2011-08-19 15:38 -0700
Message-ID<7849d9ff-f472-49c3-a1ce-0f618a301f06@e7g2000vbw.googlegroups.com>
In reply to#11844
On Aug 19, 5:00 pm, lzlu123 <lzlu...@gmail.com> wrote:
> I have an instrument that has a RS232 type serial comm port and I need
> to connect to and control. I use Python 3.2 in Windows XP, plus
> pySerial module. I have a problem when I execute a script consisting
> of statements that open the comm port, configure it, write strings to
> and receive strings from it. Thoese strings aer either commands
> pertinent to the instrument (control) or responses from the instrument
> (response).
>
> When those statements are executed in a python interpreter
> interactively (at >>>), I get what I expect and the results are good
> and correct. However, when I execute the script, either being invoked
> within the interpreter or run file, I don’t get what I want. The
> statements in the script is the same as what I use in the interactive
> interpreter.
>
> Why do I get the strange behavior and how can I change the script to
> make it to behave like in interactive interpreter?
>
> ----------------------script-----------------------
> def read(comport):
>
>     wrt_str=b'movt 3000'+b'\r\n'
>     ret_str=comport.write(wrt_str)
>
>     wrt_str=b'scan'+b'\r\n'
>     ret_str=comport.write(wrt_str)
>
>     rsp_str=comport.readlines() #########1


You use readlines() with a s at the end !

* Note that when the serial port was opened with no timeout, that
readline()
* blocks until it sees a newline (or the specified size is reached)
* and that readlines() would never return and therefore refuses to
work
* (it raises an exception in this case)!

>
>     wrt_str=b'hllo'+b'\r\n'
>     ret_str=comport.write(wrt_str)
>
>     rsp_str=comport.readlines()#########2
> ----------------------------------------------------------
>
> The problem is with the lines above with #######. In interactive mode,
> there is about 1 second delay at #1, and 9 seonds delay at #2. I get
> correct responses there. However, if I execute the script above, there
> is no delay at all and I get incorrect results (garbage). I set the
> read timeout to 0 in comm port set up, as
>
> comport.timeout=0
> So the comport should be in blocking mode if it waits for the end of
> line or end of file.

Wrong :

    timeout = None: wait forever
    timeout = 0: non-blocking mode (return immediately on read)
    timeout = x: set timeout to x seconds (float allowed)

>
> I tried many things, like exec (execfile in 2.7), but at no avail.
>
> I have an update to the original post I made a few days ago. I think I
> know what the problem is and want to know if anyone has a solution:
>
> After putting "print" and "time.sleep(delay)" after every statement, I
> found when the script is running, it appears going around the pyserial
> statement, such as "comport.write(..)" or "comport.readlines(...)"
> while the pyserial command is executing (appearing as waiting and
> busying doing some thing, you know serial port is slow). So for
> example, when I exec all statements in a python interactive shell, I
> am not able to type and run a new statement if the previous one is not
> returned. Let's ay, if comport.readlines() is not returning, I can't
> type and run the next comport.write(...) statemtn. However, in a
> script that is running, if the comport.readlines() is busy reading,
> the next statement is running, if the next statement happens to be a
> comport.write() which will abort the reading.
>
> Is there any way to force the python script to behave like running
> exactly sequentially?


You have some new things to try

[toc] | [prev] | [next] | [standalone]


#12031

Fromlzlu123 <lzlu123@gmail.com>
Date2011-08-22 07:31 -0700
Message-ID<9ceeb1ea-f896-4e64-bad7-b544c329bc0a@k15g2000yqd.googlegroups.com>
In reply to#11891
On Aug 19, 6:38 pm, aspineux <aspin...@gmail.com> wrote:
> On Aug 19, 5:00 pm, lzlu123 <lzlu...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > I have an instrument that has a RS232 type serial comm port and I need
> > to connect to and control. I use Python 3.2 in Windows XP, plus
> > pySerial module. I have a problem when I execute a script consisting
> > of statements that open the comm port, configure it, write strings to
> > and receive strings from it. Thoese strings aer either commands
> > pertinent to the instrument (control) or responses from the instrument
> > (response).
>
> > When those statements are executed in a python interpreter
> > interactively (at >>>), I get what I expect and the results are good
> > and correct. However, when I execute the script, either being invoked
> > within the interpreter or run file, I don’t get what I want. The
> > statements in the script is the same as what I use in the interactive
> > interpreter.
>
> > Why do I get the strange behavior and how can I change the script to
> > make it to behave like in interactive interpreter?
>
> > ----------------------script-----------------------
> > def read(comport):
>
> >     wrt_str=b'movt 3000'+b'\r\n'
> >     ret_str=comport.write(wrt_str)
>
> >     wrt_str=b'scan'+b'\r\n'
> >     ret_str=comport.write(wrt_str)
>
> >     rsp_str=comport.readlines() #########1
>
> You use readlines() with a s at the end !
>
> * Note that when the serial port was opened with no timeout, that
> readline()
> * blocks until it sees a newline (or the specified size is reached)
> * and that readlines() would never return and therefore refuses to
> work
> * (it raises an exception in this case)!
>
>
>
>
>
>
>
>
>
>
>
> >     wrt_str=b'hllo'+b'\r\n'
> >     ret_str=comport.write(wrt_str)
>
> >     rsp_str=comport.readlines()#########2
> > ----------------------------------------------------------
>
> > The problem is with the lines above with #######. In interactive mode,
> > there is about 1 second delay at #1, and 9 seonds delay at #2. I get
> > correct responses there. However, if I execute the script above, there
> > is no delay at all and I get incorrect results (garbage). I set the
> > read timeout to 0 in comm port set up, as
>
> > comport.timeout=0
> > So the comport should be in blocking mode if it waits for the end of
> > line or end of file.
>
> Wrong :
>
>     timeout = None: wait forever
>     timeout = 0: non-blocking mode (return immediately on read)
>     timeout = x: set timeout to x seconds (float allowed)
>
>
>
>
>
>
>
>
>
>
>
> > I tried many things, like exec (execfile in 2.7), but at no avail.
>
> > I have an update to the original post I made a few days ago. I think I
> > know what the problem is and want to know if anyone has a solution:
>
> > After putting "print" and "time.sleep(delay)" after every statement, I
> > found when the script is running, it appears going around the pyserial
> > statement, such as "comport.write(..)" or "comport.readlines(...)"
> > while the pyserial command is executing (appearing as waiting and
> > busying doing some thing, you know serial port is slow). So for
> > example, when I exec all statements in a python interactive shell, I
> > am not able to type and run a new statement if the previous one is not
> > returned. Let's ay, if comport.readlines() is not returning, I can't
> > type and run the next comport.write(...) statemtn. However, in a
> > script that is running, if the comport.readlines() is busy reading,
> > the next statement is running, if the next statement happens to be a
> > comport.write() which will abort the reading.
>
> > Is there any way to force the python script to behave like running
> > exactly sequentially?
>
> You have some new things to try

I tried to set the timeout to different values other than 0, but still
got same result -- ruuning script directly  behaves differently from
executing statements in interactive shell.  I even tried using read
instead of readline(s).

[toc] | [prev] | [next] | [standalone]


#11895

FromJavier <nospam@nospam.com>
Date2011-08-20 03:06 +0000
Message-ID<j2n8b3$30l$1@speranza.aioe.org>
In reply to#11844
Never used it, but I think you can try this:

Pexpect - a Pure Python Expect-like module
Pexpect is a pure Python Expect-like module. Pexpect makes Python...
www.noah.org/python/pexpect/ 




lzlu123 <lzlu123@gmail.com> wrote:
> I have an instrument that has a RS232 type serial comm port and I need
> to connect to and control. I use Python 3.2 in Windows XP, plus
> pySerial module. I have a problem when I execute a script consisting
> of statements that open the comm port, configure it, write strings to
> and receive strings from it. Thoese strings aer either commands
> pertinent to the instrument (control) or responses from the instrument
> (response).
> 
> When those statements are executed in a python interpreter
> interactively (at >>>), I get what I expect and the results are good
> and correct. However, when I execute the script, either being invoked
> within the interpreter or run file, I don???t get what I want. The
> statements in the script is the same as what I use in the interactive
> interpreter.
> 
> Why do I get the strange behavior and how can I change the script to
> make it to behave like in interactive interpreter?
> 
> ----------------------script-----------------------
> def read(comport):
> 
>    wrt_str=b'movt 3000'+b'\r\n'
>    ret_str=comport.write(wrt_str)
> 
>    wrt_str=b'scan'+b'\r\n'
>    ret_str=comport.write(wrt_str)
> 
>    rsp_str=comport.readlines() #########1
> 
>    wrt_str=b'hllo'+b'\r\n'
>    ret_str=comport.write(wrt_str)
> 
>    rsp_str=comport.readlines()#########2
> ----------------------------------------------------------
> 
> The problem is with the lines above with #######. In interactive mode,
> there is about 1 second delay at #1, and 9 seonds delay at #2. I get
> correct responses there. However, if I execute the script above, there
> is no delay at all and I get incorrect results (garbage). I set the
> read timeout to 0 in comm port set up, as
> 
> comport.timeout=0
> So the comport should be in blocking mode if it waits for the end of
> line or end of file.
> 
> I tried many things, like exec (execfile in 2.7), but at no avail.
> 
> I have an update to the original post I made a few days ago. I think I
> know what the problem is and want to know if anyone has a solution:
> 
> After putting "print" and "time.sleep(delay)" after every statement, I
> found when the script is running, it appears going around the pyserial
> statement, such as "comport.write(..)" or "comport.readlines(...)"
> while the pyserial command is executing (appearing as waiting and
> busying doing some thing, you know serial port is slow). So for
> example, when I exec all statements in a python interactive shell, I
> am not able to type and run a new statement if the previous one is not
> returned. Let's ay, if comport.readlines() is not returning, I can't
> type and run the next comport.write(...) statemtn. However, in a
> script that is running, if the comport.readlines() is busy reading,
> the next statement is running, if the next statement happens to be a
> comport.write() which will abort the reading.
> 
> Is there any way to force the python script to behave like running
> exactly sequentially?

[toc] | [prev] | [next] | [standalone]


#12032

Fromlzlu123 <lzlu123@gmail.com>
Date2011-08-22 07:33 -0700
Message-ID<2f3f84da-6f21-48a6-9f2d-ba0c916dbdde@f41g2000yqh.googlegroups.com>
In reply to#11895
On Aug 19, 11:06 pm, Javier <nos...@nospam.com> wrote:
> Never used it, but I think you can try this:
>
> Pexpect - a Pure Python Expect-like module
> Pexpect is a pure Python Expect-like module. Pexpect makes Python...www.noah.org/python/pexpect/
>
>
>
>
>
>
>
> lzlu123 <lzlu...@gmail.com> wrote:
> > I have an instrument that has a RS232 type serial comm port and I need
> > to connect to and control. I use Python 3.2 in Windows XP, plus
> > pySerial module. I have a problem when I execute a script consisting
> > of statements that open the comm port, configure it, write strings to
> > and receive strings from it. Thoese strings aer either commands
> > pertinent to the instrument (control) or responses from the instrument
> > (response).
>
> > When those statements are executed in a python interpreter
> > interactively (at >>>), I get what I expect and the results are good
> > and correct. However, when I execute the script, either being invoked
> > within the interpreter or run file, I don???t get what I want. The
> > statements in the script is the same as what I use in the interactive
> > interpreter.
>
> > Why do I get the strange behavior and how can I change the script to
> > make it to behave like in interactive interpreter?
>
> > ----------------------script-----------------------
> > def read(comport):
>
> >    wrt_str=b'movt 3000'+b'\r\n'
> >    ret_str=comport.write(wrt_str)
>
> >    wrt_str=b'scan'+b'\r\n'
> >    ret_str=comport.write(wrt_str)
>
> >    rsp_str=comport.readlines() #########1
>
> >    wrt_str=b'hllo'+b'\r\n'
> >    ret_str=comport.write(wrt_str)
>
> >    rsp_str=comport.readlines()#########2
> > ----------------------------------------------------------
>
> > The problem is with the lines above with #######. In interactive mode,
> > there is about 1 second delay at #1, and 9 seonds delay at #2. I get
> > correct responses there. However, if I execute the script above, there
> > is no delay at all and I get incorrect results (garbage). I set the
> > read timeout to 0 in comm port set up, as
>
> > comport.timeout=0
> > So the comport should be in blocking mode if it waits for the end of
> > line or end of file.
>
> > I tried many things, like exec (execfile in 2.7), but at no avail.
>
> > I have an update to the original post I made a few days ago. I think I
> > know what the problem is and want to know if anyone has a solution:
>
> > After putting "print" and "time.sleep(delay)" after every statement, I
> > found when the script is running, it appears going around the pyserial
> > statement, such as "comport.write(..)" or "comport.readlines(...)"
> > while the pyserial command is executing (appearing as waiting and
> > busying doing some thing, you know serial port is slow). So for
> > example, when I exec all statements in a python interactive shell, I
> > am not able to type and run a new statement if the previous one is not
> > returned. Let's ay, if comport.readlines() is not returning, I can't
> > type and run the next comport.write(...) statemtn. However, in a
> > script that is running, if the comport.readlines() is busy reading,
> > the next statement is running, if the next statement happens to be a
> > comport.write() which will abort the reading.
>
> > Is there any way to force the python script to behave like running
> > exactly sequentially?

I am using Python 3.2 in Windows in which Pexpect appeared
problematic.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web