行走的轮子

戒急用忍


  • 首页

  • 归档

  • 标签

  • 到此一游

  • 搜索

WordPress页面调用分类并分页的技术

发表于 2012-08-03   |   分类于 WordPress   |   ❤

注:之所以把注写在这里是因为以下均为网上查询结果的整合,不看也行,可以百度或者Google一下。
`1.注意加入的代码必须为**<span style="text-decoration: underline; color: #33cccc;"><span style="text-decoration: underline;">英文</span></span>**,否则会报错。 2**.**<span style="text-decoration: underline;">**<span style="color: #33cccc; text-decoration: underline;">不要用Word</span>**</span>进行中间的文字转化,否则会在HTML中增加多余的代码,删起来十分复杂。 `3.这是第一篇正式文章,有很多不熟练的地方等待以后加强。

阅读全文 »

python 串口测试工具中 py2exe的使用

发表于 2011-11-17   |   分类于 Python   |   ❤

python 串口测试工具中 py2exe的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from distutils.core import setup
import py2exe
import sys, os
sys.argv.append('py2exe')
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
if os.path.basename(pathname).lower() in ("msvcp71.dll", "dwmapi.dll", "msvcr71.dll"):
return 0
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL #exe中包含msvcp71.dll等文件
includes = ["encodings", "encodings.*"]
options = {'py2exe': { "bundle_files" : 1}} #bundle_files为1时只生成一个exe
data_files = [('data', ['msvcp71.dll'])] #包含msvcp71.dll,以防止操作系统中不包含此文件时之需
setup(options = options,
zipfile=None, #使用zipfile=None可以不生成library.zip
data_files = data_files,
#console=..... #命令行运行方式打包
windows = [{"script":'SerialTestApp.pyw', "icon_resources" : [(1, "246.ico")]}]) #icon_resources 设置图标

python串口通信模块——pySerial

发表于 2011-11-17   |   分类于 Python   |   ❤

#pySerial

##Overview
This module encapsulates the access for the serial port. It provides backends for Python running on Windows, Linux, BSD (possibly any POSIX compliant system), Jython and IronPython (.NET and Mono). The module named “serial” automatically selects the appropriate
backend.

It is released under a free software license, see LICENSE.txt for more details.
(C) 2001-2008 Chris Liechti cliechti@gmx.net

The project page on SourceForge and here is the SVN repository and the Download Page .
The homepage is on http://pyserial.sf.net/

##Features

  • same class based interface on all supported platforms
  • access to the port settings through Python 2.2+ properties
  • port numbering starts at zero, no need to know the port name in the user program
  • port string (device name) can be specified if access through numbering is inappropriate
  • support for different bytesizes, stopbits, parity and flow control with RTS/CTS and/or Xon/Xoff
  • working with or without receive timeout
  • file like API with “read” and “write” (“readline” etc. also supported)
  • The files in this package are 100% pure Python. They depend on non standard but common packages on Windows (pywin32) and Jython (JavaComm). POSIX (Linux, BSD) uses only modules from the standard Python distribution)
  • The port is set up for binary transmission. No NULL byte stripping, CR-LF translation etc. (which are many times enabled for POSIX.) This makes this module universally useful.

##Requirements

  • Python 2.2 or newer
  • pywin32 extensions on Windows
  • “Java Communications” (JavaComm) or compatible extension for Java/Jython

##Installation

###from source
Extract files from the archive, open a shell/console in that directory and let Distutils do the rest:
python setup.py install

The files get installed in the “Lib/site-packages” directory.

###easy_install
An EGG is available from the Python Package Index: http://pypi.python.org/pypi/pyserial
easy_install pyserial

###windows installer
There is also a Windows installer for end users. It is located in the Download Page
Developers may be interested to get the source archive, because it contains examples and the readme.

##Short introduction
Open port 0 at “9600,8,N,1”, no timeout

1
2
3
4
5
import serial
ser = serial.Serial(0) # open first serial port
print ser.portstr # check which port was really used
ser.write("hello") # write a string
ser.close() # close port

Open named port at “19200,8,N,1”, 1s timeout

1
2
3
4
5
ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
x = ser.read() # read one byte
s = ser.read(10) # read up to ten bytes (timeout)
line = ser.readline() # read a '\n' terminated line
ser.close()

Open second port at “38400,8,E,1”, non blocking HW handshaking

1
2
3
ser = serial.Serial(1, 38400, timeout=0, parity=serial.PARITY_EVEN, rtscts=1)
s = ser.read(100) # read up to one hundred bytes
... # or as much is in the buffer

Get a Serial instance and configure/open it later

1
2
3
4
5
6
7
8
9
10
11
>>> ser = serial.Serial()
>>> ser.baudrate = 19200
>>> ser.port = 0
>>> ser
Serial<id=0xa81c10, open=False>(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0)
>>> ser.open()
>>> ser.isOpen()
True
>>> ser.close()
>>> ser.isOpen()
False

Be carefully when using “readline”. Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that “readlines” only works with a timeout. “readlines”
depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.
Do also have a look at the example files in the examples directory in the source distribution or online.

##Examples
Please look in the SVN Repository. There is an example directory where you can find a simple terminal and more.
http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/examples/

