如何输出错误? [英] How to get error to output?

查看:108
本文介绍了如何输出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中使用阅读器RDM880编写代码读取RFID标签。它运作良好,但有时当我把卡放在远处或太快。它会像这样出错并停止工作

  tach_7   =  data [7] 
IndexError string index range



我希望在这种情况下它会重新启动。这是我的想法中的代码来解决这个问题。但我不知道如何得到错误字符串索引超出范围输出并检查它?我怎样才能做到这一点?感谢您的帮助。

  import  serial 
导入时间
来自 binascii import hexlify
chuoi = \ xAA \ x00 \ x03 \ x25 \ x26 \ x00 \ x00 \ xBB
serial = serial.Serial( / dev / ttyAMA0
baudrate = 9600,
stopbits = serial.STOPBITS_ONE,
parity = serial.PARITY_NONE,
bytesize = serial.EIGHTBITS,
timeout = 0。 1
ID_rong = 128187
def RFID (str):
serial.write(chuoi)
data = serial.readline()
tach_5 = data [ 5 ]
tach_6 =数据[ 6 ]
hex_5 = hexlify(tach_5)
hex_6 = hexlify(tach_6)
num_5 = int(hex_5, 16
num_6 = int(hex_6, 16
num_a = num_5 * 1000 + num_6
if (num_a!= ID_rong):
tach_7 = data [ 7 ]
tach_8 = data [ 8 ]
hex_7 = hexlify(tach_7)
hex_8 = hexlify(tach_8)
num_7 = int(hex_7, 16
num_8 = int(hex_8, 16
num = num_7 * 1000 + num_8 + num_6 * 1000000 + num_5 * 1000000000
else
num = num_5 * 1000 + num_6
return num
def check_read():
if ' 字符串索引超出范围' ?????):
返回 1
其他
返回 0
1
num = RFID(chuoi)
print num
x = check_read()
if (x == 1):
restart read RFID

解决方案

异常字符串索引超出范围不值得获得输出,无论它是什么。您需要防止此异常。即使你让它发挥作用,不这样做也会是严重的滥用。您可以在调试器下运行它,并看到抛出异常。让我告诉您,您的编程风格对于为生产开发的实际应用程序来说是完全不可接受的。一切都是硬编码的,重要的条件没有检查,等等。



-SA


I write the code read RFID tag using reader RDM880 in python. It work well but sometime when I put the card in the far distance or too fast.It will error like this and stop working

tach_7 = data[7]
IndexError : string index out of range


I want in this case it will restart. Here is my code in my idea to solve this problem. But I don't know how to get error "string index out of range" to output and check it? How can I do this? Thanks for help.

import serial
import time
from binascii import hexlify
chuoi="\xAA\x00\x03\x25\x26\x00\x00\xBB"
serial=serial.Serial("/dev/ttyAMA0",
                     baudrate=9600,
                     stopbits=serial.STOPBITS_ONE,
                     parity=serial.PARITY_NONE,
                     bytesize=serial.EIGHTBITS,
                     timeout=0.1)
ID_rong = 128187
def RFID(str): 
        serial.write(chuoi)
        data=serial.readline()
        tach_5 = data[5]
        tach_6 = data[6]
        hex_5=hexlify(tach_5)
        hex_6=hexlify(tach_6)
        num_5 = int(hex_5,16)
        num_6 = int(hex_6,16)
        num_a = num_5 * 1000 + num_6
        if(num_a != ID_rong):
                tach_7 = data[7]
                tach_8 = data[8]
                hex_7=hexlify(tach_7)
                hex_8=hexlify(tach_8)
                num_7 = int(hex_7,16)
                num_8 = int(hex_8,16)
                num= num_7 * 1000 + num_8 + num_6*1000000 + num_5 * 1000000000
        else:
                num = num_5 * 1000 + num_6
        return num
def check_read():
    if( 'string index out of range' ?????):
        return 1
    else:
        return 0
while 1:
    num =RFID(chuoi)
    print num
    x = check_read()
    if (x==1):
        #restart read RFID

解决方案

The exception "string index out of range" doesn't worth "getting to output", whatever it is. You need to prevent this exception instead. Not doing so would be a serious abuse, even if you make it working. You can run it under the debugger and see there the exception is thrown. Let me tell you that your programming style is totally unacceptable for a real application developed for production. Everything is hard-coded, important conditions are not checked up, and so on.

—SA


这篇关于如何输出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