View unanswered posts | View active topics It is currently Thu Aug 13, 2020 1:48 pm






Reply to topic  [ 7 posts ] 
File Access for EV3 in 4.28 
Author Message
Rookie

Joined: Fri Sep 20, 2013 8:39 am
Posts: 38
Post File Access for EV3 in 4.28
Are there detailed instructions or examples of using the file Access operations for the EV3 in Robotc 4.28. There is an example of how to use the datalog features but not an example for the general File Access


Tue Jan 27, 2015 7:31 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: File Access for EV3 in 4.28
Here's the program I used for testing the file access opcodes:
Code:
// The name of the file that we're saving data to
char * filename = "testFile";

// The read buffer, which is the amount of data we can read at once
char readBuffer[128];

// Routine to test the writing and subsequent reading
// of long-sized variables
void testLongs()
{
  writeDebugStreamLine("Testing Longs");

  // The file handle, this is a reference to
  // the file you have opened
  long fileHandle;

  long writeValue = 0x11DD0000;
  long readValue;

  // Open the file for writing and return a handle,
  // which we can use for subsequent writes
  fileHandle = fileOpenWrite(filename);
  for (int i = 0; i < 10; i++)
  {
     // Write a long value (4 byte) to the file, this is
     // binary, not a string
    fileWriteLong(fileHandle, writeValue++);
  }
  writeDebugStreamLine("fileHandle: %d", fileHandle);
 
  // Close the file, this automatically flushes the
  // buffers to the "disk".
  fileClose(fileHandle);

  // Open the file for reading and return a handle.
  fileHandle = fileOpenRead(filename);
 
  // Read long-sized values for as long as there's data.
  // The fileReadLong() function returns "false" when
  // an error occurred or not data is left
  while (fileReadLong(fileHandle, &readValue))
  {
    writeDebugStreamLine("Read: 0x%X", readValue);
  }
 
  // Close the file
  fileClose(fileHandle);
}

// Routine to test the writing and subsequent reading
// of float variables
void testFloats()
{
  writeDebugStreamLine("Testing Floats");

  // The file handle, this is a reference to
  // the file you have opened
  long fileHandle;

  float writeValue = 8.167;
  float readValue;

  // Open the file for writing and return a handle,
  // which we can use for subsequent writes
  fileHandle = fileOpenWrite(filename);
  for (int i = 0; i < 10; i++)
  {
     // Write a float value to the file, this is
     // binary, not a string     
    fileWriteFloat(fileHandle, writeValue);
    writeValue*=5;
  }
  writeDebugStreamLine("fileHandle: %d", fileHandle);
 
  // Close the file, this automatically flushes the
  // buffers to the "disk".
  fileClose(fileHandle);

  // Open the file for reading and return a handle.
  fileHandle = fileOpenRead(filename);
 
  // Read float values for as long as there's data.
  // The fileReadFloat() function returns "false" when
  // an error occurred or not data is left 
  while (fileReadFloat(fileHandle, &readValue))
  {
    writeDebugStreamLine("Read: %f", readValue);
  }
 
  // Close the file
  fileClose(fileHandle);
}

// Routine to test the writing and subsequent reading
// of float variables
void testShorts()
{
  writeDebugStreamLine("Testing Shorts");

  // The file handle, this is a reference to
  // the file you have opened
  long fileHandle;

  short writeValue = 2;
  short readValue;

  // Open the file for writing and return a handle,
  // which we can use for subsequent writes 
  fileHandle = fileOpenWrite(filename);
  for (int i = 0; i < 10; i++)
  {
     // Write a short (2 byte) value to the file, this is
     // binary, not a string     
    fileWriteShort(fileHandle, writeValue);
    writeValue*=2;
  }
  writeDebugStreamLine("fileHandle: %d", fileHandle);
 
  // Close the file, this automatically flushes the
  // buffers to the "disk".
  fileClose(fileHandle);
 
  // Open the file for reading and return a handle.
  fileHandle = fileOpenRead(filename);

  // Read short values for as long as there's data.
  // The fileReadShort() function returns "false" when
  // an error occurred or not data is left 
  while (fileReadShort(fileHandle, &readValue))
  {
    writeDebugStreamLine("Read: %d", readValue);
  }
 
  // Close the file
  fileClose(fileHandle);
}

