Author Topic: Maximum string length on one line with Arduino IDE?  (Read 41733 times)

0 Members and 1 Guest are viewing this topic.

Offline Electro FanTopic starter

  • Super Contributor
  • ***
  • Posts: 3199
Maximum string length on one line with Arduino IDE?
« on: November 09, 2015, 11:54:32 pm »
Saw this:

Wrapping long strings
You can wrap long strings like this:
char myString[] = "This is the first line"
" this is the second line"
" etcetera";


It would indicate that perhaps there is a limit to how may characters can be on string in one line (without a wrap)?

If so, what is the limit?

Thanks
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Maximum string length on one line with Arduino IDE?
« Reply #1 on: November 09, 2015, 11:56:56 pm »
It would indicate that perhaps there is a limit to how may characters can be on string in one line (without a wrap)?

I doubt it.  It just makes it more readable (tidier) that way.
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline nuno

  • Frequent Contributor
  • **
  • Posts: 606
  • Country: pt
Re: String length on one line with Arduino IDE?
« Reply #2 on: November 10, 2015, 12:00:21 am »
That is just C/C++ syntax for you to be able to break a string and continue on the next source code line, for reasons such as readability. For example, an HTTP request template string could be stored like this

const char* req = "GET %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n";

or

const char* req = "GET %s HTTP/1.0\r\n"
                  "Host: %s\r\n"
                  "Connection: close\r\n"
                  "\r\n";


These 2 forms are equivalent in terms of the program the compiler generates.

There are string size limits, but I think it is more related to the resources on the specific platform (RAM on the target machine) than the compiler.
« Last Edit: November 10, 2015, 12:02:17 am by nuno »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11261
  • Country: us
    • Personal site
Re: Maximum string length on one line with Arduino IDE?
« Reply #3 on: November 10, 2015, 12:07:42 am »
And as far as C / C++ goes, standards compliant compiler must be able to compile a program containing up to
Quote
— 4095 characters in a logical source line
— 4095 characters in a character string literal or wide string literal (after concatenation)
for C and
Quote
— Characters in one logical source line [65 536].
— Characters in a character string literal or wide string literal (after concatenation) [65 536].
for C++.

LE: Arduino uses C++, so the later limits apply.
Alex
 

Offline Electro FanTopic starter

  • Super Contributor
  • ***
  • Posts: 3199
Re: Maximum string length on one line with Arduino IDE?
« Reply #4 on: November 10, 2015, 02:03:31 am »
Thanks

New question:

