Little problem with gobetwino

I'm using gobetwino to read and write data to a textfile.
I have written a matlab program who write the values that I need in a textfile (output.txt).
After reading the values from the textfile, I want to write the same values in another textfile (logtest.txt).
The goal of this is to know the values per day and per hour, because I overwrite the values in the textfile (output.txt) where I read the values into arduino.
The code that I'm used is showed below.
The values from the output.txt are different of the values in the logtest.txt file.
What can be the reason?

int serInLen = 25;
int ledGreen = 8; // Groene led op pin 13
int ledRed = 4;  // Rode led op pin 4
char serInString[25];

void setup()                   
{
  pinMode(ledGreen, OUTPUT);  
  pinMode(ledRed, OUTPUT);
  Serial.begin(9600);
  //Serial.println("#S|OPENCAMERA|[]#");
  Serial.println("#S|OPNMATLAB|[]#");
}

void loop()     
  
{
  char buffer[12];  // long data type has 11 characters (including the minus sign) and a terminating null
  int aardbei;
  int lineNr;

        for(lineNr=1;lineNr<=3;lineNr++) 
        {
                                      
        Serial.print("#S|LEESWITHE|[");           //Leest de waarden binnen van matlab 
        Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
        Serial.println("]#");                     //Want er kunnen enkel string-waarden doorgegeven worden naar Gobetwino 
        readSerialString (serInString, 10000);    
        aardbei = atoi(serInString);    //Converteert een sting naar een integer
                  
         if (aardbei > 0) 
        {
        digitalWrite(ledGreen, HIGH);
        digitalWrite(ledRed, LOW);      
        }
        
        else if (aardbei <= 0)
        {
        digitalWrite(ledGreen, LOW);
        digitalWrite(ledRed, HIGH);  
        }
        
        Serial.print("#S|LOGTEST|[");                    //schrijft de waarden van matlab weg naar een textbes
        Serial.print(itoa((aardbei), buffer, 10));
        Serial.println("]#");
   }
}


