2013-07-20 89 views
2

我正在使用pySerial从物理尺寸读取数据的快速项目。我使用pygame作为简单GUI的前端。我正在尝试使用py2exe来部署该项目。py2exe编译pySerial没有错误,但COM端口不打开

  • 该项目工作时,通过蟒蛇运行,打开序列和阅读预期。

  • 我的py2exe安装脚本编译可执行文件运行正常(无输入错误)

  • 可执行文件无法打开串口正确。除非我在发现无法找到串口时引发异常,否则程序将在此时关闭。

  • 当所有的序列代码都在try/catch中被取消或嵌套时,可执行文件工作正常。 pygame或其他代码可能没有问题。

下面是main.py,这是唯一的文件中的相关代码:

import os, sys 
import pygame 
from pygame.locals import * 

if not pygame.font: print 'Warning, fonts disabled' 


#serial 
import serial 
import io 
import time 

...

def initSerial(): 
try: 
    #The port is hard coded for now 
    port = 11 
    ser = serial.Serial(port-1) 
    ser.baudrate = 2400 
    ser.timeout = 0.01 
    sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser)) 
    print "serial port opened" 
    return ser, sio 
except Exception, message: 
    print "couldn't open serial" 
    raise SystemExit, message #The executable dumps here if this line is included 
    return None, None 

这是我的setup.py,我主要从tutorial复制:

#see http://screeniqsys.com/blog/2009/03/20/making-py2exe-play-nice-with-pygame/ 

from distutils.core import setup 
import py2exe 
import sys 
import os 
import glob, shutil 
import pygame 
sys.argv.append("py2exe") 

VERSION = '1.0' 
PRODUCT_NAME = 'Weight Mentor' 
SCRIPT_MAIN = 'main.py' 
VERSIONSTRING = PRODUCT_NAME + " ALPHA " + VERSION 

REMOVE_BUILD_ON_EXIT = True 
PYGAMEDIR = os.path.split(pygame.base.__file__)[0] 

SDL_DLLS = glob.glob(os.path.join(PYGAMEDIR, '*.dll')) 

if os.path.exists('dist/'): shutil.rmtree('dist/') 

extra_files = [ ("graphics", glob.glob(os.path.join('graphics','*.png'))), 
       ("fonts", glob.glob(os.path.join('fonts','*.ttf'))) ] 

INCLUDE_STUFF = ['encodings',"encodings.latin_1",] 

setup(windows=[ 
    {'script': SCRIPT_MAIN}], 
    options = {"py2exe": { 
     "optimize": 2, 
     "includes": INCLUDE_STUFF, 
     "compressed": 1, 
     "ascii": 1, 
     "bundle_files": 3, #unfortunately on x64 we can't use the better bundle files options. So we bundle nothing :(
     "ignores": ['tcl', 'AppKit', 'Numeric', 'Foundation'] 
     } }, 
    name = PRODUCT_NAME, 
    version = VERSION, 
    data_files = extra_files, 
    zipfile = None) 

if REMOVE_BUILD_ON_EXIT: 
    shutil.rmtree('build/') 

for f in SDL_DLLS: 
    fname = os.path.basename(f) 
    try: 
     shutil.copyfile(f, os.path.join('dist',fname)) 
    except: 
     pass 

正常情况下,此设置脚本也有一些代码可以修正某些排除项的分布权重,但我在测试时删除了这些代码以排除排除的程序包作为错误的来源。

所以我认为必须有东西(一个DLL?INCLUDE_STUFF中的另一行?),我需要将它添加到setup.py,以使其与pySerial正常工作。我只是不知道那是什么。 py2exe文档没有提到pySerial的特殊要求,我的谷歌搜索没有发现任何东西。

任何帮助将不胜感激!

回答