Pages: [1]   Go Down
Author Topic: Gobetwino reading problem  (Read 645 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have a problem with gobetwino, specially the command RFLIN.
I think the solution is not difficult, but I don't know how to program or change it.

First I will give you more information about the project.
I write data via matlab into output.txt.
Then I read the data of output.txt via gobetwino and dependent of the value, leds will blink.
Matlab runs continuous and write contiunuous data to output.txt.
Why can't gobetwino read data when output.txt is open?
Do someone know how to fix this?
Thanks a lot.


I got the error below from gobetwino.
Reading line 1 from C:\Users\Joris\Documents\MATLAB\output.txt generated an exception.
Can not access the file C: \ Users \ Documents \ MATLAB \ output.txt, because it is being used by another process.

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

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

void loop()     
 
{
  char buffer[5];  // long data type has 11 characters (including the minus sign) and a terminating null
  int nrOfBlinks;
  int lineNr;
  int aantalAardbeien;
  int teller;
  int uitlezenXpixels;
      /*
       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.

      */
     
        lineNr=1;
        clearSerialBuffer();
        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("]#");
        readSerialString (serInString, 10000);
        aantalAardbeien = atoi(serInString);
        Serial.print("#S|LOGTEST|[");
        //Serial.print(itoa((nrOfBlinks), buffer, 10));
        Serial.print(serInString);
        Serial.println("]#");
        //delay(200);
       
       
       
       //antalAardbeien = 3;
        for (teller=1; teller<=aantalAardbeien; teller++)
        {
          digitalWrite(ledYellow, HIGH);
          delay(300);
          digitalWrite(ledYellow, LOW);
          delay(300);
          //Serial.println(teller);
        }

        uitlezenXpixels = aantalAardbeien * 3 + 1;
 
        for(lineNr=2;lineNr<=uitlezenXpixels;lineNr=lineNr+3)
        {
        clearSerialBuffer();
        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(200);
        Serial.print("#S|LOGTEST|[");
        //Serial.print(itoa((nrOfBlinks), buffer, 10));
        Serial.print(serInString);
        Serial.println("]#");
        delay(200);
         
        if (nrOfBlinks > 3 && nrOfBlinks <= 160)
        {
        digitalWrite(ledGreen, HIGH);
        digitalWrite(ledRed, LOW);
        delay(1000);
        digitalWrite(ledGreen, LOW);
        }
       
        else if (nrOfBlinks > 160)
        {
        digitalWrite(ledGreen, LOW);
        digitalWrite(ledRed, HIGH);
        delay(1000);
        digitalWrite(ledRed, LOW);
        }
       }
       delay(100);
}

void clearSerialBuffer(){
    char dummy;
    while (Serial.available())  {  //Also check for timeout here, also leave space for '\0' at the end of your C-String
      dummy = Serial.read();
      delay(10); //slight delay to allow the buffer to fill up
   }
}


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';
}
Logged

Offline Offline
Sr. Member
****
Karma: 19
Posts: 486
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps you could make output.txt sharable?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It can be a solution, but how can I make a output.txt file sharable?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not familliar with MATLAB, but is it possible to make MATLAB close the file after each update? That would solve the problem. (If you're processing the file using Gobetwino then I guess the file does is not being appended to at a high rate.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, it is possible in MATLAB to close the file after each update.
But the updates goes very fast, and in arduino (via gobetwino), I open matlab in de void setup(), it runs constantly.
Logged

Offline Offline
Sr. Member
****
Karma: 19
Posts: 486
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's the program that opens the file that determines whether it is sharable.  I don't know how to do that in MATLAB.  In a Win32 program it is just a parameter you pass when you open the file.  The problem with sharing files is that you might have to deal with partial writes or buffering issues.

Having MATLAB close the file between updates might be a better solution anyway.  You will still get the error sometimes, when Gobetwino happens to be trying to open the file while MATLAB is writing to it.  If you can just ignore the error and try again, that might work.

You might have another problem, though.  What would happen if MATLAB tried to open the file while Gobetwino was reading it?  You might have to add code to the MATLAB program to try again.
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 5
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know anything about Mathlab, but can't it do serial communication ? , so why not let Mathlab send data directly to Arduino ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I send the data from matlab directly to arduino, they give a lot problems with the total program.

Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I send the data from matlab directly to arduino, they give a lot problems with the total program.

If you can solve those problems I think you'd get a better solution.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For a little more background
http://arduino.cc/forum/index.php/topic,157724.msg1181262.html#msg1181262
http://arduino.cc/forum/index.php/topic,153032.msg1147826.html#msg1147826

You're gonna have issues not ending up with synchronization issues going this route.  Either Matlab will have the file open while arduino tries to read it or vice versa.  What you can do is create a dummy file that is empty to signal if Matlab has the file open, and another for when the arduino is reading the file.  You can get really fancy had have just one file with the first line being and index of the program that is currently reading output.txt, 1 for Matlab 2 for arduino.  Then the process is this:
Matlab:
Code:
if(first line of flag file is a 2){
       wait;
    }else{
       put a 1 in the first line of flag file
       open output.txt
       put data into output.txt
       close output.txt
       put a 0 in the first line of flag file //or delete flag file
    }
arduino:
Code:
if(first line of flag file is a 1){
       wait;
    }else{
       put a 2 in the first line of flag file
       open output.txt
       read data from output.txt
       close output.txt
       put a 0 in the first line of flag file //or delete flag file
    }

This is really cludgy but you have been really averse to using the other methods, so it may be the easiest thing to do.
« Last Edit: April 15, 2013, 03:33:33 pm by laadams85 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot smiley
I don't understand everything of your solution with the dummy file.
But I will read and read it again and search on the internet for hoping I find exactly what you mean,
because tomorrow I have to show a good working program.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How can I create a .flag file ?
And how can I perform this in arduino ?
« Last Edit: April 23, 2013, 04:07:54 pm by Joris_DP » Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The .flag extension is just a personal choice, you could make the extension whatever you want, .out .txt .lck whatever.  It's similar to the lock files that keep two people from opening Microsoft office files.  To read it you would need to make another gobetwino function.
Logged

Pages: [1]   Go Up
Jump to: