TypeError:' str'不支持缓冲区接口

时间:2017-04-21 19:37:05

标签: python string

我正在使用python 3开发图形界面,所以我发现了一些代码可以帮助我将文件(gcode)发送到GRBL,但是这段代码是在python 2中的。我试图修改程序以便工作在python 3上。

import serial
import time

# Open grbl serial port
s = serial.Serial('/dev/ttyS0',115200)

# Open g-code file
f = open(r'/home/pi/Downloads/spinner.gcode')

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

# Stream g-code to grbl
for line in f:
    l = line.strip() # Strip all EOL characters for streaming
    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())

# Wait here until grbl is finished to close serial port and file.
raw_input("  Press <Enter> to exit and disable grbl.")

# Close file and serial port
f.close()
s.close()

当我运行它时,它显示错误:

  

TypeError:&#39; str&#39;不支持缓冲区接口

1 个答案:

答案 0 :(得分:0)

只是猜测,因为你没有显示堆栈跟踪,甚至是生成错误的行。但我认为可以修复:

s.write((l + '\n').encode("utf8"))