本文摘自php中文网,作者不言,侵删。
这篇文章主要为大家详细介绍了python实现超市扫码仪计费,具有一定的参考价值,感兴趣的小伙伴们可以参考一下python实现超市扫码仪计费的程序主要是使用超市扫码仪扫商品的条形码,读取商品信息,实现计费功能。主要用到的技术是串口通信,数据库的操作,需要的环境包括:python环境,mysql,python库(serial,MySQLdb)等等。
这个程序的主要过程是:使用扫码仪扫描商品条形码,通过串口通信获取商品条形码,通过该条形码获取商品信息,显示该商品信息并统计总费用。其中商品信息保存在数据库中,可事先导入或者手动导入商品信息,而我的在这里是事先导入的(也可以边扫边倒入信息),导入到数据库中的信息如下:

程序代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import serial
import MySQLdb
ser = serial.Serial( 'COM5' , 9600 )
def recv(serial):
data = ''
while serial.inWaiting() > 0 :
data + = serial.read( 1 )
return data
def GetInfo(db,data):
data = data[ 0 : - 1 ]
print data
ret = 0.0
try :
cur = db.cursor()
sql = "set names utf8"
cur.execute(sql)
sql = "select * from productinfo where code=%s" % (data)
cur.execute(sql)
results = cur.fetchall()
for row in results:
code = row[ 0 ]
price = row[ 1 ]
info = row[ 2 ]
ret = price
print 'coding=' ,row[ 0 ], 'price=' ,row[ 1 ], 'info=' ,info.decode( 'UTF-8' ).encode( 'GBK' )
except :
print 'it has no infomation about %s' % (data)
return ret
db = MySQLdb.connect( 'localhost' , 'root' ,' ',"zou",3306,' utf8')
cursor = db.cursor()
sum = 0.0
while True :
data = recv(ser)
if data ! = '':
sum + = GetInfo(db,data)
print '总付款:' , sum
db.close()
ser.close()
|
由于刚刚开始学习python,所以代码规范上做的还不是很好,希望大家多多指出,最后程序的运行如下:

其中我的程序中可以使用中文(刚刚开始不是显示?就是显示乱码),这个问题我在前面的博客中谈论过,需要处理数据库以及从数据库读取的数据的编码方式。若是大家看出什么错误或是有意见的话,欢饮大家留言。
相关推荐:
Python实现计算圆周率π的值到任意位的方法示例
python实现简单淘宝秒杀功能
以上就是python实现超市扫码仪计费的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python 实现一行输入多个值
Python中print什么意思
Python中常见工厂函数用法详解
Python字典改变键值对的方法
anaconda和Python区别
如何用Python编写乘法口诀表
Python字符串怎么实现contains功能
Python中del函数的用法
Python遍历输出列表中最长的单词
Python和163邮箱授权码发送邮件的分析与实现(代码)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python实现超市扫码仪计费