##Parameters for the Serial class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ser = serial.Serial(
port=None, # number of device, numbering starts at
# zero. if everything fails, the user
# can specify a device string, note
# that this isn't portable anymore
# if no port is specified an unconfigured
# an closed serial port object is created
baudrate=9600, # baud rate
bytesize=EIGHTBITS, # number of databits
parity=PARITY_NONE, # enable parity checking
stopbits=STOPBITS_ONE, # number of stopbits
timeout=None, # set a timeout value, None for waiting forever
xonxoff=0, # enable software flow control
rtscts=0, # enable RTS/CTS flow control
interCharTimeout=None # Inter-character timeout, None to disable
)

The port is immediately opened on object creation, if a port is given. It is not opened if port is None.
Options for read timeout:

1
2
3
timeout=None # wait forever
timeout=0 # non-blocking mode (return immediately on read)
timeout=x # set timeout to x seconds (float allowed)

##Methods of Serial instances

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
open() # open port
close() # close port immediately
setBaudrate(baudrate) # change baud rate on an open port
inWaiting() # return the number of chars in the receive buffer
read(size=1) # read "size" characters
write(s) # write the string s to the port
flushInput() # flush input buffer, discarding all it's contents
flushOutput() # flush output buffer, abort output
sendBreak() # send break condition
setRTS(level=1) # set RTS line to specified logic level
setDTR(level=1) # set DTR line to specified logic level
getCTS() # return the state of the CTS line
getDSR() # return the state of the DSR line
getRI() # return the state of the RI line
getCD() # return the state of the CD line

##Attributes of Serial instances
Read Only:

1
2
3
4
5
portstr # device name
BAUDRATES # list of valid baudrates
BYTESIZES # list of valid byte sizes
PARITIES # list of valid parities
STOPBITS # list of valid stop bit widths

New values can be assigned to the following attributes, the port will be reconfigured, even if it’s opened at that time:

1
2
3
4
5
6
7
8
port # port name/number as set by the user
baudrate # current baud rate setting
bytesize # byte size in bits
parity # parity setting
stopbits # stop bit with (1,2)
timeout # timeout setting
xonxoff # if Xon/Xoff flow control is enabled
rtscts # if hardware flow control is enabled

##Exceptions

1
serial.SerialException

##Constants
parity:

1
2
3
serial.PARITY_NONE
serial.PARITY_EVEN
serial.PARITY_ODD

stopbits:

1
2
serial.STOPBITS_ONE
serial.STOPBITS_TWO

bytesize:

1
2
3
4
serial.FIVEBITS
serial.SIXBITS
serial.SEVENBITS
serial.EIGHTBITS

在python文件中集成图片

发表于 2011-11-14   |   分类于 Python   |   ❤

方法一:
http://leo108.com/pid-938.asp
使用base64方式编解码。
核心代码如下:
1.将图片文件编码为base64字符串:

1
2
3
4
5
import base64 #导入base64库
f = open(r'/home/1.ico','rb') #用二进制方式打开图片文件
str = base64.b64encode(f.read()) #读取文件内容,编码为base64字符串
f.close() #关闭文件
print str #输出base64编码结果

2.将base64字符串解码为图片:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import base64
import wx
import cStringIO
def GetMondrianData():
iconData = "图片BASE64字符串"
iconData = base64.b64decode(iconData)
return iconData
def GetMondrianBitmap():
return wx.BitmapFromImage(GetMondrianImage())
def GetMondrianImage():
stream = cStringIO.StringIO(GetMondrianData())
return wx.ImageFromStream(stream)
def GetMondrianIcon():
icon = wx.EmptyIcon()
icon.CopyFromBitmap(GetMondrianBitmap())
return icon

调用GetMondrianIcon()函数即可

方法二:
使用函数im2py.py,下面这个是旧版wxpython的使用
http://www.blog.pythonlibrary.org/2008/05/23/wxpython-embedding-an-image-in-your-title-bar/
wxpython_2.9.2_py27中的使用:
打开cmd,打开文件夹C:\Python27\Lib\site-packages\wx-2.9.2-msw\wx\tools,输入命令

1
python img2py.py -i (-n ***)28.ico myIcon.py

option中-n, -i的注释:
-n Normally generic names (getBitmap, etc.) are used for the
image access functions. If you use this option you can
specify a name that should be used to customize the access
fucntions, (getNameBitmap, etc.),否则默认为下划线+ico的名字
本例中为_28
-i Also output a function to return the image as a wxIcon
输出文件为myIcon,
本例中的使用方法为:

1
2
3
import myIcon
ico = myIcon._28.getIcon()
self.SetIcon(ico)

当然也可以在myIcon.py的文件末尾加

1
get_Icon = _28.getIcon

则使用方法为

1
2
3
import myIcon
ico = myIcon.get_Icon()
self.SetIcon(ico)
1…3637
Jerky Lu

Jerky Lu

戒急用忍

364 日志
45 分类
75 标签
GitHub Weibo
友情链接
  • 李琼写文章的地方
  • 落影流年
  • Koihik's Blog
© 2020 Jerky Lu
京ICP备15007413号-1
主题 - NexT.Mist