Python串口操作纸币器1
公司需要纸币器开发,参考纸币器文档和网上的一篇vb版本,代码如下:
# -*- coding:utf-8 -*-
# Author: Pete Yim<xpHook@gmail.com>
# Date : 13-7-31 import threading
import serial
from time import sleep GLOBAL_COUNT = 0 GLOBAL_OUT_INIT_NUM = 0
GLOBAL_OUT_CHECK_NUM = 0
COMMAND_INIT_FLAG = False
COMMAND_CHECK_FLAG = False
COMMAND_OUTPUT = ''
GLOBAL_CASH_1 = 0
GLOBAL_CASH_5 = 0
GLOBAL_CASH_10 = 0
GLOBAL_CASH_20 = 0
GLOBAL_CASH_50 = 0 SERIAL_SETTING = {
'PORT' : 4,
'BAUDRATE' : 9600,
'BYTESIZE' : serial.EIGHTBITS,
'PARITY' : serial.PARITY_NONE,
'STOPBITS' : serial.STOPBITS_TWO
} DATA_OUT_DIC = {
'DATA_OUT_1' : '7F8001116582',
'DATA_OUT_2' : '7F000123C988',
'DATA_OUT_3' : '7F800226FC3B56',
'DATA_OUT_4' : '7F000125DD88',
'DATA_OUT_5' : '7F0004240301126757',
'DATA_OUT_6' : '7F0001051E08',
'DATA_OUT_7' : '7F8001195602',
'DATA_OUT_8' : '7F0001093608',
'DATA_OUT_9' : '7F8001071202',
'DATA_OUT_10' : '7F0003029F0021DA',
'DATA_OUT_11' : '7F80010A3F82',
'DATA_OUT_12' : '7F0001071188',
'DATA_OUT_13' : '7F8003029F002266',
'DATA_OUT_14' : '7F00010A3C08'
} class CashSerial(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.__terminate = False def open(self, settings):
try:
self.serial = serial.Serial(port=SERIAL_SETTING['PORT'],
baudrate=SERIAL_SETTING['BAUDRATE'],
bytesize=SERIAL_SETTING['BYTESIZE'],
parity=SERIAL_SETTING['PARITY'],
stopbits=SERIAL_SETTING['STOPBITS'])
self.serial.flushInput()
self.serial.flushOutput()
global COMMAND_INIT_FLAG
COMMAND_INIT_FLAG = True
except Exception, msg:
return False, msg.message.decode("gbk") return True, "success" def resetArduino(self):
self.serial.setDTR(0)
sleep(0.1)
self.serial.setDTR(1) def terminate(self):
self.__terminate = True def send(self, data):
self.serial.write(data) def recv(self):
data, quit = '', False
while not self.__terminate:
try:
n = self.serial.inWaiting()
if n:
data += self.serial.read(n)
# for l in xrange(len(data)):
# print '%02X' % ord(data[l])
return data.encode('hex') except Exception as ex:
print str(ex) return data def close(self):
if self.serial.isOpen():
self.serial.close() def run(self):
running = True
i = 0
while running:
sleep(0.5)
# print '*******>> ' + str(i)
GLOBAL_COUNT = i
i = i + 1
#---------------------- self.exec_command_init()
sleep(0.02)
self.exec_command_check() global COMMAND_CHECK_FLAG
if COMMAND_CHECK_FLAG:
recvdata = self.recv()
# print "received data >>> " + str(recvdata)
self.exec_count_cash(recvdata) def exec_command_init(self):
global COMMAND_INIT_FLAG, GLOBAL_OUT_INIT_NUM, COMMAND_CHECK_FLAG
if COMMAND_INIT_FLAG:
GLOBAL_OUT_INIT_NUM += 1 if GLOBAL_OUT_INIT_NUM == 1:
send_data = str(DATA_OUT_DIC['DATA_OUT_1']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 2:
send_data = str(DATA_OUT_DIC['DATA_OUT_1']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 3:
send_data = str(DATA_OUT_DIC['DATA_OUT_1']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 4:
send_data = str(DATA_OUT_DIC['DATA_OUT_2']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 5:
send_data = str(DATA_OUT_DIC['DATA_OUT_3']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 6:
send_data = str(DATA_OUT_DIC['DATA_OUT_4']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 7:
send_data = str(DATA_OUT_DIC['DATA_OUT_1']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 8:
send_data = str(DATA_OUT_DIC['DATA_OUT_5']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 9:
send_data = str(DATA_OUT_DIC['DATA_OUT_1']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 10:
send_data = str(DATA_OUT_DIC['DATA_OUT_6']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 11:
send_data = str(DATA_OUT_DIC['DATA_OUT_7']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 12:
send_data = str(DATA_OUT_DIC['DATA_OUT_1']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 13:
send_data = str(DATA_OUT_DIC['DATA_OUT_8']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 14:
send_data = str(DATA_OUT_DIC['DATA_OUT_9']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 15:
send_data = str(DATA_OUT_DIC['DATA_OUT_10']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 16:
send_data = str(DATA_OUT_DIC['DATA_OUT_11']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 17:
send_data = str(DATA_OUT_DIC['DATA_OUT_12']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 18:
send_data = str(DATA_OUT_DIC['DATA_OUT_13']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 19:
send_data = str(DATA_OUT_DIC['DATA_OUT_14']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 20:
send_data = str(DATA_OUT_DIC['DATA_OUT_9']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 21:
send_data = str(DATA_OUT_DIC['DATA_OUT_12']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_INIT_NUM == 22:
send_data = str(DATA_OUT_DIC['DATA_OUT_9']).decode('hex')
GLOBAL_OUT_INIT_NUM = 0
COMMAND_INIT_FLAG = False
COMMAND_CHECK_FLAG = True
self.send(send_data) def exec_command_check(self):
global COMMAND_CHECK_FLAG, GLOBAL_OUT_CHECK_NUM
if COMMAND_CHECK_FLAG:
GLOBAL_OUT_CHECK_NUM += 1 if GLOBAL_OUT_CHECK_NUM == 1:
send_data = str(DATA_OUT_DIC['DATA_OUT_12']).decode('hex')
self.send(send_data)
elif GLOBAL_OUT_CHECK_NUM == 2:
send_data = str(DATA_OUT_DIC['DATA_OUT_9']).decode('hex')
GLOBAL_OUT_CHECK_NUM = 0
self.send(send_data) def exec_count_cash(self, recvdata=''):
if recvdata:
cach_data = recvdata[8:12]
if cach_data == 'ef01':
global GLOBAL_CASH_1
GLOBAL_CASH_1 += 1
print '1块钱:' + str(GLOBAL_CASH_1) + '张。'
if cach_data == 'ef02':
global GLOBAL_CASH_5
GLOBAL_CASH_5 += 1
print '5块钱:' + str(GLOBAL_CASH_5) + '张。'
if cach_data == 'ef03':
global GLOBAL_CASH_10
GLOBAL_CASH_10 += 1
print '10块钱:' + str(GLOBAL_CASH_10) + '张。'
if cach_data == 'ef04':
global GLOBAL_CASH_20
GLOBAL_CASH_20 += 1
print '20块钱:' + str(GLOBAL_CASH_20) + '张。'
if cach_data == 'ef05':
global GLOBAL_CASH_50
GLOBAL_CASH_50 += 1
print '50块钱:' + str(GLOBAL_CASH_50) + '张。' if __name__ == '__main__':
cserial = CashSerial()
result_status = cserial.open(SERIAL_SETTING) if result_status:
cserial.start() # data = "7F8001116582"
# hexdata = data.decode("hex")
#
# myserial.send(hexdata, _type=None)
#
插入1元、5元纸币,识别结果为:
1块钱:1张。
5块钱:1张。
Python串口操作纸币器1的更多相关文章
- 面向切面编程AOP——加锁、cache、logging、trace、同步等这些较通用的操作,如果都写一个类,则每个用到这些功能的类使用多继承非常难看,AOP就是解决这个问题的,python AOP就是装饰器
面向切面编程(AOP)是一种编程思想,与OOP并不矛盾,只是它们的关注点相同.面向对象的目的在于抽象和管理,而面向切面的目的在于解耦和复用. 举两个大家都接触过的AOP的例子: 1)java中myba ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- [python基础]关于装饰器
在面试的时候,被问到装饰器,在用的最多的时候就@classmethod ,@staticmethod,开口胡乱回答想这和C#的static public 关键字是不是一样的,等面试回来一看,哇,原来是 ...
- Day04 - Python 迭代器、装饰器、软件开发规范
1. 列表生成式 实现对列表中每个数值都加一 第一种,使用for循环,取列表中的值,值加一后,添加到一空列表中,并将新列表赋值给原列表 >>> a = [0, 1, 2, 3, 4, ...
- python基础—函数装饰器
python基础-函数装饰器 1.什么是装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的返回值是也是一个函数对象. 装饰器经常用于有切 ...
- Python文件操作与函数目录
文件操作 python文件操作 函数 Python函数学习——初步认识 Python函数学习——作用域与嵌套函数 Python函数学习——匿名函数 python内置函数 Python函数学习——递归 ...
- 初学Python——文件操作第二篇
前言:为什么需要第二篇文件操作?因为第一篇的知识根本不足以支撑基本的需求.下面来一一分析. 一.Python文件操作的特点 首先来类比一下,作为高级编程语言的始祖,C语言如何对文件进行操作? 字符(串 ...
- Day11 Python基础之装饰器(高级函数)(九)
在python中,装饰器.生成器和迭代器是特别重要的高级函数 https://www.cnblogs.com/yuanchenqi/articles/5830025.html 装饰器 1.如果说装 ...
- (转)Python中操作mysql的pymysql模块详解
原文:https://www.cnblogs.com/wt11/p/6141225.html https://shockerli.net/post/python3-pymysql/----Python ...
随机推荐
- java对象初始化顺序的简单验证
以下这段小程序对调用对象构造函数时,父类构造函数.成员变量初始化函数,以及非静态初始化块调用顺序进行验证,不考虑静态成员及静态初始化块. public class Derive extends Bas ...
- SQL Function(方法)
1.为什么有存储过程(procedure)还需要(Function) fun可以再select语句中直接调用,存储过程是不行的. 一般来说,过程显示的业务更为复杂:函数比较有针对性. create f ...
- left join, right join , inner join, join, union的意义
数据库在连接两张或以上的表来返回数据时,都会生成一张中间的临时表,然后再将临时表返回给用户left join,right join,inner join, join 与 on 配合用 select c ...
- nodejs中间层现实
初次接触nodejs,是一种非常神奇的东西,未来必火起来.个人觉得最大优势npm命令. 闲话少说,直入主题.这是一个博客项目,php最为服务端,提供数据给node:nodejs+express作为中间 ...
- zzuli oj 1167逆转数(指针专题)
Description 任意给你一个整数,这个数可能很大(最长不超过100位),你能求出它的逆转数吗? 逆转数定义如下: 1.一个末尾没有0的整数,它的逆转数就是各位数字逆序输出: 2.一个负数 ...
- ADS的默认连接分析及编译器产生符号解惑
ADS的默认连接顺序是怎样的呢?例如下边从2440init.s中摘出的编译器符号又该怎样理解呢? BaseOfROM DCD |Image##RO##Base| TopOfROM ...
- web 文件上传 无刷新的方法 使用iframe
<html> <script type="text/javascript" src="admin/view/js/jquery.min.js" ...
- 解决qt5窗口不刷新(测试窗口类型,测试窗口属性)
QApplication::notify #if QT_VERSION >= 0x050000 if (QEvent::Show == pEvent->type()) ...
- Android MediaPlayer播放一般音频与SoundPool播放短促的音效
[1]使用MediaPlayer实现一般的音频播放 MediaPlayer播放通常的音频文件 MediaPlayer mediaPlayer = new MediaPlayer(); if (medi ...
- [ZOJ 3622] Magic Number
Magic Number Time Limit: 2 Seconds Memory Limit: 32768 KB A positive number y is called magic n ...