【发布时间】:2020-06-13 14:39:29
【问题描述】:
我的网络由多个连接到Max485 的 Arduino 组成。这些 Arduino 可以完美地相互交流。
我目前正在尝试将 Raspberry Pi 连接到网络中。我一直在关注this tutorial。
我已启用 UART 引脚,并禁用了 shell over serial。
为了测试,我有wired TX(GPIO 14/pin 8)到 MAX485 上的 DI,RX(GPIO 15/pin 10)到 RO,GPIO 4(pin 7)到 DE & RE。它还为两个 MAX485 芯片供电,并且两个芯片都接地。 在 arduino 方面,我目前使用的是Mega。它有 TX3 到 DI,RX3 到 RO,引脚 2 到 DE/RE。这两个设备是此网络上的唯一设备。
Raspi Python:
import time
import serial
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
#sets pin 7 on the GPIO as DE/RE
GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW)
rs = serial.Serial(port='/dev/serial0', timeout=5, baudrate=9600)
data = bytearray()
msgIn = bytearray()
addr = 1
# Splits each byte into two, then unfold each half-byte to make a full byte.
# The slave will take this data, and fold it back to readable form
# This is to ensure anything being read by the slave is actual data, not noise.
def foldOpen(where, what):
hb = what >> 4
where.append((hb << 4) | (hb ^ 0x0F))
lb = what & 0x0F
where.append((lb << 4) | (lb ^ 0x0F))
# Unfolds the folded data
def unFold():
sByte, cByte = False, 0
timeout = time.perf_counter()
while((time.perf_counter() - timeout) < 1):
inByte = rs.read()
if((inByte >> 4) != ((inByte & 0x0F) ^ 0x0F)):
return 0
inByte >>= 4
if(sByte):
cByte <<= 4
cByte |= inByte
return cByte
else:
cByte = inByte
sByte = True
timeout = time.perf_counter()
return 0
# add's each piece of data into the crc
def AddCrc(crc, n):
for i in range(0, 8):
mix = (n ^ crc) & 0x01
crc >>= 1
if(mix):
crc ^= 0x8C
n >>= 1
return crc & 0xFF
#Receives a start bit, then address, then data length, then data, and finally crc.
#If everything is formatted correctly, the right amound of data is passed and crc correct
#it will return true
def recvMsg(msg):
msgState = crc = msgL = 0
timeout = time.perf_counter()
while(msgState <= 4):
if(rs.in_waiting > 0):
if(msgState < 1):
inByte = rs.read()
sleep(1)
else:
inByte = unFold()
if(msgState == 4):
for x in msg:
crc = AddCrc(crc, x)
if(crc == inByte):
return 1
elif(msgState == 3):
msg.append = inByte
if(len(msg) == msgL):
msgState = 4
elif(msgState == 2):
msgL = inByte
msgState = 3
elif(msgState == 1):
if(inByte == addr):
msgState = 2
else:
msgState = 5
elif(msgState == 0):
print('Start bit is ')
print(inByte)
if(inByte == 2):
print('accepted')
msgState = 1
if((time.perf_counter() - timeout) >= 5):
msgState = 5
#Sends a message, starting with start bit (2), addr, msg length, data, and crc
def sendMsg(where, size, what):
GPIO.output(7, GPIO.HIGH)
msg = bytearray()
crc = 0
msg.append(2)
foldOpen(msg, where)
foldOpen(msg, size)
for x in what:
foldOpen(msg, x)
for x in what:
crc = AddCrc(crc, x)
foldOpen(msg, crc)
rs.write(msg)
rs.flush()
GPIO.output(7, GPIO.LOW)
#creating random data to send to slave for testing
data = bytearray()
info = ord('A')
info2 = 45
data.append(info)
data.append(info2)
sendMsg(2, len(data), data)
#reads 1 byte, just so I know I made a connection
timer = time.perf_counter()
while((time.perf_counter() - timer) < 10):
if(rs.in_waiting):
inByte = rs.read(1)
print(inByte)
Arduino 代码:
#include <RS485_Comm.h>
byte enablePin = 2;
byte check = 0;
size_t rsWrite (const byte what) {
Serial3.write (what);
Serial3.flush();
}
bool rsAvailable () {
return Serial3.available ();
}
int rsRead () {
return Serial3.read ();
}
RS485 myChannel (rsWrite, rsAvailable, rsRead, 20, 2, 2, 1);
//name(Write CB, AvailableCB, ReadCB, buffer, Epin, Addr, Debug)
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
myChannel.begin();
Serial.print("A-OK");
}
void loop() {
if (myChannel.recvMsg()) {
if (myChannel.getMsg()[0] == 'A') {
Serial.print("A-OK");
byte msgOut[] = "A";
myChannel.sendMsg(msgOut, sizeof(msgOut), 1);
}
}
}
同样,我可以从 Raspi 向 Arduino 发送消息。相同的 Arduino,以相同的配置接线,可以与网络上的其他 Arduino 来回通信。
我无法从 Arduino 获取任何信息到树莓派。 rs.read(1) 不返回任何内容或一些随机噪声。我哪里错了?
【问题讨论】:
-
你在树莓派上
/boot/cmdline.txt的设置是什么? -
Arduino代码还是python???
-
糟糕,刚刚解决了这个问题。
-
和@hcheung 它的“console=tty1 root=PARTUUID=ea7d04d6-02 rootfstype=ext4 lift=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles”
标签: python arduino raspberry-pi rs485