树莓派一直发送信号,arduino一直接受乱码
import serial #import serial module
import os
import time
a = os.system("sudo chmod 777 /dev/ttyAMA0") # 使用a接收返回值,同时在树莓派中获取端口使用权限
print(a)
ser = serial.Serial('/dev/ttyAMA0', 9600,timeout=1) #open named port at 9600,1s timeot
#try and exceptstructure are exception handler
try:
while 1:
ser.write('s'.encode("utf-8"))#writ a string to port
time.sleep(1)
response = ser.readall()#read a string from port
print(response.decode("utf-8"))
print("ok")
except:
已经参考
进行过设置,树莓派串口能发送信息。
接收端arduino也能够收到信息,但是大部分都是乱码,代码如下:
void setup()
{
serial.begin(9600); // 9600 bps
}
void loop()
{
if ( serial.available())
{
if('s' == serial.read())
serial.println("Hello Raspberry,I am Arduino.");
}
}