如何管理错误:TypeError:参数必须是int,或者具有fileno()方法

时间:2015-04-03 13:01:35

标签: python raspberry-pi zigbee

我想让zigbee设备使用RaspberryPi一起说话。 我正在使用Xbee"扩展"和系列1终端设备。 他们目前正在传输数据,我想为图书馆提取特定数据。我的代码很简单。

#coding: utf
#!/usr/bin/python
import serial
# for serie 1 componants
from xbee import XBee 
# for serie 2 componants
#from xbee import ZigBee 


##############################################################
def CalcDCAmps(data):
    mes = (data*20)/1024    # conversion 0-1023 en 0-20mA

    if (mes <4) :   #si la valeur reçues est inférieure à 4mA
        return -1 # problème sur le capteur
    else :
        return (mes*50)/20  # conversion 4-20mA en 0-50A

##############################################################


# Open serial port
serial_port = serial.Serial('/dev/ttyAMA0', 9600)

# Create XBee Series 1 object
zb = XBee(serial_port)
#zb = ZigBee(serial_port)



while True:
    j = 0;
    data_int = "";

    try:
        data = zb.wait_read_frame() #Get data for later use
        #print data # for debugging only
        print data['samples']# print the data frame of the list
        # frame type received[{'adc-0': 1023}]

        sample = str(data['samples']) #transformation de l'élément de la bibliothèque en chaîne de caractères
    #print "Equivalent String : %s" % sample

        for i in range(len(sample)):    # boucle pour isoler les chiffres de la chaîne de caractères
            if ((sample[i]>='0') & (sample[i]<='9')) :
                data_int += sample[i]   # stockage des chiffres dans une nouvelle chaîne de caractères

        data_int = int(data_int) # conversion de la chaîne de caractère en entier
        print "Valeur extraite:", data_int  

        courant = CalcDCAmps(data_int)
        print "Courant DC: ", courant, "A"

    except KeyboardInterrupt:
        break

    serial_port.close()

(抱歉法国评论) 当我计算它时,会出现:

[{'adc-0': 172}]
Valeur extraite: 172
Courant DC:  -1 A
Traceback (most recent call last):
  File "conversion_v3.py", line 36, in <module>
    data = zb.wait_read_frame() #Get data for later use
  File "/usr/local/lib/python2.7/dist-packages/xbee/base.py", line 399, in wait_read_frame
    frame = self._wait_for_frame()
  File "/usr/local/lib/python2.7/dist-packages/xbee/base.py", line 117, in _wait_for_frame
    if self.serial.inWaiting() == 0:
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 431, in inWaiting
    s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
TypeError: argument must be an int, or have a fileno() method.

现在我不知道如何管理它。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您正在关闭循环内的串口,因此在第一次迭代后它不能再读取帧。