Nano Every with 2 UART-TTL to RS485 converters with external device

Hello Forums,

I am currently working on constructing a testing jig for a device controlled through RS-485. In this setup, I am using an Arduino Nano Every board equipped with two UART TTL to RS-485 adapters, specifically the
joy-it-joy-it-omvormermodule

While monitoring the serial monitor on my PC, I can observe the HEX commands when they are received, and the TXd LED flashes. However, the device does not react to these commands. Notably, the device to be tested responds appropriately when tested directly with a USB dongle with a 24V power supply V+ and 0V with A+, B+ and E (not Arduino).

I've been troubleshooting for a few weeks now and seem to be encountering a bottleneck. Could there be a code issue or a flaw in how I'm using Serial1, Serial and SoftwareSerial in conjunction with each other? Your insights and suggestions would be greatly appreciated.

I went though the Forums and found some thing that i already tried.

I currently just want to send a simple command and hope it arrived to the Test device.

The Wiring diagrams+ code:
Arduino Forum

code minimized to used functions:


/Define Includes libraries:
  #include <SoftwareSerial.h>
//---

//Define Serial configuration for communication with RS-485 trancievers MAX485:
  SoftwareSerial moduleSerial(-1, 10);  // RX_unused, TXd_2 //Only Send Commands
  //SoftwareSerial moduleSync(2, 10);  // RXd_0, TXd_1 //Recieve commands + send BREAK
//---

//Global definitions
  #define BUFFER_SIZE_1 8// Define the size of the buffer to store received bytes
  //#define BUFFER_SIZE_2 20// Define the size of the buffer to store received bytes
  #define EXPECTED_RESPONSE_TIME 500// less than 500ms
  #define WAIT_FOR_RESPONSE_TIME 500// wait for 500ms for message
  #define FRAME_SIZE 397
//---

// Define Bytes used in saving data from Rx0 and sending data to Txd_pin D10
  byte receivedBuffer[BUFFER_SIZE_1];//the Byte defined to save the commands recieved on RxD form pin RX0
//DEFINITIONS
  //================Commands Sent (StartApplication,Start Addressing,addressingCommands,StopAddressing)=========================================================================================================================//
    byte StartApplication[] = {0x55, 0xFF, 0xEF, 0x00, 0x01, 0x01, 0x7A, 0x88};//Power on and start application 0x55 0xFF 0xEF 0x00 0x01 0x01 0x7A 0x88
  //================Commands Sent (StartApplication,Start Addressing,addressingCommands,StopAddressing)=========================================================================================================================//
//SETUP
void setup() {
  //baud rate 1Mb/s
  Serial.begin(1000000); //Testing + debugging COM connnected to PC
  Serial1.begin(1000000); //Send Commands on Tx1 pin to TX pin on TX module + recieve Rx0 pin from Sync module 
  moduleSerial.begin(1000000); //  TXd_2 pin D10 Only send BREAK command pin D10 to Sync module TX

}


//MAIN CODE LOOP

void loop() {
 if (digitalRead(12) == LOW && !testingInProgress) {//CHECK PUSHBUTTON STATE &&
    delay(500);
     //POWER ON - Start Application operational state

      sendCommand(StartApplication, sizeof(StartApplication));
      WaitforRequest();
      Serial.println(receivedBuffer[0]);
      Serial.println(receivedBuffer[1]);
      Serial.println(receivedBuffer[2]);
      Serial.println(receivedBuffer[3]);
      Serial.println(receivedBuffer[4]);
      Serial.println(receivedBuffer[5]);
      Serial.println(receivedBuffer[6]);
      delay(3000);
    Serial.println("StartApplication");
    // Serial.println();
    
    Serial.println("Testing_Started");
    //startTest();
  }
}

void Start_Application(){//POWER ON - Start Application operational state

  delay(500);// delay of 500 ms required before fisrt sent command
  sendCommand(StartApplication, sizeof(StartApplication));
  delay(3000);
}

void sendCommand(const byte* command_byte,int length)// Fuctional for Start_test() V0.1 
{//During testing Serial.write, production = moduleSerial.write

  Serial.write(command_byte,length);// for debugging to see sent data on PC
  Serial1.write(command_byte,length); // Send Data to unit to be tested
  //moduleSerial.write(command_byte,length);

}

byte WaitforRequest(){
  
  
  memset(receivedBuffer, 0, BUFFER_SIZE_1);

  int bytesRead = readBytes(receivedBuffer, BUFFER_SIZE_1, EXPECTED_RESPONSE_TIME);

  if (bytesRead == BUFFER_SIZE_1) {
    Serial.print("Received Bytes: ");
    for (int i = 0; i < bytesRead; i++) {
      Serial.print(receivedBuffer[i], HEX);
      Serial.print("   ");
    }
    Serial.println("Bytes printed");
  }
  return receivedBuffer;


}

int readBytes(byte* buffer, int bufferSize, int timeout) {
  unsigned long startTime = millis();
  int bytesRead = 0;

  while (millis() - startTime < timeout && bytesRead < bufferSize) {
    if (Serial.available() > 0) {
      buffer[bytesRead++] = Serial.read();
    }
  }


  return bytesRead;
}

