python读取串口信息

python读取配置文件中串口的名称,然后打开串口接受读卡器信息,再提交到某个网页上。

reader.ini:
[Default]
port=/dev/ttyUSB0

serial-test.py:

[python]
#!/usr/bin/python
import serial
import urllib2
import ConfigParser

def hexShow(argv):
result = ''
hLen = len(argv)
for i in xrange(hLen):
hvol = ord(argv[i])
hhex = '%02x'%hvol
result += hhex
# print 'hexShow:',result
return result

config = ConfigParser.ConfigParser()
config.readfp(open('reader.ini'))

sPort = config.get("Default","port")

ser = serial.Serial(sPort,9600)
while ser.isOpen():
sdata = ser.read(5)
cardid = hexShow(sdata)
# print cardid
cidurl = "http://127.0.0.1/cid.php?cid="+cardid

res_data = urllib2.urlopen(cidurl)
res = res_data.read()
print res
[/python]