void readSerialString (char *strArray,long timeOut) {
  // Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}

values of the output.txt are:
83 116
148 140
150 89

values of the logtest.txt are:
1/04/2013 17:22:03;3
1/04/2013 17:22:03;0
1/04/2013 17:22:03;0
1/04/2013 17:22:03;3
1/04/2013 17:22:03;0
1/04/2013 17:22:04;0
1/04/2013 17:22:04;3
1/04/2013 17:22:04;0
1/04/2013 17:22:04;-3
1/04/2013 17:22:04;3
1/04/2013 17:22:05;-3
1/04/2013 17:22:05;33
1/04/2013 17:22:05;3
1/04/2013 17:22:05;-3
1/04/2013 17:22:06;33
1/04/2013 17:22:06;3
1/04/2013 17:22:06;-3
1/04/2013 17:22:06;33
1/04/2013 17:22:06;3
1/04/2013 17:22:07;-3
1/04/2013 17:22:08;33

What are you seeing in the GoBetwino monitor?

Just a quick look would seem that maybe your readserialstring is timing out and returning? It doesn't look like you are checking the data for validity or for timeout, but just sending it off to matlab. So, if it were to time out and return, it would send whatever junk was stored in that portion of memory taken up by your string buffer.

PaulS:
What are you seeing in the GoBetwino monitor?

PaulS:
What are you seeing in the GoBetwino monitor?

Values of the logtest.txt:
2/04/2013 20:49:38;66
2/04/2013 20:49:39;6
2/04/2013 20:49:41;26
2/04/2013 20:49:42;66
2/04/2013 20:49:43;6
2/04/2013 20:49:44;26
2/04/2013 20:49:44;66
2/04/2013 20:49:45;6
2/04/2013 20:49:46;26
2/04/2013 20:49:47;66
2/04/2013 20:49:47;6
2/04/2013 20:49:48;26
2/04/2013 20:49:48;66
2/04/2013 20:49:49;6
2/04/2013 20:49:50;26

values of the output.txt:
26
146
33
211
34
236
41
183
64
235
150
155

Those messages about reading the file generating an exception don't bother you?

You are generating an exception when reading your output.txt file. It looks like that file is still open by another process. I would imagine that you never closed the file in matlab.

First of all, thanks for your help :slight_smile:

That can be the reason.. I'm looking for it

fid = fopen('C:\Users\Joris\Documents\MATLAB\output.txt','wt');
for teller = 1:aantalAardbeien,
aardbeiX = zwaartepunten(teller,1);
aardbeiY = zwaartepunten(teller,2);
fprintf(fid, '%s\n', num2str(aardbeiX));
fprintf(fid, '%s\n', num2str(aardbeiY));
end

fid(close);

This is the matlab code. So you can see... The fill will be closed.
Maybe the problem is the loop in matlab who is to fast?

I would bet that your arduino code is running faster than the matlab code can execute. Put a delay at the end of you setup to test that theory.

void setup()                   
{
  pinMode(ledGreen, OUTPUT);  
  pinMode(ledRed, OUTPUT);
  Serial.begin(9600);
  //Serial.println("#S|OPENCAMERA|[]#");
  Serial.println("#S|OPNMATLAB|[]#");
  delay(10000); //delay for 10s to wait for matlab to exit adjust as necissary.
}

I have rewrite my code and this is now the result.

int serInLen = 25;
int ledGreen = 8; // Groene led op pin 8
int ledRed = 4;  // Rode led op pin 4
char serInString[25];

void setup()                   
{
  pinMode(ledGreen, OUTPUT);  
  pinMode(ledRed, OUTPUT);
  Serial.begin(9600);
  Serial.println("#S|OPENCAMERA|[]#");
  delay(5000);
}

void loop()     
  
{
  char buffer[5];  // long data type has 11 characters (including the minus sign) and a terminating null
  int nrOfBlinks;
  int lineNr;
  
        
      /*
       Your buffer must be large enough to hold the maximum number of characters in the string. 
       For 16-bit base 10 (decimal) integers, that is seven characters (five digits, a possible minus sign, and a terminating 0 that always signifies the end of a string); 
       32-bit long integers need 12 character buffers (10 digits, the minus sign, and the terminating 0). 
       No warning is given if you exceed the buffer size; this is a bug that can cause all kinds of strange symptoms, 
       because the overflow will corrupt some other part of memory that may be used by your program. 
       The easiest way to handle this is to always use a 12-character buffer and always use ltoa because this will work on both 16-bit and 32-bit values.

      */
        Serial.println("#S|OPNMATLAB|[]#");
        delay(15000);
        
        for(lineNr=1;lineNr<=3;lineNr++) {
        Serial.print("#S|LEESWITHE|[");           //Leest de waarden binnen van matlab 
        Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
        Serial.println("]#");                     //Want er kunnen enkel string-waarden doorgegeven worden naar Gobetwino 
        readSerialString (serInString, 10000);    
        nrOfBlinks = atoi(serInString);    //Converteert een sting naar een integer
         
        delay(2000);
        Serial.begin(9600);
        Serial.print("#S|LOGTEST|[");
        Serial.print(itoa((nrOfBlinks), buffer, 10));
        Serial.println("]#");
      
         
         if (nrOfBlinks >= 50) 
        {
        digitalWrite(ledGreen, HIGH);
        digitalWrite(ledRed, LOW);      
        }
        
        else if (nrOfBlinks < 50)
        {
        digitalWrite(ledGreen, LOW);
        digitalWrite(ledRed, HIGH);  
        }
       }
       delay(15000);
}


void readSerialString (char *strArray,long timeOut) {
  // Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}

The results:

==> So you can see that the values of the output.txt (= the correct values) are different with the testlog.txt.
Can someone help me?
Thanks a lot

Lets first debug that you are getting the correct data from the ".txt" file first. Then we can know if it is the character to integer conversion that is fudging things up. Also make sure to null terminate your string otherwise it isn't a real C-String.

int serInLen = 25;
int ledGreen = 8; // Groene led op pin 8
int ledRed = 4;  // Rode led op pin 4
char serInString[25];

void setup()                   
{
  pinMode(ledGreen, OUTPUT);  
  pinMode(ledRed, OUTPUT);
  Serial.begin(9600);
  Serial.println("#S|OPENCAMERA|[]#");
  delay(5000);
}

void loop()     
  
{
  char buffer[5];  // long data type has 11 characters (including the minus sign) and a terminating null
  int nrOfBlinks;
  int lineNr;
  
        
      /*
       Your buffer must be large enough to hold the maximum number of characters in the string. 
       For 16-bit base 10 (decimal) integers, that is seven characters (five digits, a possible minus sign, and a terminating 0 that always signifies the end of a string); 
       32-bit long integers need 12 character buffers (10 digits, the minus sign, and the terminating 0). 
       No warning is given if you exceed the buffer size; this is a bug that can cause all kinds of strange symptoms, 
       because the overflow will corrupt some other part of memory that may be used by your program. 
       The easiest way to handle this is to always use a 12-character buffer and always use ltoa because this will work on both 16-bit and 32-bit values.

      */
        Serial.println("#S|OPNMATLAB|[]#");
        delay(15000);
        
        for(lineNr=1;lineNr<=3;lineNr++) {
        Serial.print("#S|LEESWITHE|[");           //Leest de waarden binnen van matlab 
        Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
        Serial.println("]#");                     //Want er kunnen enkel string-waarden doorgegeven worden naar Gobetwino 
        readSerialString (serInString, 10000);    
        nrOfBlinks = atoi(serInString);    //Converteert een sting naar een integer
         
        delay(2000);
        Serial.begin(9600);
        Serial.print("#S|LOGTEST|[");
        //Serial.print(itoa((nrOfBlinks), buffer, 10));
        Serial.print(serInString);
        Serial.println("]#");
      
         
         //if (nrOfBlinks >= 50) 
        //{
        //digitalWrite(ledGreen, HIGH);
        //digitalWrite(ledRed, LOW);      
        //}
        
        //else if (nrOfBlinks < 50)
        //{
        //digitalWrite(ledGreen, LOW);
        //digitalWrite(ledRed, HIGH);  
        /}
       }
       delay(15000);
}


void readSerialString (char *strArray,long timeOut) {
  // Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
   strArray[i]='\0';
}

I have tried your solution.
I just have rewrite one line of your code => strArray='\0'; placed before } .

