Hi, ive been through a few iterations of this code making improvements, I recently removed strings and swapped for char[] although serial disconnect time has improved every few days it will completely stop responding to serial requests.
Can any one see any code issues that could be leading to this?
wow thanks both for your speedy replies, I will add that edit and see how it goes.
I was/am worried it could be the way I'm dynamically setting and using pins and I suppose I'm at the stage where I could start hard coding these things but the repetitive code is ugly
I will report back.
Thanks
Edit:
I'm not sure I understand what that's doing though, from stack overflow
"modulus Operator gives you remainder"
how does it work if you don't mind helping me understand.
As others have pointed out, this should be maxChars-1 as it could potentially be 79 at start of loop and would be incremented to 80 which overflows the array..
Might want to add a check to see if ndx is getting to high,do a serial print for debugging, obviously something in the data is not correct..
I notice you don't have a check for this potential..
Be nice if you have another serial port to debug print too..
try something like this..
void processSerialData() {
char startMarker = '<';
char endMarker = '>';
while (Serial.available() > 0 && ndx < maxChars-1) {
char c = Serial.read();
if (c == startMarker) {
ndx = 0;
} else if (c == endMarker) {
receivedChars[ndx] = '\0';
processInput();
ndx = 0; // Reset index after processing
} else {
receivedChars[ndx++] = c;
}
}
//check here for too much wrong data, reset things..
if (ndx == maxChars-1){
ndx = 0;
memset(receivedChars,0,sizeof(receivedChars));//emtpy
}
}
it adds a check to see if ndx is sitting at 79 with no end marker in sight you will not enter while loop anymore..
so we reset back to ndx 0 and empty the buffer..
still not sure why the incoming is getting mucked up..
You could print the mess to see what it is..
How is the device getting data from a program or you using a secure shell telnet connection to it??
the mega's got a few serial ports..
The Arduino is plugged into raspberry pi 4 at a family members house, which is their smart heating system so when the temperature data stops the system stops
I ssh in to work remotely and the serial is over USB, all seems to be working now so time will tell but the local testing I did showed me the error straight away, cant believe I hadn't checked sooner.
I may consider changing my code to a single request temp command
"<22,23,24,25>"
and have the Arduino probe all pins asked for and send a single array back,
"[22.7,45.1,....]"
I don't think the amount of serial requests I'm doing is good design.
nah, probably not, just make sure you stay in sync somehow..
ok, makes more sense, on the pi is it your program talking to the mega, might want to add some transmission control..
have the mega send a nak back when it starts to overflow, when you recv a nak reset the sending side of things..
I've enjoyed getting back into the Arduino its been fun and completely unforgiving :), I've been hobby coding in python for a while and its a different world, low level and low power so much more to consider with the Arduino.
I'm using node-red on the pi to send and receive serial, I will look into the link I want to make this system bulletproof.
Thinking about this some more..
Serial is anything but bullet proof..
Could just be some corruption..
Might be a good idea to add a simple checksum of sorts to the data to make sure it's valid..
Look at the UDPSend and UDPRecv in my git it adds a simple checksum to each packet and then checks it to make sure data is received valid..
To bullet proof, each packet should have a checksum that is checked and a ACK is sent if checksum fails send a NAK back, have your sender resend..
Can log the NAKs back on pi to see if line is dirty but expect some bad packets as they will happen..
~q
Thank you, I will look into this later on today. The commands I send are fairly trivial and I expect the event to fail if the command was damaged in any way but it could occur that the command was damaged in a way it could trigger the wrong event, definitely need it to work as professionally as possible so I don’t get headache from the user