开源改变世界

如何通过 Websocket 连接并发送 g 代码 #138

推推 grbl 3年前 (2023-02-05) 357次浏览
关闭
hamidb 打开了这个问题 2021 年 11 月 26 日 · 5 条评论
关闭

如何通过 Websocket 连接并发送 g 代码#138

hamidb 打开了这个问题 2021 年 11 月 26 日 · 5 条评论

评论

如何通过 Websocket 连接并发送 g 代码 #138

嗨,
我正在尝试编写一个 python 客户端应用程序,通过 wifi 连接将我的命令发送到 Fluidnc 主机。
我不确定我应该如何在 python 客户端中设置连接。

以下是我的尝试的简要说明:

  1. 我已经设置了与STA的Fluidnc 连接,在引导消息中我得到:
[MSG:INFO: Connecting to STA SSID:3212]
[MSG:INFO: Connecting.]
E (1057) wifi:AP has neither DSSS parameter nor HT Information, drop it
[MSG:INFO: Connecting..]
[MSG:INFO: Connected - IP is 10.88.111.15]
[MSG:INFO: WiFi on]
[MSG:INFO: Start mDNS with hostname:http://fluidnc.local/]
[MSG:INFO: SSDP Started]
[MSG:INFO: HTTP started on port 80]
[MSG:INFO: Telnet started on port 23]

所以在 python 代码中我尝试了以下操作:

import websockets
import asyncio

async def hello():
  async with websockets.connect('ws://10.88.111.15/') as ws:
    await ws.send('HOW TO SEND COMMAND')
    greeting = await ws.recv() # HOW TO RECEIVE RESPONSE
    print(greeting)

asyncio.get_event_loop().run_until_complete(hello())

我有两点不确定:

  1. 我不确定这是否是建立连接以将 g 代码发送到 esp32 的正确方法
  2. 即使建立连接成功,如何向esp发送gcode命令?把它当作串行接口?
  3. 当我使用ws4py库(除了websockets)时,我收到以下消息,之后connect给我的印象是缺少某些东西:
ws4py.exc.HandshakeError: Invalid response status: b'200' b'OK'  

如果有人能够让它与 websocket 客户端一起工作,我将不胜感激。
问候

如何通过 Websocket 连接并发送 g 代码 #138

websocket 端口号比 http 端口多 1,因此如果 http 端口为默认值 80,则需要在 81 端口上连接 websocket。

之后,您可以像通过串行连接一样发送 Grbl 命令。

如何通过 Websocket 连接并发送 g 代码 #138
作者

谢谢(你的)信息。
我试过了,我能够建立连接。但我仍然无法让步进器移动。
这是我的新尝试:

import websocket
import _thread
import time

def on_message(ws, message):
    print('in message ', message)

def on_error(ws, error):
    print('error: ', error)

def on_close(ws, close_status_code, close_msg):
    print("### closed ###")

def on_open(ws):
    def run(*args):
      ws.send('$HX\n')
      time.sleep(1)
      ws.close()
      print("thread terminating...")
    _thread.start_new_thread(run, ())

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://10.88.111.15:81",
                              on_open=on_open,
                              on_message=on_message,
                              on_error=on_error,
                              on_close=on_close)
    ws.run_forever()

这是日志:

--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Host: 10.88.111.15:81
Origin: http://10.88.111.15:81
Sec-WebSocket-Key: UOnb7DZkNEkQVYO6FMqXVQ==
Sec-WebSocket-Version: 13
Connection: Upgrade


-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Server: arduino-WebSocketsServer
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Version: 13
Sec-WebSocket-Accept: Uwdjp+mfNTLrZjyYd6gwT9WJwFM=
-----------------------
++Rcv raw: b'\x89\x00'
++Rcv decoded: fin=1 opcode=9 data=b''
++Sent raw: b'\x81\x84,\xad1&\x08\xe5i,'
++Sent decoded: fin=1 opcode=1 data=b'$HX\n'
++Sent raw: b'\x8a\x80pF(\xd6'
++Sent decoded: fin=1 opcode=10 data=b''
++Rcv raw: b'\x81\x0cCURRENT_ID:0'
++Rcv decoded: fin=1 opcode=1 data=b'CURRENT_ID:0'
in message  CURRENT_ID:0
++Rcv raw: b'\x81\x0bACTIVE_ID:0'
++Rcv decoded: fin=1 opcode=1 data=b'ACTIVE_ID:0'
in message  ACTIVE_ID:0
++Sent raw: b"\x88\x82'\x10\xd8\xf4$\xf8"
++Sent decoded: fin=1 opcode=8 data=b'\x03\xe8'
### closed ###
如何通过 Websocket 连接并发送 g 代码 #138

我在 websocket 处理中发现了一个错误。该错误现已在 WebSockets 分支中修复。

这是一个简单的测试,显示它基本上可以正常工作。

PS C:\Users\wmb\Documents\GitHub\fluidterm> python
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import websocket
>>> ws = websocket.WebSocket()
>>> ws.connect("ws://192.168.2.150:81")
>>> ws.send("$\n")
8
>>> print(ws.recv())
CURRENT_ID:0
>>> print(ws.recv())
ACTIVE_ID:0
>>> print(ws.recv())
PING:0
>>> print(ws.recv())
PING:0
>>> print(ws.recv())
b'[HLP:$$ $+ $# $S $L $G $I $N $x=val $Nx=line $J=line $SLP $C $X $H $F $E=err ~ ! ? ctrl-x]\nok\n'
>>>
如何通过 Websocket 连接并发送 g 代码 #138
作者

感谢您的修复。我确认它也解决了 Linux 的问题。

如何通过 Websocket 连接并发送 g 代码 #138 bdring 将此 添加到 开发中的错误 2021 年 11 月 28 日
如何通过 Websocket 连接并发送 g 代码 #138 bdring 将其从Bug 移至In progress in Development 2021 年 11 月 28 日
如何通过 Websocket 连接并发送 g 代码 #138 bdring 将此从进行 中 移至开发中的错误 2021 年 11 月 28 日
如何通过 Websocket 连接并发送 g 代码 #138 bdring 将其从Bug 移至In progress in Development 2021 年 11 月 28 日
如何通过 Websocket 连接并发送 g 代码 #138 bdring 将此从进行 中 移至开发中的错误 2021 年 11 月 28 日
如何通过 Websocket 连接并发送 g 代码 #138 bdring 将其从Bug 移至In progress in Development 2021 年 11 月 28 日
如何通过 Websocket 连接并发送 g 代码 #138 bdring 将此从进行 中 移至开发中的错误 2021 年 11 月 28 日
如何通过 Websocket 连接并发送 g 代码 #138

由#142修复