SoftwareSerial at 1000000 baud? I would start by using MCUdude's MegaCoreX which allows you to easily use all four hardware serial ports and get rid of SoftwareSerial. If you have the means to do so, you might want to check the actual baud rate for accuracy, the Nano Every runs off the internal oscillator instead of a crystal/resonator, and despite the box and almost all other literature saying 20MHz, the frequency is actually 16MHz. However, the clock frequency is set every time you program the board, and MegaCoreX has options in the tool menu to select the frequency.

1 Like

My first guess is that the baud rate is way too high for a software serial port.

I think you can get access to 3 hardware serial ports on the Nano Every but you would need to check this in the documentation.

1 Like

The compiler does not like the return statement, you have declared the function as returning a byte, but receiveBuffer is a byte*. Not going to matter in this code, because you never use the return value, but might cause unexpected results in the actual code.

byte WaitforRequest() {
  memset(receivedBuffer, 0, BUFFER_SIZE_1);

  int bytesRead = readBytes(receivedBuffer, BUFFER_SIZE_1, EXPECTED_RESPONSE_TIME);

  if (bytesRead == BUFFER_SIZE_1) {
    Serial.print("Received Bytes: ");
    for (int i = 0; i < bytesRead; i++) {
      Serial.print(receivedBuffer[i], HEX);
      Serial.print("   ");
    }
    Serial.println("Bytes printed");
  }
  return receivedBuffer;
}
1 Like

nano every contiains an Atmega4809 which has 4 USART channels(

This does mean I need to change my electrical wiring and software. but not sure if the clock frequency is going to be an issue or not.

Will the High baud rate be an issue?

I'll start by implementing the Hardware serial instead of the Software serial in the code.

The pinout from the MegaCoreX github page:

1 Like

I tried a simple Code and changed arduino_pins.h file ([https://www.youtube.com/watch?v=yG_ialcawho](https://youtube arduino Every 4 channel Serial)) but it didn't seem to work:
Any clue why it wouldn't work?

Edit:
I got this Working, now to try it with the rest of the code starting with sending StarApplication()

// --------------------------------------------------------------------------------------------------------- //
// Robert's Smorgasbord 2021                                                                                 //
// https://robertssmorgasbord.net                                                                            //
// https://www.youtube.com/channel/UCGtReyiNPrY4RhyjClLifBA                                                  //
// Arduino Nano Every: Four Hardware Serial Ports – Without Additional Hardware https://youtu.be/yG_ialcawho //
// --------------------------------------------------------------------------------------------------------- //

void setup() 
{
   Serial.begin(1000000); // 1Mbps

   while (!Serial);
   
   Serial1.begin(1000000); // 1Mbps
   Serial2.begin(1000000); // 1Mbps
   Serial3.begin(1000000); // 1Mbps
}
 
void loop()
{
   while (Serial.available()) Serial1.write(Serial.read());
   // Serial1 TX (TX) --> Serial2 RX (D7)
   while (Serial2.available()) Serial2.write(Serial2.read());
   // Serial2 TX (D2) --> Serial3 RX (D3)
   while (Serial3.available()) Serial3.write(Serial3.read());
   // Serial3 TX (D6) --> Serial1 RX (RX)
   while (Serial1.available()) Serial.write(Serial1.read()); 
}

MCUdude's MEgaCoreX can i just extract the Zip into Arduino Files?


No longer needed

The Solution was to Use Serial , Serial1 and Serial2 and to use this to communicate with the to be tested device, the electrical wireing is of importance and i have gotten it to work using the code below as a base:

// --------------------------------------------------------------------------------------------------------- //
// Robert's Smorgasbord 2021                                                                                 //
// https://robertssmorgasbord.net                                                                            //
// https://www.youtube.com/channel/UCGtReyiNPrY4RhyjClLifBA                                                  //
// Arduino Nano Every: Four Hardware Serial Ports – Without Additional Hardware https://youtu.be/yG_ialcawho //
// --------------------------------------------------------------------------------------------------------- //

void setup() 
{
   Serial.begin(1000000); // 1Mbps

   while (!Serial);
   
   Serial1.begin(1000000); // 1Mbps   Serial1 RX (RX) |Sync module|   Serial1 TX (TX)   |TX module  |
   Serial2.begin(1000000); // 1Mbps   Serial2 RX (D7) |NO module  |  Serial2 TX (D2)    |Sync module|
   Serial3.begin(1000000); // 1Mbps   Serial3 RX (D3) |NO module  |    Serial3 TX (D6)  |NO module  | 
}
 
void loop()
{
   while (Serial.available()) Serial1.write(Serial.read());
   // Serial1 TX (TX) --> Serial2 RX (D7)
   while (Serial1.available()) Serial.write(Serial1.read());

//
  //  while (Serial.available()) Serial1.write(Serial.read());
  //  // Serial1 TX (TX) --> Serial2 RX (D7)
  //  while (Serial2.available()) Serial2.write(Serial2.read());
  //  // Serial2 TX (D2) --> Serial3 RX (D3)
  //  while (Serial3.available()) Serial3.write(Serial3.read());
  //  // Serial3 TX (D6) --> Serial1 RX (RX)
  //  while (Serial1.available()) Serial.write(Serial1.read()); 
//

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.