Reading from USB

Discuss issues and ideas you have to configuring displays with PowerVision
cconner_aie
Posts: 93
Joined: Thu Jun 11, 2015 10:12 am

Reading from USB

Post by cconner_aie » Mon Sep 18, 2017 10:37 am

I apologize if this has been discussed before. I looked through the documentation and examples and couldn't find a good example.

I'd like to be able to fill these 'OEM_X' strings from a file on a thumbdrive:
Image

I believe this should work, but the strings aren't updating and 'couldNotMount' never gets called. I did a couple tests and I can confirm that the USB mounts correctly and the file is opened correctly. I tried removing the '!f.isEndOfFile()' thinking there was an issue with that and I get the same result.

Code: Select all

void $readUSB$ () 
{
	mountUSB(EventIDs.getOEM, EventIDs.couldNotMount);
}

void $getOEM$ () 
{
    file f;
	
	// OEM.txt resides on the root of the USB drive
	if (f.open("OEM.txt", "r") >= 0)
	{
		string line = "";
		
		// read up to 8 lines until EOF is found
		for (int i = 0; i < 8 && !f.isEndOfFile(); i++)
		{
			line = f.readLine();
	
			// update oem line
			switch(i)
			{
				case 0: cultureSetString(StringIDs.OEM_1, line, true); break;
				case 1: cultureSetString(StringIDs.OEM_2, line, true); break;
				case 2: cultureSetString(StringIDs.OEM_3, line, true); break;
				case 3: cultureSetString(StringIDs.OEM_4, line, true); break;
				case 4: cultureSetString(StringIDs.OEM_5, line, true); break;
				case 5: cultureSetString(StringIDs.OEM_6, line, true); break;
				case 6: cultureSetString(StringIDs.OEM_7, line, true); break;
				case 7: cultureSetString(StringIDs.OEM_8, line, true); break;
			}
		}
		
		f.close();
	}
	else
	{
		// flash screen if file can't be found (I'll do something more clever later)
    		SendAction(ApplicationIDs.System, ActionIDs.System_Backlight_Off, ActionData.Empty);
   		SendAction(ApplicationIDs.System, ActionIDs.System_Backlight_On, ActionData.Empty);
	}
    
	unmountUSB(EventIDs.Empty);
}
Image

The contents of OEM.txt are:
test1
test2
test3
test4
test5
Coleby Conner
Controls Engineer, Anderson Industrial Engines
boyce
Enovation Controls Development
Enovation Controls Development
Posts: 322
Joined: Wed Sep 08, 2010 5:09 pm

Re: Reading from USB

Post by boyce » Fri Sep 22, 2017 10:18 am

There are a couple of things that are causing the problems. The file needs to be copied from the USB file into the display's flash so that it can be read with the file system functions. The other thing is, there is a bug in PV2.9 Patch 1 where the directory path is wrong for scripting. So, the full file path is needed on the file opens to work around the bug. The file path for scripting will be fixed in PV2.9 Patch 2.

Code: Select all

//---------------------------------------------------------------------------------------------
// Murphy Scripting
// - Leave EventName as $readUSB$ for main script method
//---------------------------------------------------------------------------------------------
void $readUSB$ () 
{
   mountUSB(EventIDs.getOEM, EventIDs.couldNotMount);
}

void $getOEM$ () 
{
	copyFromUSB("OEM.txt", EventIDs.fileReady);
}

void $fileReady$ ()
{
	file f;
	
   if (f.open("/fs/etfs/script/OEM.txt", "r") >= 0)
   {
      string line = "";
      
      // read up to 8 lines until EOF is found
      for (int i = 0; i < 8 && !f.isEndOfFile(); i++)
      {
         line = f.readLine();
		 
		 cultureSetString(StringIDs.OEM_8, "line " + line, true);
   
         // update oem line
         switch(i)
         {
            case 0: cultureSetString(StringIDs.OEM_1, line, true); break;
            case 1: cultureSetString(StringIDs.OEM_2, line, true); break;
            case 2: cultureSetString(StringIDs.OEM_3, line, true); break;
            case 3: cultureSetString(StringIDs.OEM_4, line, true); break;
            case 4: cultureSetString(StringIDs.OEM_5, line, true); break;
            case 5: cultureSetString(StringIDs.OEM_6, line, true); break;
            case 6: cultureSetString(StringIDs.OEM_7, line, true); break;
            case 7: cultureSetString(StringIDs.OEM_8, line, true); break;
         }
      }
      
      f.close();
   }
   else
   {
      // flash screen if file can't be found (I'll do something more clever later)
          SendAction(ApplicationIDs.System, ActionIDs.System_Backlight_Off, ActionData.Empty);
         SendAction(ApplicationIDs.System, ActionIDs.System_Backlight_On, ActionData.Empty);
   }
    
   unmountUSB(EventIDs.Empty);
}
To copy the USB file to flash:

Code: Select all

void $getOEM$ () 
{
   copyFromUSB("OEM.txt", EventIDs.fileReady);
}
The full path to the file is needed on the file open:

Code: Select all

   if (f.open("/fs/etfs/script/OEM.txt", "r") >= 0)
   {
Boyce Schrack
Enovation Controls
cconner_aie
Posts: 93
Joined: Thu Jun 11, 2015 10:12 am

Re: Reading from USB

Post by cconner_aie » Fri Sep 22, 2017 10:24 am

Thanks,

Just to clarify, will the root folder always be in "/fs/etfs/script/"? Would there be any harm in leaving that in the script, or would it be better to use the relative path for root once patch 2 comes out in case the file system changes at some point?
Coleby Conner
Controls Engineer, Anderson Industrial Engines
boyce
Enovation Controls Development
Enovation Controls Development
Posts: 322
Joined: Wed Sep 08, 2010 5:09 pm

Re: Reading from USB

Post by boyce » Fri Sep 22, 2017 10:54 am

The problem is the copyFromUSB is putting the file in the correct place, /fs/etfs/script. The default path for the file system functions in Patch 1 is /fs/etfs/config/config0 which is not the place it should be. All previous versions had /fs/etfs/script as the default path. There would not be a problem in leaving /fs/etfs/script after Patch 2.
Boyce Schrack
Enovation Controls