Shop OBEX P1 Docs P2 Docs Learn Events
Prop and the Parallax DataLogger - Issue writing to a text file — Parallax Forums

Prop and the Parallax DataLogger - Issue writing to a text file

Michael @ AfineolMichael @ Afineol Posts: 33
edited 2011-08-02 20:27 in Propeller 1
What I am trying to do is read from a file (Input.txt). Store the individual Bytes into a free range of RAM. Read from the same region of RAM into a new file text file (Output2.txt). then light up LED's on a board in the order of 1 2 3 ...

This is proof of concept for me before I can write my real program.

My program works, but Section 3 when I write to Output2.txt I get garbage in the file. What am I doing wrong?

Input.txt contains: 123456123456CRLF
Output2.txt contains:
¬#¤ð
¬#¤ð
'Section 1
  logger.flush
  logger.changeDirectory(string("\"))
  IF logger.openFileForRead(string("Input.txt"),0)
    logger.seek(USBOffset)                                                         
    logger.readFromFileUntilChar(string("Input.txt"),@arrUSBBuffer,CR,@USBOffset)
    logger.closeFile(string("Input.txt"))
    lcdout.cls 
    lcdout.str(lcdnum.dec(USBOffset))
    WAITCNT((CLKFREQ + 2) + CNT)
  
 'Section 2 
    Repeat I from 0 to (USBOffset - 1) 
      lcdout.cls
      WAITCNT(CLKFREQ / 4 + CNT)
      arrString[0] := arrUSBBuffer[I]
      IF arrString[0] == CR
         arrString[0] := 65                              ' A
      lcdout.str(@arrString)
      Byte[$39D0][I] := arrString[0]     'Write data to region of free memory
      WAITCNT(CLKFREQ / 4 + CNT)
  ELSE
    lcdout.cls
    lcdout.str(string("Error...", CR))

'Section 3
  IF logger.openFileForWrite(string("Output2.txt"),0)
    'logger.writeToFile(string("ABCDE"),5,0)                           'Works but not used
    Repeat I from 0 to (USBOffset - 2) 
        ID := Byte[$39D0][I]
        'ID := ID + 48 
        logger.writeToFile(ID,1,0)
    logger.closeFile(string("Output2.txt"))
  ELSE
    lcdout.cls
    lcdout.str(string("Error...", CR))

'Section 4 
  dira[LEDs_START..LEDs_END]~~               ' Set entire pin group to output
  Repeat I from 0 to (USBOffset - 2)
     ID := Byte[$39D0][I]
     ID := ID - 32      'LED's start at D17 - D25 to compensate  -48 (ASCII)  + 16 (Target)  = D17
     outa[ID] ~~
     waitcnt(clkfreq + cnt)             ' Wait 1 second -> 1 Hz
     outa[ID] ~

Comments

  • RonPRonP Posts: 384
    edited 2011-07-31 13:31
    attachment.php?attachmentid=78421&d=1297987572
  • Michael @ AfineolMichael @ Afineol Posts: 33
    edited 2011-08-01 10:36
    I updated the code section of my post. This should enable everyone to now read my code as it displays in SPIN.
  • kuronekokuroneko Posts: 3,623
    edited 2011-08-01 19:27
    'Section 3
      IF logger.openFileForWrite(string("Output2.txt"),0)
        '[COLOR="orange"]logger.writeToFile(string("ABCDE"),5,0)[/COLOR]           'Works but not used
        Repeat I from 0 to (USBOffset - 2) 
            ID := Byte[$39D0][I]
            'ID := ID + 48 
            [COLOR="blue"]logger.writeToFile(ID,1,0)[/COLOR]
        logger.closeFile(string("Output2.txt"))
      ELSE
        lcdout.cls
        lcdout.str(string("Error...", CR))
    
    When you look at the working writeToFile call you'll notice that the first parameter is a string address and 5 characters are written starting from that address (not sure about the last parameter). In your second invocation you use ID which is a byte value and not the address you want (if treated as one). Going by the same rules (1 char written starting from the address passed as the first parameter) you should use $39D0 + l as the address or better yet, define some VAR/DAT array as a buffer and use its address. Writing to arbitrary memory is almost always a bad idea.
  • Michael @ AfineolMichael @ Afineol Posts: 33
    edited 2011-08-02 20:27
    Thank you. This worked!!!
    logger.writeToFile(@ID,1,0) 
    
Sign In or Register to comment.