Expanding the size of an existing array?


  • Plugin Developer

    After a lot of pulling hairs, I realised that the SoftwareSerial library has a hard coded 64 byte RX buffer.

    I originally thought I could do something like:

    #define _SS_MAX_RX_BUFF 255                         // Override the define to increase the buffer size
    #include <SoftwareSerial.h>                                // Load the library code
    SoftwareSerial softSerial1(GSM_RECEIVE_PIN,GSM_TRANSMIT_PIN); // Create the software serial object, hopefully with a bigger than normal buffer
    

    (updated the code to reflect that the #define came first)

    I assumed that other libraries would work like MySensors, and I could override this value. But it seems this override doesn't work.

    Now I could hardcode the bigger size into the library. But I was wondering: is there a way to do this without modifying the library code itself? Can I, for example, increase the size of the buffer after it's been created somehow?


  • Plugin Developer

    Trying some things.

    #include <SoftwareSerial.h>
    char SoftwareSerial::_receive_buffer[255];
    

    Results in error: conflicting declaration

    Strange, looking at the official code, it should support user overrides of this value.


  • Hardware Contributor

    not tried, but this should work better with the define before the include

    #define _SS_MAX_RX_BUFF 255                         // Override the define to increase the buffer size
    #include <SoftwareSerial.h>                                // Load the library code
    SoftwareSerial softSerial1(GSM_RECEIVE_PIN,GSM_TRANSMIT_PIN); // Create the software serial object, hopefully with a bigger than normal buffer
    

  • Plugin Developer

    I checked the IDE, and it also has the correct code:

    #ifndef _SS_MAX_RX_BUFF
    #define _SS_MAX_RX_BUFF 64 // RX buffer size
    #endif
    

    If I print out the value of _SS_MAX_RX_BUFF in the setup, then it shows the increased value.

    What could be causing this?


  • Plugin Developer

    @scalz Yes, you're right. That was how I had it originally (similar to how it works with mySensors), but that also doesn't work.


  • Hardware Contributor

    @alowhum oki.
    well I've no time to take a look, and not really a fan of softwareserial.. I just noticed the inversion in your 1st post
    So I guess you have few options if you can't find the solution:

    • change in lib (define, or constructor)
    • handle your own buffer in your sketch if you don't want to make changes to the lib


  • Perhaps the define is used in another place somewhere in your project also, which overrides yours. You could force it in the header file, or find the other define and change that


  • Plugin Developer

    Perhaps the define is used in another place somewhere in your project also

    I explored that too, but it's really not. It's all a one-file sketch.

    I have now embedded the entire software serial code in my code. It works now, even though it's unsightly.

    This hasn't been cleaned, but it might be interesting. It's upgraded code for the Candle Smart Lock, which can also be triggered via SMS (and this feature can be toggled).

    Apparently the code is too long to post
    


  • @alowhum said in Expanding the size of an existing array?:

    It's all a one-file sketch.

    Even it is only one file, the file can be included from other locations also. So is this #include <SoftwareSerial.h> also in other files inside your project?

    you can also check it by adding this before your define. If the compiler gives an error, this define is used somewhere already.

    #ifdef _SS_MAX_RX_BUFF
      #error "_SS_MAX_RX_BUFF defined already"
    #endif
    

  • Mod

    @alowhum I didn't check the code, but could the value of 255 cause an issue? Eg does it work when set to 200, or even 127?


  • Plugin Developer

    @electrik I'll have to test that. But there is only one file in my project, so..

    @Yveaux I tried that too (well, 256 and 128), but no effect.

    The full code (with built in software serial..) is now on Github:
    https://github.com/createcandle/Devices/blob/master/Smart_lock/Smart_lock.ino



  • I think this is your issue.

    If your "libraries" are truly libraries (i.e. - they consist of a .h file and a .cpp file), then they get compiled separately from the .ino, so they will never see ANY #defines you put in the .ino. The compiled libraries and compiled .ino file are joined only a link time, LONG after the #defines have been tossed away. If you want both the sketch and one or more libraries to "see" #defines, then those #defines must be defined in a separate .h file that is #included into both the sketch AND the libraries.

    From:

    https://forum.arduino.cc/index.php?topic=445646.0


  • Plugin Developer

    Ah, so that's it!

    So then is MySensors a special case? How is it possible that #defines in the sketch work for MySensors?



  • I think - am not sure - because you only include mysensors.h, which is only a header file without cpp files.


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 10
  • 1
  • 2
  • 3

68
Online

11.4k
Users

11.1k
Topics

112.7k
Posts