title: I have an Arduino Rs485 network, and am trying to add a in Raspberry Pi. The Pi is able to send messages to the Arduino's, but is unable to receive
body: <p>My network consists of multiple Arduino's connected to the <a href="https://hobbycomponents.com/wired-wireless/663-max485-rs485-transceiver-module" rel="nofollow noreferrer">Max485</a>. These Arduino's can talk perfectly between each other.</p>
  <p>I am currently attempting to wire a Raspberry Pi into the network. I have been following <a href="https://circuitdigest.com/microcontroller-projects/rs485-serial-communication-between-arduino-and-raspberry-pi" rel="nofollow noreferrer">this tutorial</a>.</p>
  <p>I have enabled the UART pins, and disabled shell over serial.</p>
  <p>For testing, I have <a href="https://i.stack.imgur.com/lUfhD.png" rel="nofollow noreferrer">wired</a> TX(GPIO 14/pin 8) to DI on the MAX485, RX(GPIO 15/pin 10) to RO, GPIO 4 (pin 7) to DE &amp; RE. It is also powering both MAX485 chips, and both chips are grounded to it.
  On the arduino side, I am currently using a <a href="https://i.stack.imgur.com/G9ofO.png" rel="nofollow noreferrer">Mega</a>. It has TX3 to DI, RX3 to RO, and pin 2 to DE/RE. These two devices are the only ones on this network.</p>
  <p>Raspi Python:</p>
  <pre><code>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 &gt;&gt; 4
      where.append((hb &lt;&lt; 4) | (hb ^ 0x0F))
      lb = what &amp; 0x0F
      where.append((lb &lt;&lt; 4) | (lb ^ 0x0F))
  
  # Unfolds the folded data
  def unFold():
      sByte, cByte = False, 0
      timeout = time.perf_counter()
  
      while((time.perf_counter() - timeout) &lt; 1):
          inByte = rs.read()
          if((inByte &gt;&gt; 4) != ((inByte &amp; 0x0F) ^ 0x0F)):
              return 0
  
          inByte &gt;&gt;= 4
  
          if(sByte):
              cByte &lt;&lt;= 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) &amp; 0x01
          crc &gt;&gt;= 1
          if(mix):
              crc ^= 0x8C
          n &gt;&gt;= 1
      return crc &amp; 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 &lt;= 4):
          if(rs.in_waiting &gt; 0):
              if(msgState &lt; 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) &gt;= 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) &lt; 10):
      if(rs.in_waiting):
          inByte = rs.read(1)
          print(inByte)
  </code></pre>
  <p>Arduino Code:</p>
  <pre><code>#include &lt;RS485_Comm.h&gt;
  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(&quot;A-OK&quot;);
  }
  
  void loop() {
    if (myChannel.recvMsg()) {
      if (myChannel.getMsg()[0] == 'A') {
        Serial.print(&quot;A-OK&quot;);
        byte msgOut[] = &quot;A&quot;;
        myChannel.sendMsg(msgOut, sizeof(msgOut), 1);
      }
    }
  
  }
  </code></pre>
  <p>Again, I can send messages from the Raspi to the Arduino's. The same Arduino, wired in the same configuration, can talk back and forth with other Arduino's on the network.</p>
  <p>I just can't get any info from an Arduino to the raspberry pi. rs.read(1) returns nothing, or some random noise. Where am I going wrong?</p>