Can I send multiple data in one package?

Good day. Please help with Arduino sketch! I need to send a package of data via serial. Data consists of ID and massive of N elements. I mean i need to send data as
(ID & massive[50]). How can i realize this, i cant find it. Please help!

something like this assuming the type of massive knows how to be printed.

Serial.print(ID);
for (auto && m: massive) Serial.print(m);

if you want to send in binary:

Serial.write((const byte *) &ID, sizeof ID);
for (auto && m: massive) Serial.write((const byte *)&m, sizeof m);

Thanks. But if we use serial.print twice, to different packets of data. I need to send two parameters (ID and data) in the same packet.
I have one receiver and two senders. I need to sort received massives according to the ID of node.

Serial is -- as the name implies -- a serial data interface. It does not know the notion of packets.
Sending in one command a payload or with multiple commands, byte by byte just sends out the same thing...

➜ you define what protocol is to be used, if you use a start or end marker to build a frame / packet for your payload.

if you prefer having one command, just get the data together in a structure and send the structure in binary:

struct __attribute__((packed)) {
  uint32_t ID;
  uint8_t massive[50];
} myDataPacket  {10 , {0,1,2,3,4,....., 49}}; // fill it up I'm too lazy to type all the data

...

Serial.write((const byte *) & myDataPacket, sizeof myDataPacket);

This is what i need! Thanks a lot!

what happens on the serial line is the same wether you send it in one go or in multiple write(). The timing is guided by the baud rate and usually your prints happen faster than the actual buffered output of the bytes so there is not even a glitch in timing.

going to the structure only makes sense if conceptually the structure makes sense for your code.
Don't create one to copy existing data into the structure before sending it out. that would be wasting memory for nothing.

what happens if you miss a byte or or get UART overrun - you end with rubbish and have no idea where the next packet starts
for reliable serial communications you probably need to frame the data with start and end characters and also have a CRC check,
e.g. a frame is encoded so (with DLEs being byte stuffed)
SYN DLE STX byte stuffed data CRC DLE ETX
you can check for corrupt frames, lost bytes, etc and in case of problems reqyuest retransmission

Serial is not a packet protocol; it is a serial protocol that is represented as bits in a frame.

So many Internet expressions, but this is reasonably easy to understand https://learn.sparkfun.com/tutorials/serial-communication/all#:~:text=One%20of%20the%20more%20common,fast%20data%20can%20be%20transferred.

You will find some helpful ideas in the Serial Input Basics tutorial:

1 Like

post #1 talked of a package of data consisting of an ID and N elements - this is what I tend to to think of as a packet
how do you know when a packet (package) of data starts and ends?
how do you know if information is lost or corrupted?

IBM: Parameters used during serial communication include bits-per-character, bits-per-second (bps), baud rate, parity, and start, stop, and mark bits.

  • Bits-per-character
    The number of bits-per-character (bpc) indicates the number of bits used to represent a single data character during serial communication.
  • Bits-per-second (bps)
    This provides a description of the bits-per-second statistic.
  • Baud rate
    The baud rate is the number of times per second a serial communication signal changes states; a state being either a voltage level, a frequency, or a frequency phase angle.
  • Parity bits
    The parity bit, unlike the start and stop bits, is an optional parameter, used in serial communications to determine if the data character being transmitted is correctly received by the remote device.
  • Start, stop, and mark bits
    The start and stop bits are used in asynchronous communication as a means of timing or synchronizing the data characters being transmitted.

Typically in "old days" ack/nak and parity were utilized.
For Arduino, this lib is available (On your own as I have not used.) Or, you can write your own checksum routine.

GitHub - MatthewAHarvey/SerialChecker: An arduino serial handler which can use a checksum, ACK and NAK messages, and check for present messages/commands.

===> uint32_t ID;

Is it a tag or what?

What is "mark bit"?

A typical electrical/transmission representation of the above:
185-00

indeed… typing on phone…

It’s a GCC thingy. The packed type attribute specifies that a type must have the smallest possible alignment. See Type Attributes - Using the GNU Compiler Collection (GCC)

I was thinking of transmission of large amounts of data (possibly binary) in frames
e.g. thinking back to IBM's BISYNC byte oriented protocol or even IBM's HDLC bit oriented protocol
I tend to use similar techniques when transmitting raw sensor data in binary from microcontrollers to PCs, smart phones, etc.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.