int serInLen = 25;
int ledGreen = 8; // Groene led op pin 8
int ledRed = 4;  // Rode led op pin 4
char serInString[25];

void setup()                   
{
  pinMode(ledGreen, OUTPUT);  
  pinMode(ledRed, OUTPUT);
  Serial.begin(9600);
  Serial.println("#S|OPENCAMERA|[]#");
  delay(5000);
}

void loop()     
  
{
  char buffer[5];  // long data type has 11 characters (including the minus sign) and a terminating null
  int nrOfBlinks;
  int lineNr;
  
        
      /*
       Your buffer must be large enough to hold the maximum number of characters in the string. 
       For 16-bit base 10 (decimal) integers, that is seven characters (five digits, a possible minus sign, and a terminating 0 that always signifies the end of a string); 
       32-bit long integers need 12 character buffers (10 digits, the minus sign, and the terminating 0). 
       No warning is given if you exceed the buffer size; this is a bug that can cause all kinds of strange symptoms, 
       because the overflow will corrupt some other part of memory that may be used by your program. 
       The easiest way to handle this is to always use a 12-character buffer and always use ltoa because this will work on both 16-bit and 32-bit values.

      */
        Serial.println("#S|OPNMATLAB|[]#");
        delay(15000);
        
        for(lineNr=1;lineNr<=3;lineNr++) {
        Serial.print("#S|LEESWITHE|[");           //Leest de waarden binnen van matlab 
        Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
        Serial.println("]#");                     //Want er kunnen enkel string-waarden doorgegeven worden naar Gobetwino 
        readSerialString (serInString, 10000);    
        nrOfBlinks = atoi(serInString);    //Converteert een sting naar een integer
         
        delay(2000);
        Serial.begin(9600);
        Serial.print("#S|LOGTEST|[");
        //Serial.print(itoa((nrOfBlinks), buffer, 10));
        Serial.print(serInString);
        Serial.println("]#");
      
         
         //if (nrOfBlinks >= 50) 
        //{
        //digitalWrite(ledGreen, HIGH);
        //digitalWrite(ledRed, LOW);      
        //}
        
        //else if (nrOfBlinks < 50)
        //{
        //digitalWrite(ledGreen, LOW);
        //digitalWrite(ledRed, HIGH);  
        //}
       }
       delay(15000);
}


void readSerialString (char *strArray,long timeOut) {
  // Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
      strArray='\0';
   }
   
}

