我用python编写了一个脚本,用于M5Stack Stick C(如raduino)与树莓派之间的串行通信。一切正常。我可以将树莓派中的“ X”,“ Y”或“ Z”发送到棍子,他将把值(G-Force)返回给raspi!到现在为止还挺好
代码:
在raspy上使用Python:
import serial
import time
import threading
ser = serial.Serial('/dev/rfcomm5') #init serial port
input_line = []#init input char array
def process_data(_data):
#called every time a sream is terminated by \n
#and the command string is ready to use
command = convert(_data)
print(command)
def convert(s): #convert the char list in a string
new = "" #init string to append all chars from char array
for x in s: # traverse in the string
new += str(x)
return new # return string
def processIncomingByte(inByte):#adding incoming chars to input_line
global input_line# globalize