In a wire.write it seems fine to include a character such as:

    if(c == 'H')
    {
      Wire.beginTransmission(5);
      Wire.write('H');
      Wire.endTransmission();


If instead of a character a string is inserted is there any limit on the length of the string?  Such as:

    if(c == 'H')
    {
      Wire.beginTransmission(5);
      Wire.write("ABCDEFGJIJKLMNOPQRSTUV");
      Wire.endTransmission();


Thanks
 

Offline nuno

  • Frequent Contributor
  • **
  • Posts: 606
  • Country: pt
Re: Maximum string length on one line with Arduino IDE?
« Reply #5 on: November 10, 2015, 02:30:28 am »
You'll have to check the Wire library's documentation.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Maximum string length on one line with Arduino IDE?
« Reply #6 on: November 10, 2015, 02:38:21 am »
+1 for check the docs!

A character constant is *NOT* a string, and cannot be substituted for one except if the function is overloaded and has implementations for both parameter types.  Google: C++ function overloading
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Maximum string length on one line with Arduino IDE?
« Reply #7 on: November 10, 2015, 02:46:51 am »
Quote
Arduino uses C++, so the later limits apply.
I would expect any practical limit to be in the IDE/Editor, rather than in the C++ Compiler.
(Although, people who write source code with lines exceeding 4000 characters should be shot.  You have ";" to terminate logical lines, so you don't need to be ridiculous!)
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Maximum string length on one line with Arduino IDE?
« Reply #8 on: November 10, 2015, 03:12:41 am »

    if(c == 'H')
    {
      Wire.beginTransmission(5);
      Wire.write("ABCDEFGJIJKLMNOPQRSTUV");
      Wire.endTransmission();



From looking at the header file in 1.6.5 ({path_to}/Arduino/hardware/avr/libraries/Wire/Wire.h) the Wire library does not support the accepting of a C style null-terminated string without a length argument, so the point is moot.

However if in some future version it did support sending a C style null-terminated string without a length argument, it would be a very unusual implementation if it had an artificial limit on the sting size - but of course your hardware imposes a practical physical limit, because your memory (both flash and sram) is extremely limited.


Wire.h (in 1.6.x {path_to}/Arduino/hardware/avr/libraries/Wire/Wire.h) defines a buffer length of 32 bytes, and it pulls in Print.h's write() implementation for sizes which will effectively fill the buffer and discard the rest. 

Because of the wire libraries method of sending only a full buffer when the endTransmission() call is made, you are limited to 32 characters (bytes) sending for a single transmission with the Wire library.

« Last Edit: November 10, 2015, 05:42:23 am by sleemanj »
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Electro FanTopic starter

  • Super Contributor
  • ***
  • Posts: 3199
Re: Maximum string length on one line with Arduino IDE?
« Reply #9 on: November 10, 2015, 03:30:04 am »
Thanks

Background - I'm at the very beginning of Uno (or any) programming.

More background - For the moment I'm not trying to program, I'm just trying to create a way to send some text (a string, I guess) from one Uno to another via I2C so I can create a "test pattern" to probe with a scope.

So, I found the Uno sketch that showed how to cause one Uno to communicate to another Uno via I2C.  The sketch called for entering H and sending a H for High and entering L and sending L for Low.  I was able to decode the H and L into ASCII and Binary but I found that monitoring one character wasn't too interesting.  So I tried substituting the transmitted H for several characters and discovered what must be programming lesson #1 - the need to enclose a string of characters with " " instead of ' ' for a single character.  This worked fine up to a fairly limited number of characters in the string.  Which is what started the questions in this thread.  So, for now, all I want to do is to add lots of text (hundreds, thousands, 10s of thousands of text characters) to the string so when I hit H I will be able to monitor the long I2C transmission with the scope.

Any suggestions (beyond reading the docs - which I am doing but so far I must be reading the wrong ones)?

Thanks again
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Maximum string length on one line with Arduino IDE?
« Reply #10 on: November 10, 2015, 03:57:10 am »
The Wire library for the master transmits only a full buffer at a time, the transmission only happens on endTransmission (for the master).  The buffer is 32 bytes long as defined in Wire.h

   Wire.write("HELLO WORLD THE QUICK BROWN FOX", 31);

instead of your Wire.write('H') should probably work, note double quotes, note the length of the string (31) and note that the length is less than 32.


~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Maximum string length on one line with Arduino IDE?
« Reply #11 on: November 10, 2015, 04:23:47 am »
And if you don't have a DSO, to get a stable trace on an analog scope for you to pick through, wrap that in a while(1){ ... } loop, and pulse a spare output pin before calling Wire.write, so you've got a signal that occurs ONCE per transmission block to feed to your scope's trigger input.  Once you have a reliable trigger point, even with an old 50MHz 'boatanchor', as long as its got two channels and a delay timebase with a smooth-acting vernier knob, you can pick your way through the data packet bit by bit decoding it with pencil and squared paper.

Its all a lot easier with a DSO, as you can do a one-shot capture, and if its a fancy one with a protocol analyser, it can even decode the I2C bytes for you - *IF* you know how to set up that feature.
 

Offline Electro FanTopic starter

  • Super Contributor
  • ***
  • Posts: 3199
Re: Maximum string length on one line with Arduino IDE?
« Reply #12 on: November 10, 2015, 04:43:56 am »
So, 32 Bytes maximum with wire.write?  That would explain what I have been seeing (short strings).

I have a DSO and decoding is pretty straight forward.

Any suggestions on how to take a different approach than wire.write (adjust the small piece of code that I have so far below) that would enable much longer text strings to be sent via I2C?  Thanks

//i2c Master Code(UNO)
#include <Wire.h>

void setup()
{
  Serial.begin(9600);
 
  Wire.begin();
}

void loop()
{
  while(Serial.available())
  {
    char c = Serial.read();
   
    if(c == 'H')
    {
      Wire.beginTransmission(5);
      Wire.write("ABCDEFGJIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
      Wire.endTransmission();
    }
    else if(c == 'L')
    {
      Wire.beginTransmission(5);
      Wire.write('L');
      Wire.endTransmission();
    }
  }
}

----

PS, Where in the library does it say 32 Bytes is the max?  Thx
https://www.arduino.cc/en/Reference/Wire
« Last Edit: November 10, 2015, 04:46:33 am by Electro Fan »
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Maximum string length on one line with Arduino IDE?
« Reply #13 on: November 10, 2015, 05:38:26 am »
The 32 byte buffer is defined in Wire.h where it says #define BUFFER_LENGTH 32

Your

  Wire.write("ABCDEFGJIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

"works" because I missed that Wire.h also pulls in the write function from Print.h which defines the write() function for C style null terminated strings, so Wire.write() also supports them, PROVIDED the length is 32 characters or less, any more than 32 characters will be discarded.



~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline Electro FanTopic starter

  • Super Contributor
  • ***
  • Posts: 3199
Re: Maximum string length on one line with Arduino IDE?
« Reply #14 on: November 10, 2015, 07:38:20 pm »
The 32 byte buffer is defined in Wire.h where it says #define BUFFER_LENGTH 32

Your

  Wire.write("ABCDEFGJIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

"works" because I missed that Wire.h also pulls in the write function from Print.h which defines the write() function for C style null terminated strings, so Wire.write() also supports them, PROVIDED the length is 32 characters or less, any more than 32 characters will be discarded.

I believe it is 32 characters or less.  Any suggestions on how to adjust the code above to use something other than Wire.write - or is there another sketch available (any link?) that would make it relatively easy to send many more (hundreds, thousands, etc) via I2C?  Could be from a PC to one Uno, or from a PC for transmission between two Unos.  Thanks
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1641
  • Country: nl
Re: Maximum string length on one line with Arduino IDE?
« Reply #15 on: November 10, 2015, 08:07:10 pm »
String formatted over multiple lines makes sense if you enter newlines in the string (like the HTTP example).

Moreover some coding standards impose a maximum of 80 characters wide per line.
This is artificial, i.e. not limited by the machine but instead by humans. Ever had a code file that not only requires vertical scrolling, but also horizontal? PITA.

Read material
 

Offline Electro FanTopic starter

  • Super Contributor
  • ***
  • Posts: 3199
Re: Maximum string length on one line with Arduino IDE?
« Reply #16 on: November 10, 2015, 08:18:29 pm »
String formatted over multiple lines makes sense if you enter newlines in the string (like the HTTP example).

Moreover some coding standards impose a maximum of 80 characters wide per line.
This is artificial, i.e. not limited by the machine but instead by humans. Ever had a code file that not only requires vertical scrolling, but also horizontal? PITA.

Read material

Hans, yes I get that 80 characters is a typical line length - it makes for good reading by a human.  Humans don't like to scroll horizontally, fully agreed.

The purpose for this particular project, however, is not to generate human readable code or text.  The purpose of the project is to test the capacity of a Logic Analyzer (actually an oscilloscope).  So no one will have to "read" the code or the output. 

All I want to figure out is an easy way to send text from an IDE to an Arduino Uno and from the Arduino Uno out of it's I2C pins (perhaps to another Arduino Uno). I just need a lot of text (tens of thousands of characters) so I can probe the transmission of the characters on the I2C (SDA and SCL) pins.  It's just a matter of knowing what statements to use in the IDE so I can cut and paste a few pages of text from War and Peace (or any other text) into the sketch and not hit a character limit during the I2C transmission of the data (the 1s and 0s that comprise ASCII text).

Thx, EF
« Last Edit: November 10, 2015, 08:24:28 pm by Electro Fan »
 

Offline DimitriP

  • Super Contributor
  • ***
  • Posts: 1307
  • Country: us
  • "Best practices" are best not practiced.© Dimitri
Re: Maximum string length on one line with Arduino IDE?
« Reply #17 on: November 10, 2015, 08:41:58 pm »
Perhaps I'm missing something too obvious by why can't the "transmission" code be put in a loop so the same "short" string is being transmitted over and over?
   If three 100  Ohm resistors are connected in parallel, and in series with a 200 Ohm resistor, how many resistors do you have? 
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1641
  • Country: nl
Re: Maximum string length on one line with Arduino IDE?
« Reply #18 on: November 10, 2015, 08:44:33 pm »
If you're testing the instruments functionality, why not send out Hello World! repeatedly?

Code: [Select]
void loop()
{
  Wire.beginTransmission(...);
  for (int i = 0; i < 250; i++)
  {
    Wire.write("Hello World!");
  }
  Wire.endTransmission();
}
The Hello World! string is 12 characters, times 250 is 3000 characters of text.

I imagine you want to see what happens if you decode the I2C midstream. I know that my Rigol DS1074Z (old firmware nevertheless) can only decode what is on screen, and wont do a good job if the start bit is not on screen or you're far zoomed out.
 

Offline DimitriP

  • Super Contributor
  • ***
  • Posts: 1307
  • Country: us
  • "Best practices" are best not practiced.© Dimitri
Re: Maximum string length on one line with Arduino IDE?
« Reply #19 on: November 10, 2015, 08:49:33 pm »
If you're testing the instruments functionality, why not send out Hello World! repeatedly?

Code: [Select]
void loop()
{
  Wire.beginTransmission(...);
  for (int i = 0; i < 250; i++)
  {
    Wire.write("Hello World!");
  }
  Wire.endTransmission();
}
The Hello World! string is 12 characters, times 250 is 3000 characters of text.

I imagine you want to see what happens if you decode the I2C midstream. I know that my Rigol DS1074Z (old firmware nevertheless) can only decode what is on screen, and wont do a good job if the start bit is not on screen or you're far zoomed out.

Maybe you are missing something obvious too :) Can't wait to find out what it is.
   If three 100  Ohm resistors are connected in parallel, and in series with a 200 Ohm resistor, how many resistors do you have? 
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Maximum string length on one line with Arduino IDE?
« Reply #20 on: November 10, 2015, 08:50:25 pm »
All I want to figure out is an easy way to send text from an IDE to an Arduino Uno and from the Arduino Uno out of it's I2C pins (perhaps to another Arduino Uno).

You need to learn about loops; for, while, and do-while

You also seem to not realise how little memory your arduino has, you simply can not handle "10s of thousands" of unique hard coded character sequences.  Your simplest solution is just to put your i2c begin-write-end logic in a loop.
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Maximum string length on one line with Arduino IDE?
« Reply #21 on: November 10, 2015, 08:52:08 pm »
If you're testing the instruments functionality, why not send out Hello World! repeatedly?

Code: [Select]
void loop()
{
  Wire.beginTransmission(...);
  for (int i = 0; i < 250; i++)
  {
    Wire.write("Hello World!");
  }
  Wire.endTransmission();
}
The Hello World! string is 12 characters, times 250 is 3000 characters of text.

I imagine you want to see what happens if you decode the I2C midstream. I know that my Rigol DS1074Z (old firmware nevertheless) can only decode what is on screen, and wont do a good job if the start bit is not on screen or you're far zoomed out.

Right idea. Wrong implementation, as I recall the buffer is only sent and emptied on endTransmission, once buffer is full, rest is discarded.

In other words, move the begin and end inside the loop also :-)
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline DimitriP

  • Super Contributor
  • ***
  • Posts: 1307
  • Country: us
  • "Best practices" are best not practiced.© Dimitri
Re: Maximum string length on one line with Arduino IDE?
« Reply #22 on: November 10, 2015, 09:03:37 pm »
If you're testing the instruments functionality, why not send out Hello World! repeatedly?

Code: [Select]
void loop()
{
  Wire.beginTransmission(...);
  for (int i = 0; i < 250; i++)
  {
    Wire.write("Hello World!");
  }
  Wire.endTransmission();
}
The Hello World! string is 12 characters, times 250 is 3000 characters of text.

I imagine you want to see what happens if you decode the I2C midstream. I know that my Rigol DS1074Z (old firmware nevertheless) can only decode what is on screen, and wont do a good job if the start bit is not on screen or you're far zoomed out.

Right idea. Wrong implementation, as I recall the buffer is only sent and emptied on endTransmission, once buffer is full, rest is discarded.

In other words, move the begin and end inside the loop also :-)


I liked your previous advice better: 
Quote
You need to learn about loops; for, while, and do-while



   If three 100  Ohm resistors are connected in parallel, and in series with a 200 Ohm resistor, how many resistors do you have? 
 

Offline Electro FanTopic starter

  • Super Contributor
  • ***
  • Posts: 3199
Re: Maximum string length on one line with Arduino IDE?
« Reply #23 on: November 10, 2015, 09:20:08 pm »
Perhaps I'm missing something too obvious by why can't the "transmission" code be put in a loop so the same "short" string is being transmitted over and over?

That is a very fair question.  The answer is that by sending the same short transmission over and over it is pretty hard to know which of the transmissions is being decoded and viewed on the oscilloscope.  By sending a long text with reasonably unique paragraphs, sentences, and words it is much easier to verify where in the long string a particular set of characters resides.  Not only can you use a word processor to search for text location (if need be), but after a few hours of tedious examination you can pretty much know the document and the relative text location by human memory, if not recite it  :)
« Last Edit: November 10, 2015, 09:22:09 pm by Electro Fan »
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1641
  • Country: nl
Re: Maximum string length on one line with Arduino IDE?
« Reply #24 on: November 10, 2015, 09:40:09 pm »
If you're testing the instruments functionality, why not send out Hello World! repeatedly?

Code: [Select]
void loop()
{
  Wire.beginTransmission(...);
  for (int i = 0; i < 250; i++)
  {
    Wire.write("Hello World!");
  }
  Wire.endTransmission();
}
The Hello World! string is 12 characters, times 250 is 3000 characters of text.

I imagine you want to see what happens if you decode the I2C midstream. I know that my Rigol DS1074Z (old firmware nevertheless) can only decode what is on screen, and wont do a good job if the start bit is not on screen or you're far zoomed out.

Right idea. Wrong implementation, as I recall the buffer is only sent and emptied on endTransmission, once buffer is full, rest is discarded.

In other words, move the begin and end inside the loop also :-)

Okay, sorry I didn't run the code. I also don't own an Arduino to do so. So yeah.. I basically assumed write() is a completely synchronous operation.

If that isn't the case I would quickly discard such a library and instead start bit-banging pins or find/write other I2C routines.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf