这个问题与此相关,我创建了以下代码:

 import paho.mqtt.client as mqtt
import time
import serial

# Open grbl serial port
s = serial.Serial('COM3',9600)

# Wake up grbl
s.write("\r\n\r\n")
time.sleep(2)   # Wait for grbl to initialize 
s.flushInput()  # Flush startup text in serial input

f = """
O1000
T1 M6
(Linear / Feed - Absolute)
G0 G90 G40 G21 G17 G94 G80
G54 X-75 Y-75 S500 M3  (Position 6)
G43 Z100 H1
Z5
G1 Z-20 F100
X-40                   (Position 1)
Y40 M8             (Position 2)
X40                    (Position 3)
Y-40                   (Position 4)
X-75                   (Position 5)
Y-75                   (Position 6)
G0 Z100
M30
"""

# Runs when the client receives a CONNACK connection acknowledgement.
def on_connect(client, userdata, flags, result_code):
    print "Successful connection."
    # Subscribe to your topics here.
    client.subscribe("hi")

# Runs when a message is PUBLISHed from the broker. Any messages you receive
# will run this callback.
def on_message(client, userdata, message):
    if message.topic == "hi":
        if message.payload == "run":

            # Stream g-code to grbl
            for line in f:
                l = line.strip() # Strip all EOL characters for consistency
                print 'Sending: ' + l,
                s.write(l + '\n') # Send g-code block to grbl
                grbl_out = s.readline() # Wait for grbl response with carriage return
                print ' : ' + grbl_out.strip()

                # Close file and serial 
                s.close()
            # You could do something here if you wanted to.
        elif message.payload == "STOP":
            # Received "STOP". Do the corresponding thing here.
            # Close file and serial 
                s.close()
            print "CNC is stopped."

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)
client.loop_start()
 


但是当我运行它,但Successful connection.一无所有,然后代码结束了。你能告诉我发生了什么吗?

评论

很好地检查在经纪人那边或什至是Wireshark看到的东西。

同样如前所述,此类问题更适合于Stack Overflow,因为它是通用软件而不是IoT专用的

#1 楼

将最后一行从

client.start_loop()


更改为

client.loop_forever()