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;
}