pyserial newbie questions

Gilles unix at unix.com
Fri Apr 11 02:26:14 EDT 2003


In article <8tfla.154$N5.27502 at mencken.net.nih.gov>, Mike
<uptonothing at yahoo.com> wrote:

Hello all,

Fank Zheng sent me this sample via email, and it works great.


import serial
import time
    s = serial.Serial(300) #here u can set timeout
    command = '\x00'*20
    s.write(command) #the return value of write(..) is the actual size
written.
    time.sleep(0.01)
    s.write('HELLO') 
recv = s.read(5) #if u don't set timeout, here will stop until get 5
bytes from the serial port
    if recv == 'READY':
        #do something 

Thanks again Frank :)

Gilles




> "Gilles" <gilles.celli at ecgs.lu> wrote in message
> news:gilles.celli-943DC4.10083810042003 at webmail.restena.lu...
> > Hello folks,
> >
> > Since I am very new to Python I have the following question:
> >
> > I try to write a program in Python (on RHat 8.0 Linux) with the
> > "pyserial" module which communicates to a device via RS232 serial cable.
> >
> > The program should do:
> >
> > 1. Set Com port  to 300,n,8,1
> > 2. Send 20 NULL characters Chr(0)
> > 3. Wait 10 ms ( may vary in your case)
> > 4. Send a string "HELLO"
> > 5. Wait to receive ³READY² string  from device
> > 6. If above string OK SEND   ³START²
> >
> > I figured out how to set the port, send the 20 NULL characters :)
> >
> > But I have no idea how to "wait for 10ms", send the HELLO string and
> > read the READY string...
> >
> > Anyone has a clue or source code sample ?
> >
> > Thanks a lot.
> >
> > Gilles
> 
> I don't know if this is the answer you're looking for but you can use the
> sleep(secs) function in the time module.
> 
> <code>
> import time
> 
> time.sleep(.01)  # sleep 10 ms
> ...
> </code>
> 
>




More information about the Python-list mailing list