Hardware: Arduino ESP32 (slave) and Raspberry Pi (master) Issue Summary: Problem with serial communications using HardwareSerial.h using a Modbus protocol Issue Detail: I am having an issue with serial communications that seems to be related to the order that hardware is turned on.
If the Pi is already running and the ESP32 is turned on the serial communications is established.
If the ESP32 is already running and the Pi is turned on then communications will not establish.
I am using the command peek() to see if data appears on the Arduino ESP32 serial line. Nothing appears when the connection order is wrong.
I can't ensure the order devices are turned on so this is a problem.
I figure this must be related somehow to the initialization of the Arduino in the setup function, and if nothing is detected on the Arduino serial line during startup then it must not check again?
Is there a way to force the arduino to start and keep a connection open even if the other device is not present?
Any advice on ways to resolve would be much appreciated.
Yes, programing section, I am seeking a programing solution because I can not control when and what order hardware is connected.
Yes, I am probably missing something(s). If I had known what the something(s) was I would not have posted for help.
So thanks for your totally useless answer. Maybe try to help next time or dont bother posting at all.
Incase anyone comes across a similar issue I will post what the issue was and how I resolved it.
The issue is the initial state of output pins on the Raspberry Pi due to the internal pull up/down resistors. During a boot up they default to the Broadcom chip default state before the software kicks in. The initial state of the pins was pulling both the RX and TX lines high. It holds the pins high for at least 1 second, maybe closer to 2 seconds.
On the Arduino, the serial line records this data as the RX line is high. In arduino code I am reading the serial data into an array variable where I need to pre-define the size. Under normal conditions the array is under 20 bytes. But during booting of the Pi at a baud rate 9600+ then 1 or 2 seconds is alot of data, way more than 20 bytes. I am not sure if it was overflowing the array into adjacent memory and corrupting things or if the serial buffer just ended up full and new data was not being received.
My fix is as follows. As I am recording data into the array, if the received data array counter is larger then 20 bytes (the maximum size I know I will receive) then I enter a small if loop where I just keep reading the serial data into a temporary value within the if loop until there is no more data to receive. This makes sure the array size is never exceeded and also ensures all the data from the serial line is removed so I can receive new data.
I can now plug in either piece of hardware first and it is working.
Is there a way to force the arduino to start and keep a connection open even if the other device is not present?
Serial.begin(baud rate); configures the serial port, that's all that's needed; there is nothing to keep 'open'.
Separately from that your code that uses the serial port might care about what it sees or fails to see coming in from the serial port, but that's an issue with your program, not the serial port.
In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless
If the code exceeds the 9000 character inline limit then attach it to a post