hi, i've been trying to send an array of bytes with 108 buffer size through serial rs485. what happened is, it is only send the last 44 buffer data and sometimes it merged with the next buffer. does anyone have any idea on how to send large bugger through serial?
p.s i try to use serial flush after a chunk of buffer, but it doesn't sent instead and only the last chunk that have been sent
The Arduino has print buffers for both rece3ive and transmit. You are probably overflowing one or the other or both. Try cutting your packet size to about 30 bytes and see if it works, if it does that would be the problem.
okay, i apologize for the minimum information.... i'm using STM32 microcontroller board with arduino ide. i've been trying to commuicate two board by sending 108 bytes of data using serial with rs485 module, here's the wiring (both receiver and sender):
i've try it, i divide the packet size into 3 chunk with 36 bytes each with serial.flush() between each chunk... and the receiver only receive the last chunk
i also try removing the serial.flush() but there are no different as well
i use serial.available() and serial.read() to receive the data sent by sender. and on receiver side i serial.print() the received data, there are only 44 data printed in serial monitor
can usually ties these 2 together and use 1 pin..
add a Serial2.flush() just before postTransmission to make sure all has been sent before disabling the driver..
and really you should be able to write it all it once..
Serial2.write(data,sizeof(data));
I'm actually kind of surprised you got anything..
Driver enable must be high (enabled) to transmit..
Receive enable should be high (disabled) else you recv what you trans..
Receive enable's logic is reversed from the Driver enable..
I don't have RS485 modules so I just used the code without the additional code for the RS485. I also don't have your boards so I used a Mega as the sender and a Leonardo as the receiver (both using Serial1). I can not reproduce your problem.
One thing that I noticed is that you're spamming the serial monitor with empty lines after a timeout. I suggest that you use a flag to only print one new line after a timeout; something like below for the receiver.
void loop()
{
static bool received = false;
if (Serial1.available() > 0)
{
received = true;
Serial.print(Serial1.read());
Serial.print(" ");
prevMillis = millis();
}
else if (millis() - prevMillis >= 1000)
{
if(received == true)
{
Serial.println();
received = false;
}
}
}
There is one point that might be important and that is interrupt priorities. I have had serious problems on a Mega to receive blocks of data (200 bytes at 100000 baud nearly continuously) and trying to print them. The reason was that the Serial Tx interrupt had a higher priority than the Serial1 Rx interrupt; eventually I only printed new data if it had changed.
hi @qubits-us ,
yes, i used to do this on my ESP32 or Arduino UNO
but when it comes to STM32 (STM32F407VG) board i got nothing, so i experimented with the DE and RE pin until i got something on the receive side which result on this:
okay, let me try this on some other board as well to see wether it is the board or the module that affect serial communication.
also, the interrupt priorities you stated might do something in this matter i think i've to include this as well
thinking why it's working at all..
that pre, shuts down the 485 completely..
you then send out 64 bytes and then flush them away..
the next 44 bytes are sent for transmission, no flush..
then your post, turns the 485 back on for transmission and reception..
luckily the last 44 bytes which weren't flushed get sent..
that's my observations anyways..
I'm with @qubits-us here. RE & DE are usually either tied together or if separately controlled, both high or both low. The only time I've had RE != DE is when I wanted to hear my own transmission.
Have you tried the comms without the RS485 line drivers?
I'm guessing that the STM32F407VG is a 3.3v device. Which RS485 line driver chip are you using?
To be clear, many of the MAX485 piggyback adapters from Amazon, AliExpress, etc. have very poor (misleading) labelling. Often, both RE and DE are labeled exactly that way, with no indication that RE is in fact a low-enable input, vs the high-enable for DE.
The MAX485 diagram, from the AnalogDevices datasheet:
Note how the RE is an inverted input, while the DE is not. When we tie them together and drive from one output, which is standard practice, either Driver or Receiver is enabled, and the other is the opposite. This creates a 'half duplex' connection arrangement, where the Arduino can talk, or Receive, but not both. Thus, when you set one low and one high, you've enabled or disabled both at the same time.
okay, so i grab a new pair of rs485 module, change receiver board with ESP32 and also i change my code on the pre/post transmission like @qubits-us shown.
i tested by sending 64 bytes of data using rs485 module from STM32 to ESP32, here's the code on the sender side:
it only receive until 62nd array, the 63rd array and so on isn't received by receiver. does anyone got any idea if i want to send more than 63 array size?
hello, thank you everyone for the answers
i somehow managed to make it work. i reckon the problem was the first RS485 module, after getting a new module and changing the MAX485 chip it works now.
also i think it's because the message interval as well, i set the interval to 3 seconds and it works fine (maybe because of the 9600 baudrate).
and just like @markd833 stated i'm adding Serial.flush() after each transmission.
here's the final code, just in case someone stumble into similar problem