// Routine to test the writing and subsequent reading
// of float variables
void testChars()
{
  writeDebugStreamLine("Testing Chars");

  // The file handle, this is a reference to
  // the file you have opened
  long fileHandle;

  char writeValue = 1;
  char readValue;

  // Open the file for writing and return a handle,
  // which we can use for subsequent writes 
  fileHandle = fileOpenWrite(filename);
  for (int i = 0; i < 10; i++)
  {
     // Write a char (1 byte) value to the file, this is
     // binary, not a string
    fileWriteChar(fileHandle, writeValue);
    writeValue+=5;
  }
  writeDebugStreamLine("fileHandle: %d", fileHandle);
 
  // Close the file, this automatically flushes the
  // buffers to the "disk".
  fileClose(fileHandle);

  // Open the file for reading and return a handle.
  fileHandle = fileOpenRead(filename);

  // Read char values for as long as there's data.
  // The fileReadChar() function returns "false" when
  // an error occurred or not data is left   
  while (fileReadChar(fileHandle, &readValue))
  {
    writeDebugStreamLine("Read: %d", readValue);
  }
 
  // Close the file
  fileClose(fileHandle);
}

void testData()
{
  writeDebugStreamLine("Testing Data");

  long fileHandle;

  char * string1 = "I like peanutbutter";
  int strlen1 = strlen(string1);

  char * string2 = "ROBOTC is cool";
  int strlen2 = strlen(string2);

  // Open the file for writing and return a handle,
  // which we can use for subsequent writes 
  fileHandle = fileOpenWrite(filename);
  writeDebugStreamLine("fileHandle: %d", fileHandle);

  writeDebugStreamLine("strlen1: %d", strlen1);
  fileWriteData(fileHandle, string1, strlen1 + 1); // gets the 0 terminator as well
  fileWriteData(fileHandle, string2, strlen2 + 1); // gets the 0 terminator as well

  // Close the file, this automatically flushes the
  // buffers to the "disk".
  fileClose(fileHandle);

  // Open the file for reading and return a handle.
  fileHandle = fileOpenRead(filename);
 
  memset(readBuffer, 0, 128);
  fileReadData(fileHandle, readBuffer, strlen1 + 1);
  writeDebugStreamLine(readBuffer);
  fileReadData(fileHandle, readBuffer, strlen2 + 1);
  writeDebugStreamLine(readBuffer);

  // Close the file
  fileClose(fileHandle);
}


task main()
{
  testLongs();
  testFloats();
  testShorts();
  testChars();
  testData();
}


= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Thu Jan 29, 2015 2:01 am
Profile WWW
Rookie

Joined: Fri Sep 20, 2013 8:39 am
Posts: 38
Post Re: File Access for EV3 in 4.28
Thanks for that.


Thu Jan 29, 2015 4:23 am
Profile
Rookie

Joined: Fri Mar 27, 2015 8:22 am
Posts: 11
Post Re: File Access for EV3 in 4.28
when I use the program to test, to open the file only appear strange symbols. Am I doing something wrong?


Tue Mar 31, 2015 3:31 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: File Access for EV3 in 4.28
No, you're not doing something wrong, but the data you write with fileWriteShort(), Longs(), Floats(), etc, is binary and not text. If you want to write text to it, you'll have to use fileWriteData(); Look at the testData() function in the example above.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Mar 31, 2015 6:00 am
Profile WWW
Rookie

Joined: Fri Mar 27, 2015 8:22 am
Posts: 11
Post Re: File Access for EV3 in 4.28
Thanks for your help , you saved me from my boss .. XD


Tue Mar 31, 2015 7:16 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: File Access for EV3 in 4.28
No worries :)

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Tue Mar 31, 2015 9:53 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.