2014-05-23 57 views
0
的Python系列进口

当我想用串行库,我得到这个错误:错误在Cygwin的

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

我的Cygwin在Windows7

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

和我的Python版本是3:

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

有人能告诉我如何让串行工作?我将需要此来运行到我的串行端口的串行流量:

#!/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 ") 
+0

你试过安装它吗? –

+0

我如何在Cygwin中安装? Cygwin没有apt-get install命令! – Mehdi

+0

这是pySerial吗?有很多方法可以安装它 - http://pyserial.sourceforge.net/pyserial.html#installation – dsolimano

回答

0

我最近处理这个问题,你可能有同样的问题。

此线程指向标准使用python.org安装程序不会与cygwin的发挥很好安装python的,即使你正确设置PATH变量的一个问题: Using Windows Python from Cygwin

我的解决办法是让cygwin的安装程序做一个单独的Python安装: cygwin的SETUP.EXE - >然后选择“的Python:Python语言解释器”

然后抓住pyserial库(通常简称“串行”的文件夹),并复制到C: \ cygwin \ lib \ pythonX.X \ site-packages \

通过这种方式,交互模式可以工作,pyserial包可以在标准的cygwin搜索路径中找到。