Question

Under Linux, use fdpexpect module to interact with the serial port, such as:

 fd = os.open(TTY, os.O_NONBLOCK|os.O_RDWR|os.O_NOCTTY)
 child = fdpexpect.fdspawn(fd)

In Windows, how to implement the above?

Was it helpful?

Solution 2

I just got this working over this weekend on Windows 7. Here is how I did it:

First, the fdpexpect module does seem to be the only way to "chat" with a serial port in Python 2.7. The latest Python pexpect module docs says it can take an integer (int) file descriptor (like fdpexpect), but it doesn't work on my Ubuntu 12.10 install. So it seems that fdpexpect is the way to go. Get if from:

http://www.opensource.apple.com/source/lldb/lldb-69/test/pexpect-2.4/fdpexpect.py

Second, the fdpexpect module requires a file descriptor as input. Although the Python Pyserial module ("import serial") it cross-platform, to use it with fdpexpect one must use the Serial.fileno() method to get the int file descriptor for your serial port. But the Serial.fileno() method does not exist in Windows Python; it only exists in POSIX Python, where integer file descriptors are used.

Fortunately, it can be made to work using Cygwin. Cygwin is a free POSIX-like environment for the Windows OS. Run the Cygwin setup.exe and select the following Cygwin packages:

python
nano
wget

Then the run the following commands at the Cygwin Bash shell prompt:

# Install 'distribute', so we can use it to install 'pip':
wget.exe http://python-distribute.org/distribute_setup.py

# Execute the downloaded script:
python distribute_setup.py

# Now do the 'pip' installer:
wget --no-check-certificate https://raw.github.com/pypa/pip/master/contrib/get-pip.py

python get-pip.py

# Install pyserial for serial comms w/pexpect support via Serial.fileno()
pip install pyserial

Now, if you run your Python script under the Cygwin Python install (and not Windows native Python), you can pass the output of your Serial.fileno() to fdpexpect, and communicate with sendline() and expect(). I am communicating with two different embedded systems under Windows using this method.

Note that the serial.Serial() constructor takes a string like "/dev/ttyS0" under Unix, but under Windows (incl. Cygwin) it needs an integer. Use int(2) for COM3, int(3) for COM4, and so on. The Device Manager will tell you what COM port numbers you should use.

...

One final note if you are talking to an Arduino Uno... on my Windows 7 system, plugging in the Arduino causes the /dev/ttyS## to show up immediately, like one would expect. However, the serial port does not work until you use the Arduino software serial terminal, OR Putty, OR the Cygwin 'screen' command to open the Arduino serial port. Once you've opened it in one of those programs, it works fine until it is unplugged. I don't know why; it seems to be a bug in the Arduino driver. (I do not have this problem using my FTDI driver for my non-Arduino device.)

OTHER TIPS

pyserial provides a platform independent interface for serial ports.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top