Shop OBEX P1 Docs P2 Docs Learn Events
Reading from a file — Parallax Forums

Reading from a file


I trying to read a text file to get a file name.

Just reading a simple text file with two lines in it.

Right now I would be happy just to get the first line.
FILE1000.HEX
Other options


This is the output I get.
Starting Initializing SD Card System

SD Card System Initialized

Opening File: STARTUP.TXT

File Opened

File to start with: 



con { timing }

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000                                          ' use 5MHz crystal

  CLK_FREQ = (_clkmode >> 6) * _xinfreq                         ' system freq as a constant
  MS_001   = CLK_FREQ / 1_000                                   ' ticks in 1ms
  US_001   = CLK_FREQ / 1_000_000                               ' ticks in 1us


con { io pins }

  sdDOpin   = 1     ' Data Out Pin
  sdCKpin   = 2     ' Clock Pin
  sdDIpin   = 3     ' Data in Pin
  sdCSpin   = 4     ' Chip Select Pin

  sdLEDpin  = 17    ' Status LED pin number.

  RX1  = 31         ' programming / terminal
  TX1  = 30

  SDA  = 29         ' eeprom / i2c
  SCL  = 28
  
con { settings  }

  maxled = 288

obj

  sd    : "SD-MMC_FATEngine.spin"
  time  : "jm_time_80.spin" 
  pst   : "Parallax Serial Terminal.spin"
  

var

  byte  frameFileName[13]
  
pub main | errorNumber, errorString, tLong

  pst.Start(115200)
  pst.Str(String("Starting "))
  
  pst.Str(String("Initializing SD Card System"))
  pst.Chars(pst#NL, 2)
  sd.fatEngineStart(sdDOpin, sdCKpin, sdDIpin, sdCSpin, -1, -1, -1, -1, -1)
  pst.Str(String("SD Card System Initialized"))
  pst.Chars(pst#NL, 2)
  
  readStartFile




pub readStartFile 
  openFile(string("STARTUP.TXT"))
  sd.readString(@frameFileName, 14)
  pst.Str(string("File to start with: "))
  pst.Str(@frameFileName)
  pst.Chars(pst#NL, 2)

  pst.Char(frameFileName[0])
  pst.Char(frameFileName[1])
  pst.Char(frameFileName[2])
  pst.Char(frameFileName[3])
  pst.Char(frameFileName[4])
  pst.Char(frameFileName[5])
  pst.Char(frameFileName[6])
  pst.Char(frameFileName[7])
  pst.Char(frameFileName[8])
  pst.Char(frameFileName[9])
  pst.Char(frameFileName[10])
  pst.Char(frameFileName[11])
  pst.Char(frameFileName[12])
  pst.Chars(pst#NL, 2)

'  closeFile
  
'****************************************************************
'
'
'
pub OpenFile(fileName) | errorString, errorNumber
  
  pst.Str(String("Opening File: "))
  pst.Str(fileName)
  pst.Chars(pst#NL, 2)
  errorString := \sd.openFile(fileName, "R")
  errorNumber := sd.partitionError
  if(errorNumber) 
    pst.Str(String("Error Opening File"))
    pst.Chars(pst#NL, 2)
  else
    pst.Str(String("File Opened"))
    pst.Chars(pst#NL, 2)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2017-06-10 23:22
    The only thing that's obvious is that you read in up to 14 characters for the file name, but the size of frameFileName is 13. You need room for 13 characters plus one or two for the carriage return or newline (or carriage return and line feed) plus the terminating zero byte. Declare "byte frameFileName[16]" and see what happens.
  • JonnyMacJonnyMac Posts: 9,104
    edited 2017-06-10 23:22
    I wrote a parser object so that I could use serial streams or text from a file as commands or configuration. As I'm in the middle of a new EFX-TEK product that uses a configuration file, I copied my configuration routines from it and modified based on your post.

    As it stands, this demo allows you to set a filename string (8.3 format) and up to three, byte options. The way my configuration reader works you can add comments by using the single quote character -- these can be at the start of a line, or even after the commands.

    The nice thing about using a parser like this is that you're not depending on the order of items in the file (as long as they are syntactically correct), and you can mix notes into the file using the comment character.

    This is the contents of the test file I used:
    ' Configuration file demo
    
    filename file1000.hex
    
    option 0 7			' JonnyMac's birth month
    option 1 25			' JonnyMac's birth day
    option 2 62 			' JonnyMac's birth year
    

    Note that I'm able to use white space and comments.

Sign In or Register to comment.