我试图让我的ardunio通过蓝牙与我的覆盆子pi进行通信。截至目前,我已经让他们使用Minicomm进行通信,但我没有成功使用pyserial。我尝试了无数的东西,但我无法让它发挥作用; 我肯定知道的一些事情(来自minicomm和其他东西):
这是我在pi上的代码
import serial
import time
port="/dev/rfcomm0"
print('hello world')
bluetooth= serial.Serial(port,9600)
print ('hello world 2')
bluetooth.flushInput()
print ('hello world 3')
for i in range(100):
print("we are in the for loop",i)
inputs=bluetooth.readline()
print("we are in the inputs for loop",i)
inputasinteger= int(inputs)
if inputs:
print('we have inputs')
fileb= open("blue.txt",'wU')
fileb.write(inputasInteger*10)
time.sleep(.1)
print('sleeping')
fileb.close()
print('file has been closse')
exit()
你可以假设缩进是正确的......我不确定如何在这里修复它们 但是我的代码一直运行到输入行= bluetooth.readline();那它只是挂起 有人对这个有经验么?有解决方案吗知道我可以使用的任何其他模块吗?
答案 0 :(得分:2)
你确定Arduino方面正在发送换行符(“\ n”)吗?代码将挂起,除非它收到换行符。有关readline()的信息,请参阅pySerial文档: pySerial API
读取以行尾(eol)字符(默认为\ n)或直到超时终止的行。
如果您确定Arduino正在发送换行符,请在此处发布Arduino代码。