2

I need to count the number of bytes appearing in the serial (UART bus) bus, how to count bytes in Arduino with Arduino Serial functions? is this possible?

Application - RS485 communication with multiple boards without slave ID

slave boards will send data to the master board when,

1st 10 bytes = masters send a 10byte message to all slaves

slaves listening to the bus always until it is the turn to send the data

when byte count is 10, slave 1 sends data 5 bytes to master

when byte count is 15 (10+5), slave 2 sends data 5 bytes to master

when byte count is 20 (15+5), slave 3 sends data 5 bytes to master

max count is 25 known value

5
  • 1
    if (Serial.available() > 0) { incoming[byteCount] = Serial.read(); byteCount++; } where byteCount counts the number of bytes received and incoming[..] stores the received bytes, or something like that? Looks like a protocol that's not very fault-tolerant.
    – StarCat
    Commented Sep 18, 2020 at 11:07
  • @StarCat I am trying to achieve maximum data throughput. 1st master initiates the bus 10-byte message, all slaves listing to the bus, then slaves sending data when its turn comes. so I can reduce waste time on request response time, I have 20 slaves. maybe each slave send some checksum at last of each slaves data packet where the master can decode once data received
    – kobi89
    Commented Sep 18, 2020 at 11:15
  • 1
    Counting is easy, the hard part is knowing when to reset the count. Assume you will sometimes get out of sync. Commented Sep 18, 2020 at 13:25
  • @Chris Stratton what did you mean? reset count? i can reset each slave ones the data is sent right? other slaves will wait until there turn
    – kobi89
    Commented Sep 18, 2020 at 18:02
  • 1
    You need to have a reliable, recoverable way to know when to start counting. Assume the system will occasionally become mis-synchronized for one reason or another, you need to have a design which is self correcting and can get all of the nodes back on board and counting the next cycle together. Commented Sep 18, 2020 at 18:06

1 Answer 1

1

Serial.available() returns the number of bytes available for reading in the buffer.

int nBytes=Serial.available();
if (nBytes > x) {
............
............
............
}
1
  • 1
    Welcome to Arduino:SE. Just an observation: answers posted here should be written in English. Commented Dec 5, 2020 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.