Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

python - Caught exception in on_message: [Errno 9] Bad file descriptor

I'm publishing and subscribing through paho library I'm able to connect and send & receive mqtt messages but when I'm sending message from broker(rabbitmq) to my device it gave me sometimes an error :

[Errno 9] Bad file descriptor

below you'll find the error logs, I have search for a solution but it wasn't successful =/

freqTRmessage = "JJ"

def subscribe(client: mqtt_client):

    def on_message(client, userdata, msg):

            #variable
            global TIOTvariable
            global RSvariable
            global RSSvariable
            global update_time
            global bat_cons
            global freqTRmessage

            #print message received
        
            print("
 message received = " ,str(msg.payload.decode("utf-8")))

            #technology change
            if str(msg.payload.decode("utf-8")) == "LTE-M" and msg.topic == topic_modif:
                print("
 Update requested : TECHNOLOGIE_IOT ==> RSLE")
                config_M1_MODE()

            elif str(msg.payload.decode("utf-8")) == "2G" and msg.topic == topic_modif:
                print("
 Update requested : TECHNOLOGIE_IOT ==> RS2G")
                config_GSM_MODE()

            elif str(msg.payload.decode("utf-8")) == "NB-IoT" and msg.topic == topic_modif:
                print("
 Update requested : TECHNOLOGIE_IOT ==> RSNB_IOT")
                config_NB1_MODE()

            #CAS_USAGE
            if str(msg.payload.decode("utf-8")) == "MM" and msg.topic == topic_modif:
                print("
 Update requested : 1 uplink messages/day")
                update_time_telemetry = 30
                update_time_battery = 0
                freqTRmessage = "MM"

            elif str(msg.payload.decode("utf-8")) == "JJ" and msg.topic == topic_modif:
                print("
 Update requested : 3 uplink messages/day")
                update_time_telemetry = 20
                update_time_battery = 2
                freqTRmessage = "JJ"

            elif str(msg.payload.decode("utf-8")) == "alerte" and msg.topic == topic_modif:
                print("
 Update requested : battery consumption = 12% ")
                bat_cons=12
                
            elif str(msg.payload.decode("utf-8")) == "100" and msg.topic == topic_modif:
                print("
 Update requested : battery consumption = 100% ")
                bat_cons=100

    client.subscribe([(topic_service,1),(topic_telemetry,1),(topic_diagnostic,1),(topic_modif,1)])
    client.on_message = on_message
    client.on_log = on_log 
Caught exception in on_message: [Errno 9] Bad file descriptor
    Exception in thread Thread-1:
    Traceback (most recent call last):
      File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
        self.run()
      File "/usr/lib/python3.7/threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)
      File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3452, in _thread_main
        self.loop_forever(retry_first_connection=True)
      File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1779, in loop_forever
        rc = self.loop(timeout, max_packets)
      File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1181, in loop
        rc = self.loop_read(max_packets)
      File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1572, in loop_read
        rc = self._packet_read()
      File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 2310, in _packet_read
        rc = self._packet_handle()
      File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 2936, in _packet_handle
        return self._handle_publish()
      File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3216, in _handle_publish
        self._handle_on_message(message)
      File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3444, in _handle_on_message
        self.on_message(self, self._userdata, message)
      File "Demo.py", line 168, in on_message
        config_GSM_MODE()
      File "Demo.py", line 28, in config_GSM_MODE
        node.setMode(node.GSM_MODE)
      File "/usr/local/lib/python3.7/dist-packages/sixfab_cellulariot-1.2.0-py3.7.egg/cellulariot/cellulariot.py", line 310, in setMode
        self.sendATComm("AT+QCFG="nwscanseq",01,1","OK
")
      File "/usr/local/lib/python3.7/dist-packages/sixfab_cellulariot-1.2.0-py3.7.egg/cellulariot/cellulariot.py", line 212, in sendATComm
        while(ser.inWaiting()):
      File "/home/pi/.local/lib/python3.7/site-packages/serial/serialutil.py", line 594, in inWaiting
        return self.in_waiting
      File "/home/pi/.local/lib/python3.7/site-packages/serial/serialposix.py", line 549, in in_waiting
        s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
    OSError: [Errno 9] Bad file descriptor 

here is my function config_GSM_MODE

    node = cellulariot.CellularIoT()
    node.setupGPIO()
    node.disable()
    sleep(1)
    node.enable()
    sleep(1)
    node.powerUp()
    node.setMode(node.GSM_MODE)
    for i in range(1,7):
        sleep(10)

Cellulariot is a librairy from Sixfab, infact i send a message to my device for changing network mode, sometimes there is no issue and sometimes this issue appears while the message is received on my device


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...