我正在学习python,我需要从套接字解析一些gcode,然后将命令传递给串行端口。我有一些工作,使用选择器。
conn是我的TCP连接,正在接收Gcode。 sbus是我的串行端口。
假设data = b'G0 X1.0 Y2.0 Z0.0;移至X,Y,Z'
这是典型的gcode行。
我的代码在下面的输出是这样的:
1 b'G0
2 X1.0
3 Y2.0
4 Z0.0
因此,它按我的意愿丢弃了评论后的所有内容。
它像我想要的那样丢弃了裸露的b'\ n'。
但是,第一个元素包括b',其他元素不包括。
我对如何摆脱b'感到困惑
我确定我没有以正确的pythonic方式执行此操作,并且我希望对如何处理第一个项目的b'有一些见识(如果没有gcode注释,最后一个项目的末尾有'',我也必须处理)
谢谢
def read(conn, mask):
data = conn.recv(1000) # Should be ready
print(Color.Red, data,Color.end) #debug print, make text red
if data==b'\n': # don't process the slash-n
return
if data:
conn.send(b'ok\r\n') # sends back to openPnP
print('wroteback ok to tcp') # debug print
i=1
for word in repr(data).split(' '):
if word==';':
break
if word=='':
continue
print(i,Color.Green+word+Color.end) # prints each part of gcode line
i=i+1
sbus.write(data) # will actually send translated commands to serial prot, not just echo the data
else:
print('closing', conn)
sel.unregister(conn)
conn.close()
参考方案
b是字节。它只是告诉您数据类型是字节。这是检查字节和字符串以及如何从一个移到另一个的代码。
b = b'I am a bytes'
s = 'I am a string'
print(type(b), # bytes
type(s), # string
type(b.decode('utf8')), # string
type(s.encode('utf8')) # bytes
)
# change byte to string
b_s = b.decode('utf8')
print(b_s == 'I am a bytes')
# True
# change string to bytes
s_b = s.encode('utf8')
print(s_b == b'I am a string')
# True
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python ThreadPoolExecutor抑制异常 - pythonfrom concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…
如何用'-'解析字符串到节点js本地脚本? - python我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…