The results are good.
The next step is to write the values who you can see in gobetwino to the logtest.txt file.
Can you help with that?
Thanks al lot.

Yeah sorry, apparently I missed to code tags, rookie move. You wan't the character array value right after the last character to be zero. It should be

while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
   strArray[i]='\0';

oh an make sure to initialize your index variable i to 0

int i = 0;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < (serInLen-1) && (millis()-startTime) < timeOut) {  //Also check for timeout here, also leave space for '\0' at the end of your C-String
      strArray[i] = Serial.read();
      i++;
      delay(10); //slight delay to allow the buffer to fill up
   }
   strArray[i] = '/0'

Thanks al lot !!
There is just small problem, you can see it on the image.
How can I fix it?
Kind regards.

int serInLen = 25;
int ledGreen = 8; // Groene led op pin 8
int ledRed = 4;  // Rode led op pin 4
char serInString[25];

void setup()                   
{
  pinMode(ledGreen, OUTPUT);  
  pinMode(ledRed, OUTPUT);
  Serial.begin(9600);
  Serial.println("#S|OPENCAMERA|[]#");
  delay(5000);
}

void loop()     
  
{
  char buffer[5];  // long data type has 11 characters (including the minus sign) and a terminating null
  int nrOfBlinks;
  int lineNr;
  
        
      /*
       Your buffer must be large enough to hold the maximum number of characters in the string. 
       For 16-bit base 10 (decimal) integers, that is seven characters (five digits, a possible minus sign, and a terminating 0 that always signifies the end of a string); 
       32-bit long integers need 12 character buffers (10 digits, the minus sign, and the terminating 0). 
       No warning is given if you exceed the buffer size; this is a bug that can cause all kinds of strange symptoms, 
       because the overflow will corrupt some other part of memory that may be used by your program. 
       The easiest way to handle this is to always use a 12-character buffer and always use ltoa because this will work on both 16-bit and 32-bit values.

      */
        Serial.println("#S|OPNMATLAB|[]#");
        delay(15000);
        
        for(lineNr=1;lineNr<=3;lineNr++) {
        Serial.print("#S|LEESWITHE|[");           //Leest de waarden binnen van matlab 
        Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
        Serial.println("]#");                     //Want er kunnen enkel string-waarden doorgegeven worden naar Gobetwino 
        readSerialString (serInString, 10000);    
        nrOfBlinks = atoi(serInString);    //Converteert een sting naar een integer
         
        delay(2000);
        Serial.begin(9600);
        Serial.print("#S|LOGTEST|[");
        //Serial.print(itoa((nrOfBlinks), buffer, 10));
        Serial.print(serInString);
        Serial.println("]#");
      
         
         //if (nrOfBlinks >= 50) 
        //{
        //digitalWrite(ledGreen, HIGH);
        //digitalWrite(ledRed, LOW);      
        //}
        
        //else if (nrOfBlinks < 50)
        //{
        //digitalWrite(ledGreen, LOW);
        //digitalWrite(ledRed, HIGH);  
        //}
       }
       delay(15000);
}


void readSerialString (char *strArray,long timeOut) {
  // Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
   long startTime=millis();
   int i = 0;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
    while (Serial.available() && i < (serInLen-1) && (millis()-startTime) < timeOut) {  //Also check for timeout here, also leave space for '\0' at the end of your C-String
      strArray[i] = Serial.read();
      i++;
      delay(10); //slight delay to allow the buffer to fill up
   }
   strArray[i]='\0';
   
}

Joris_DP:
There is just small problem, you can see it on the image.
How can I fix it?

It looks as if your testlog file has an extra leading zero for one entry. Where does the content of that file come from?

The content of that file is coming from matlab.
But there is no extra zero.
In the picture below, you can see the problem in more detail.
Thanks for your help.

I can only deal with one example at a time otherwise it just gets confusing.

You are showing three windows:
Gobetwino
testlog
output

Where does this data originate, and what path does it take through your system? I thought testlog was the file that Gobetwino was reading from, which you say is written by Matlab, and includes the extra 0 that you pointed to as an error. In that case, the error lies with whatever is writing to that file.

This is his order of operations
-arduino executes Matlab through gobetwino
-Matlab writes output.txt
-Arduino reads output.txt through gobetwino
-Arduino ouputs logtest.txt through gobetwino

Like laadams85 said is correct.
This is the whole process.
Thanks