-2

I have asked a question here and now the code below makes the work that I expected (the work that I mentioned previously). Now I have another issue: if I write tmp[20] and if the size of the input is 20 chars, the code works. However the size of the input is not known. Therefore, there can be a maxsize, but the actual size depends on the input. How can I make this code work with every length? (yes, the code works when I write 200 inside of tmp but the size of 'message' depends on the whole array. It should be 3 chars before end and 6 chars after start).Therefore when I write 200 inside of tmp[] and the input is shorter than 200 chars, the message returns incorrectly. I think I should use dynamic memory allocation but I could not implement it to my code.

Here is my complete code:

char tmp[20] = { 0 };
int len = sizeof(tmp) / sizeof(tmp[0]);
String pack;
String command;
String Size;
String messum;
String message;
String checksum;

int Index = 0;
bool Seen = false;

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

void loop() {  
    while (Serial.available() > 0) {
        char received = Serial.read();

        if (received == '!') {
            Seen = true;
        } else
        if (received == '#') {
            return strdup(tmp);
        } else
        if (Seen == true) { 
            if (Index < 2) {
                //Serial.print("P");
                pack = tmp[Index++] = received;
                Serial.print(pack);
            } else
            if (Index < 4) {
                //Serial.print("C");
                command = tmp[Index++] = received;
                Serial.print(command);
            } else
            if (Index < 6) {
                //Serial.print("S");
                Size = tmp[Index++] = received;
                Serial.print(Size);
            } else
            if (Index < (len - 3)) {
                //Serial.print("M");
                message = tmp[Index++] = received;
                Serial.print(message);
            } else
            if (Index < len) {
                //Serial.print("C");
                checksum = tmp[Index++] = received;
                Serial.print(checksum);
            }  
        }
    }
    return NULL;
    //input:  asdeyh134!123456789abcdefghtry#8647dfn
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Why can't you use dynamic memory allocation? – CRM Apr 11 '20 at 14:43
  • I can use but I could not implement it to my code. I am quite beginner – ReduX Completedy Apr 11 '20 at 14:44
  • You must still fix the error `return strdup(tmp);` as `loop` is `void`. – Paul Ogilvie Apr 11 '20 at 14:44
  • It is not an error when I delete that part the output does not stop when # is seen – ReduX Completedy Apr 11 '20 at 14:45
  • There are plenty of examples online ([here](https://www.educative.io/edpresso/how-to-use-malloc-in-c)) and on SO, as for instance [this](https://stackoverflow.com/questions/41830461/allocating-string-with-malloc), ... – CRM Apr 11 '20 at 14:47
  • Ok but how can I know n without knowing the input? – ReduX Completedy Apr 11 '20 at 14:48
  • Since your array is going to contain character strings, then just keep the string null terminated by always writing a 0 to the next spot when you add a character. Then you can use strlen to get the length of the string in the array. It will count up to that null character. – Delta_G Apr 11 '20 at 16:29
  • @Delta_G Where should I add \0 can you write an example? – ReduX Completedy Apr 11 '20 at 16:58
  • Anywhere you write any character to the end of your string. Then the next line should write 0 to the next slot of the array. Can you identify the lines in your code that write values into your array? – Delta_G Apr 11 '20 at 17:56

1 Answers1

0

Anywhere you are adding characters to your array like this:

if (Seen == true) { 
            if (Index < 2) {
                //Serial.print("P");
                pack = tmp[Index++] = received;
                Serial.print(pack);

You should also null terminate your string like so:

if (Seen == true) { 
            if (Index < 2) {
                //Serial.print("P");
                pack = tmp[Index++] = received;
                tmp[Index] = 0;
                Serial.print(pack);

That way you can use strlen to get the length of the string in the array. It will count the number of characters up to that null character.

int length = strlen(tmp);

On the next pass of the loop when you write in another character you will write it over that null and then write a new null right after that new character.

Delta_G
  • 2,927
  • 2
  • 9
  • 15