Using pySerial with pexpect

2,729 views
Skip to first unread message

David Lyon

unread,
Feb 21, 2013, 5:28:13 AM2/21/13
to sydney...@googlegroups.com
Hi all,

I need to use pyserial and pexpect together. What I'm trying to do is
connect up a Raspberry-Pi then log into that from the pc with a serial
cable and read the ip address of the raspberry-pi.

The problem is I can't find documentation/examples of how to use both
of these libraries together.

This is essentially the pexpect code:

   child.expect('login:')
   child.sendline ('pi')
   child.expect('password:')
   child.sendline ('raspberry')
   # We expect any of these three patterns...
   i = child.expect (['Permission denied', 'Terminal type', '[#\$] '])
   if i==0:
       print 'Permission denied on host. Can't login'
       child.kill(0)
   elif i==2:
       print 'Login OK... need to send terminal type.'
       child.sendline('vt100')
       child.expect ('[#\$] ')
   elif i==3:
       print 'Login OK.'
       print 'Shell command prompt', child.after


and the (working) serial setup is:

 - # configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

ser.open()
ser.isOpen()

What I need to know is how to combine them together. Couldn't find a single example by searching.


David Lyon

unread,
May 30, 2013, 9:14:00 PM5/30/13
to sydney...@googlegroups.com


On Fri, May 31, 2013 at 2:08 AM, <ofer...@gmail.com> wrote:
Hi David,

I'm also looking for a combined usage of pyserial and pexpect but couldn't find any example nor documentation of how to use them together.
I'm currently using teraterm for all my scripting over serial port, but this can only run on Win machine.
The teraterm help is very simple is useful.
Install teraterm goto help->Index->Content->MACRO help->TTL command reference.

In your case the TTL script should be straight forward.

Thanks for posting that. I did end up getting it working correctly using 'screen', which turns out to apparently be the
default serial terminal program for linux (it seems).

I posted the project here:

 - https://github.com/OzBerryPi/serial-IPreader

and the code here:

 - https://github.com/OzBerryPi/serial-IPreader/blob/master/read-serialip.py
 
If I was doing it again (and for windows), I'd probably try using the python terminal emulator in pyserial. Live and Learn.

Stanley Mathew

unread,
Dec 23, 2013, 2:51:11 PM12/23/13
to sydney...@googlegroups.com
David,

I wouuld like to see you  code , but for some reason the "https://github.com/OzBerryPi/serial-IPreader" url reports page not found.  Do you  have some other location you posted?

The problem is with the other link too "https://github.com/OzBerryPi/serial-IPreader/blob/master/read-serialip.py"

Thanks
Stanley

David Lyon

unread,
Dec 24, 2013, 4:37:26 PM12/24/13
to sydney...@googlegroups.com
Hi Stanley,

They've obviously taken the code down.

It might be on one of my machines at work, but I'll have to wait to check in the new year.

Seasons Greetings

David


--
You received this message because you are subscribed to the Google Groups "sydneypython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sydneypython...@googlegroups.com.
To post to this group, send email to sydney...@googlegroups.com.
Visit this group at http://groups.google.com/group/sydneypython.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages