Skip to main content
added 271 characters in body
Source Link
st2000
  • 7.5k
  • 2
  • 13
  • 20

If your primary concern is transmission speed, you might consider turning everything into bytes and reversing the process at the other end. If speed is not important, you can use sprintf() to create a character string and sscanf() to parse out the character string at the other end.

Turning everything into bytes is a very custom / specific approach. A character would turn into 1 byte, an integer into 2 bytes, ect. You would have to write custom code to encode this and remember to write code at the other end to do exactly the reverse process. The advantage is that you would only be sending your data and no other information. But this approach is not vary flexible. If you were able to fit your data into 1 byte then suddenly needed 2 bytes (for example, a value once always less than 255 was now 300), you would have to re-write your code at both ends of the connection.

Turning everything into a string with sprintf() is very similar to printing out all your data. You will be including extra characters like commas and / or space for delineation. At the other end sscanf() is used to parse out the character string. At both ends, if done properly, all string assembly and parsing should be done by only these 2 calls. And some issues are taken care of automatically. For instance, the number 100 is 3 characters long and the number 10 is 2 character long. But the sprintf() and sscanf() calls automatically compensate for these (reasonable) variations and continue to work as expected.

added later...

FYI: Even though sscanf() is available for the Arduino, it is not clear to me if it is well supported. You might try testing (create and parse a string in the same sketch) what you want to send / receive before actually committing to this method.

If your primary concern is transmission speed, you might consider turning everything into bytes and reversing the process at the other end. If speed is not important, you can use sprintf() to create a character string and sscanf() to parse out the character string at the other end.

Turning everything into bytes is a very custom / specific approach. A character would turn into 1 byte, an integer into 2 bytes, ect. You would have to write custom code to encode this and remember to write code at the other end to do exactly the reverse process. The advantage is that you would only be sending your data and no other information. But this approach is not vary flexible. If you were able to fit your data into 1 byte then suddenly needed 2 bytes (for example, a value once always less than 255 was now 300), you would have to re-write your code at both ends of the connection.

Turning everything into a string with sprintf() is very similar to printing out all your data. You will be including extra characters like commas and / or space for delineation. At the other end sscanf() is used to parse out the character string. At both ends, if done properly, all string assembly and parsing should be done by only these 2 calls. And some issues are taken care of automatically. For instance, the number 100 is 3 characters long and the number 10 is 2 character long. But the sprintf() and sscanf() calls automatically compensate for these (reasonable) variations and continue to work as expected.

If your primary concern is transmission speed, you might consider turning everything into bytes and reversing the process at the other end. If speed is not important, you can use sprintf() to create a character string and sscanf() to parse out the character string at the other end.

Turning everything into bytes is a very custom / specific approach. A character would turn into 1 byte, an integer into 2 bytes, ect. You would have to write custom code to encode this and remember to write code at the other end to do exactly the reverse process. The advantage is that you would only be sending your data and no other information. But this approach is not vary flexible. If you were able to fit your data into 1 byte then suddenly needed 2 bytes (for example, a value once always less than 255 was now 300), you would have to re-write your code at both ends of the connection.

Turning everything into a string with sprintf() is very similar to printing out all your data. You will be including extra characters like commas and / or space for delineation. At the other end sscanf() is used to parse out the character string. At both ends, if done properly, all string assembly and parsing should be done by only these 2 calls. And some issues are taken care of automatically. For instance, the number 100 is 3 characters long and the number 10 is 2 character long. But the sprintf() and sscanf() calls automatically compensate for these (reasonable) variations and continue to work as expected.

added later...

FYI: Even though sscanf() is available for the Arduino, it is not clear to me if it is well supported. You might try testing (create and parse a string in the same sketch) what you want to send / receive before actually committing to this method.

Source Link
st2000
  • 7.5k
  • 2
  • 13
  • 20

If your primary concern is transmission speed, you might consider turning everything into bytes and reversing the process at the other end. If speed is not important, you can use sprintf() to create a character string and sscanf() to parse out the character string at the other end.

Turning everything into bytes is a very custom / specific approach. A character would turn into 1 byte, an integer into 2 bytes, ect. You would have to write custom code to encode this and remember to write code at the other end to do exactly the reverse process. The advantage is that you would only be sending your data and no other information. But this approach is not vary flexible. If you were able to fit your data into 1 byte then suddenly needed 2 bytes (for example, a value once always less than 255 was now 300), you would have to re-write your code at both ends of the connection.

Turning everything into a string with sprintf() is very similar to printing out all your data. You will be including extra characters like commas and / or space for delineation. At the other end sscanf() is used to parse out the character string. At both ends, if done properly, all string assembly and parsing should be done by only these 2 calls. And some issues are taken care of automatically. For instance, the number 100 is 3 characters long and the number 10 is 2 character long. But the sprintf() and sscanf() calls automatically compensate for these (reasonable) variations and continue to work as expected.