I'm building a master/slave project using RS485. This all worked at one point in the past as intended but my gut tells me I likely have some older code saved on my computer that is not up to date with what once worked.
I can confirm the slave correctly communicates to the master because it's able to transmit the string "New feeder detected with UUID:" followed by what is supposed to be a unique identifier number like 85AE4826-F9B8-42A1-A2A4-DE9446317FCD but instead the UUID is garbage: �������������������������������
I'm using this Waveshare barcode scanner to scan a QR code and store it in the slave's EEPROM. I can confirm the waveshare scanner works as intended when I hook it up to an Arduino and directly output to serial monitor.
I'm hoping someone can take a look at the slave code attached (made up of a few files - I omitted ones that I believe are irrelevant like NeoPixel control files) and let me know what's wrong in my code that's causing this to read out garbage when I connect a slave to the master's serial monitor. I've tried setting my serial monitor on my master at both 57600 baud and 9600, no luck. Also have Both NL & CR enabled if it matters (I assume not since the first part of the string gets sent successfully).
I've also attached the master code in the first code block. Any help appreciated!
Master Code
pinMode(EnRS485,INPUT);
Serial1.begin(9602);
Serial.begin(57600);
while(Serial.available()>0);
Serial.print("S");
}
void loop(){
updateData();
}
void updateData(){
if(Serial1.available()>5){
String e=Serial1.readStringUntil('\r');
Serial.println(e);
}
if(Serial.available()>1){
String t=Serial.readStringUntil('\r');
const char* c=t.c_str();
if(strstr(c,"M602")){
t=t.substring(4,t.length()-2);
FeederUUID=t.substring(1,t.indexOf(' ')-1);
int d=t.substring(t.indexOf(' ')).toInt();
int dir=2;
digitalWrite(EnRS485,LOW);
Serial1.print("<"+FeederUUID+","+String(d)+","+String(dir)+">\r");
delay(5);
} else if(strstr(c,"M603")){
t=t.substring(4);
Slave Code files from here on
Main file:
#include <SoftwareSerial.h>
int RequestedDistance = 0; //Holds target distance information for feeder motor
boolean CoverSensorValue; //true if cover is open
boolean TensionSensorValue; //true if no tension is sensed
boolean isRSCommand=false; //true when last received move motor command is RS command
boolean isFeederActive=true; //True if feeder motor is moving right now
boolean LockPeelingMotor=true; //Makes sure peeling motor is locked once operation is done, so error messages "halted" isnt shown
String FeederUUID;
String EEPROMUUID;
//UUID
int ee_address = 0;
const byte numChars = 37;
char uuid[numChars]; // an array to store the received data
char default_uuid[numChars] = "00000000-0000-0000-0000-000000000000\0";
//edcfeb27-de3c-46bb-9b83-cea63a52c36b
struct UUID {
char is_uuid;
char _uuid[numChars];
};
struct UUID eeprom_uuid;
boolean newData = false;
void setup() {
initNeoPixel(); // Initialize LED
setLedState(LED_READY);
pinMode(ENCA,INPUT);
pinMode(CoverSensor,INPUT);
pinMode(TensionSensor,INPUT);
pinMode(MotorFeedPINA,OUTPUT);
pinMode(MotorFeedPINB,OUTPUT);
pinMode(MotorPeelPINA,OUTPUT);
pinMode(MotorPeelPINB,OUTPUT);
pinMode(EnRS485, OUTPUT); //DE/RE Controling pin of RS-485
pinMode(ForwardButton,INPUT_PULLUP);
delay(5);
readStringFromEEPROM(0, &EEPROMUUID);
EEPROMUUID.remove(EEPROMUUID.length()-1);
delay(5);
// send “EEPROM: <uuid>” in one RS-485 transaction:
{
String banner = "New feeder dete
void readButtons(){
if(digitalRead(ForwardButton)==LOW){
if(MotorReverseDir==1){MotorFeedDirection=0;}
while(digitalRead(ForwardButton)==LOW){
MoveMotor(0,FeederSpeed,MotorFeedDirection);//Feed motor, speed, direction
HandlePeeling();
}
RequestedDistance = 1;
MotorPosition = 0;
while(digitalRead(ReverseButton)==LOW){
MoveMotor(0,FeederSpeed,MotorFeedDirection);//Feed motor, speed, direction
}
RequestedDistance = 1;
MotorPosition = 0;
isRSCommand=false;
}
}
SerialFunctions.ino
RequestedDistance=RequestedDistance/DistanceDivider;
isRSCommand=true;
LockPeelingMotor=false;
MotorPosition = 0;
PeelingTimer=millis();
}
}
Serial.flush();
}
}
void sendserial(String dat){
digitalWrite(EnRS485 , HIGH);
delay(1);
Serial.print(dat);
delay(20);
digitalWrite(EnRS485 , LOW);
}
UUID.ino
// terminate and flag
uuid[ndx] = '\0';
ndx = 0;
newData = true;
}
}
if (newData) {
newData = false;
// echo and save
sendserial("Feeder address: " + String(uuid) + "\n");
writeStringToEEPROM(0, String(uuid));
}
}
}
int readStringFromEEPROM(int addrOffset, String *strToRead) {
int newStrLen = EEPROM.read(addrOffset);
}
data[newStrLen] = '\0'; // ← correct null terminator!
*strToRead = String(data);
return addrOffset + 1 + newStrLen;
}