通过 Arduino Mega R32060 从 MAX485 或 USB TTL 转换器接收数据

Receive data via Arduino Mega R32060 from MAX485 or USB TTL convertor

我正在尝试使用 Arduino Mega R32060 的代码接收 MAX485 或 USB TTL 转换器。

#include <SoftwareSerial.h>

#define SSerialRX        15
#define SSerialTX        14 
#define SSerialTxControl 3  

#define RS485Transmit    HIGH
#define RS485Receive     LOW

#define Pin13LED         13

SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

int byteReceived;
int byteSend;

void setup()
{
  Serial.begin(9600);

  pinMode(Pin13LED, OUTPUT);
  pinMode(SSerialTxControl, OUTPUT);

  digitalWrite(SSerialTxControl, RS485Receive); 

  RS485Serial.begin(9600); 
}

void loop()
{  
  if (Serial.available())
  {
    digitalWrite(Pin13LED, HIGH);
    byteReceived = Serial.read();
    Serial.println(byteReceived);

    digitalWrite(SSerialTxControl, RS485Transmit);  
    RS485Serial.write(byteReceived);

    delay(10);
    digitalWrite(Pin13LED, LOW);
    digitalWrite(SSerialTxControl, RS485Receive); 
  }

  if (RS485Serial.available())  //Look for data from other Arduino
  {
    digitalWrite(Pin13LED, HIGH);  // Show activity
    byteReceived = RS485Serial.read();    // Read received byte
    Serial.write(byteReceived);        // Show on Serial Monitor
    
    delay(10);
    digitalWrite(Pin13LED, LOW);  // Show activity 
    Serial.println(byteReceived);
  }
}

这里的Pin 3是MAX485传输和接收模式的开关。

不过,我可以从 arduino 完美地发送数据

但是条件RS485Serial.available()永远不会成立。这可能是什么原因?

尝试使用 Serial 读取您的数据。 RX 15 和 TX 14 是 Arduino Mega 上的第三个系列。

这是从 Serial3.

读取数据的 MWE
int val = 0;

void setup()
{
  Serial.begin(9600);
  Serial3.begin(9600);
}

void loop()
{
  if (Serial3.avaliable() > 0) {
    val = Serial3.read();
    Serial.write(val);
  }
}