zoukankan      html  css  js  c++  java
  • 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张。
  • 相关阅读:
    Angular中文api
    Angular各版本和组件下载
    判断一个浏览器是否支持opacity
    查找函数参数名称
    运行时代码求值
    简单的动画引擎
    利用闭包特性改写addEventListener的回调函数
    springboot指定redis库编号配置实现
    springboot获取IOC(ApplicationContext)实例
    ajax-springMVC提交表单的方式
  • 原文地址:https://www.cnblogs.com/phirothing/p/3280888.html
Copyright © 2011-2022 走看看