0

When I want to use serial library, I get this error:

$ ./console.py Traceback (most recent call last): File "./console.py", line 7, in import serial ImportError: No module named serial

I have Cygwin in Windows7

$ uname -a CYGWIN_NT-6.1 ES-T20019350 1.7.24(0.269/5/3) 2013-08-15 11:59 x86_64 Cygwin

and my Python version is 3:

Python 3.2.5 (default, Jul 30 2013, 20:11:30)

Can someone tell me how can I make the serial to work? I will need this to run serial traffic to my serial ports:

#!/usr/bin/python3
# =======================================
# establish communication using python
# =======================================

import time
import serial

# initialization and open the port
# possible timeout values:
#    1. None: wait forever, block call
#    2. 0: non-blocking mode, return immediately
#    3. x, x is bigger than 0, float allowed, timeout block call

ser = serial.Serial()

#ser.port = "/dev/ttyUSB0"
ser.port = "/dev/ttyS3"
#ser.port = "/dev/ttyS2"

ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None          #block read
ser.timeout = 0             #non-block read
#ser.timeout = 2              #timeout block read

ser.xonxoff = False     #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False       #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2     #timeout for write

try:
    ser.open()
except getopt.GetoptError as e:
        print ("error openning serial port",str(e))
        exit()

if ser.isOpen():
        try:
                ser.flushInput() #flush input buffer, discarding all its contents
                ser.flushOutput()#flush output buffer, aborting current output
                #and discard all that is in buffer
                #write data
                ser.write("ac_spi_slash 3\x0D")
                print("ac_spi_slash 3 sent\x0D")
                time.sleep(0.5)  #give the serial port sometime to receive the data
                numOfLines = 0

                while True:
                        response = ser.readline()
                        print("read data: " + response)
                        numOfLines = numOfLines + 1
                        if (numOfLines >= 5):
                                break
                ser.close()

        except getopt.GetoptError as e1:
                print ("error communicating...: ",str(e1));
else:
        print ("cannot open serial port ")
dsolimano
  • 8,870
  • 3
  • 48
  • 63
Mehdi
  • 113
  • 1
  • 11

1 Answers1

0

I was dealing with this issue recently and you might have the same problem.

This thread points to an issue that the standard install of python by using the python.org installer does not play nicely with cygwin, even if you set the path variable properly: Using Windows Python from Cygwin

My solution is to allow the cygwin installer to do a separate python install: cygwin setup.exe -> then select 'python: Python Language interpreter'

Then grab the pyserial library (usually just a folder called 'serial') and copy to C:\cygwin\lib\pythonX.X\site-packages\

This way the interactive mode works and the pyserial package is found in a standard cygwin search path.

Community
  • 1
  • 1