Shop OBEX P1 Docs P2 Docs Learn Events
SD card .txt file need help with line feed — Parallax Forums

SD card .txt file need help with line feed

Tony_tsiTony_tsi Posts: 98
edited 2013-06-01 21:16 in Propeller 1
I building a data logger as a school project. I want to log into a txt file in the following manner

Time,data,
Time,data,
Time,data,

But instead end up with this

Time,data,time,data,time,data

I have everything working great except for the line feed or carriage return(not sure what to call it)

So my question is what do u do to make the txt file line feed?

Things I have tried

13
"13"
"^13"
"\n
"\n"
"\|"
"EOL"
"LF"
"NL"
"\r"
"\rl"

I am using the micro sd file from the object exchange

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-05-25 19:10
    You didn't say how you tried it but I'd use something like sdfat.pputs(string(13, 10)) (CR/LF, win32) or sdfat.pputc(10) (LF, unix & Co).
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-05-25 19:20
    sdfat.pputs(string(13, 10)) <
    this fixed all of my problems but I still have a curiosity question......

    Why will it not line feed without a carriage return?
    If I type sdfat.pputs(string(13)) it should return to the next line but not go to the left side of the page. But it does not do this.
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-25 19:23
    Tony_tsi wrote: »
    Why will it not line feed without a carriage return? If I type sdfat.pputs(string(13)) it should return to the next line but not go to the left side of the page. But it does not do this.
    Isn't that behaviour up to the editor you're using? IIRC a single CR (13) line ending is MAC format. If your editor/viewer doesn't get this then you're out of luck ...
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-05-25 19:28
    Actually I do have one more code question.
    Lets say i have 4 long variables called x, month, day, year
    Month:=5
    Day:=25
    Year:=2013

    What I want to do is somthing like this

    X:=String(month,"_"'day,"_",year,".txt")

    So that when I make a file the file name would be

    5_25_2013.txt
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-25 19:31
    To clarify ... There are 3 different conventions for a new text line depending on which operating system you're using. Some text editors are multi-lingual and will accept any of the conventions and many editors are not.

    For Windows, you use 13 (CR), then 10 (LF). The 13 (CR) will do a carriage return, returning the cursor to the left margin while the 10 (LF) will get you a vertical line space.
    For the MacOS, you use 13 (CR) to do both a carriage return and a line feed. An additional 10 (LF) will get you another vertical line space without returning the cursor to the left margin.
    For Linux, you use 10 (LF) to do both a carriage return and a line feed (a 'new line').
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-05-25 19:31
    kuroneko wrote: »
    Isn't that behaviour up to the editor you're using? IIRC a single CR (13) line ending is MAC format. If your editor/viewer doesn't get this then you're out of luck ...
    You are correct I have been using notepad to view the txt because it has to be able to open in note pad but if I use notepad ++ it does recognize it!
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-05-25 19:34
    Mike Green wrote: »
    To clarify ... There are 3 different conventions for a new text line depending on which operating system you're using. Some text editors are multi-lingual and will accept any of the conventions and many editors are not.

    For Windows, you use 13 (CR), then 10 (LF). The 13 (CR) will do a carriage return, returning the cursor to the left margin while the 10 (LF) will get you a vertical line space.
    For the MacOS, you use 13 (CR) to do both a carriage return and a line feed. An additional 10 (LF) will get you another vertical line space without returning the cursor to the left margin.
    For Linux, you use 10 (LF) to do both a carriage return and a line feed (a 'new line').

    Thank you this clears things up a lot.
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-25 19:36
    Tony_tsi wrote: »
    Actually I do have one more code question ...
    First, filenames are limited to 8.3 with FSRW. So you probably have to settle for 05252013.txt. As for string building, that needs some effort, i.e. you allocate a character/byte array and fill it with data by using something like the Numbers object.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-25 19:38
    To convert a number into a corresponding sequence of digits, you'd use the Numbers object from the Object Exchange to make the strings you need. If you declare

    OBJ num : "Numbers"

    Then you'd do something like

    sdfat.pputs(num.dec(month))
    sdfat.pputc("_")
    sdfat.pputs(num.dec(day))
    sdfat.pputc("_")
    sdfat.pputs(num.dec(year))
    sdfat.pputs(string(".txt"))

    This would write the file name out to the file. If what you want to do is construct a file name from the date, I'd do it differently.

    VAR byte filename[11]

    filename[0] := month/10+"0" ' two digit month value
    filename[1] := month//10+"0"
    filename[2] := day/10+"0" ' two digit day value
    filename[3] := day//10+"0"
    filename[4] := (year//100)/10+"0" ' rightmost two digits of year value
    filename[5] := (year//100)//10+"0"
    bytemove(@filename+6,string(".txt"),5) ' moves ".txt" plus a final zero byte to the end
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-05-25 19:43
    Awesome thank you so much will I need to put string before"_"
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-25 19:55
    .pputc is different from .pputs

    The former accepts a character like "_" while the latter requires the address of a zero-byte terminated string which string("_") supplies.

    If you want the underlines in my 2nd example, use

    VAR byte filename[13]

    bytemove(@filename,string("00_00_00.txt"),13)
    filename[0] += month/10 ' two digit month value
    filename[1] += month//10
    filename[3] += day/10 ' two digit day value
    filename[4] += day//10
    filename[6] += (year//100)/10 ' rightmost two digits of year value
    filename[7] += (year//100)//10
  • RforbesRforbes Posts: 281
    edited 2013-05-25 20:26
    As an alternative to using another object (Numbers) you can use something like the following. It might help to layout your DAT section like this and study it a bit. Sometimes seeing things in different ways can really make the light bulb come on (or burn out!) :)
    DAT
                                       
      filename              byte    "0","0","0","0","0","0",".txt",0
      ascii_date            byte    "0","0","-","0","0","-","0","0",0                                   
      ascii_time            byte    "0","0",":","0","0",":","0","0",0  
    
    pub date_convert
    {{
      Here, we build the portion of our string that contains the date and time data.
      This portion of our string always has the format of: yy-mm-dd and hh:mm:ss
      Our file name always has the format yymmdd.txt
    
      
    }}
    
    
     Assuming year, month, day, hour, minute, second are all 2 digit decimal values.
     
    
    byte[@filename][0]:=byte[@ascii_date][0]:=(year/10)+48                  'This converts values from decimal to ascii characters.
    byte[@filename][1]:=byte[@ascii_date][1]:=(year-((year/10)*10))+48   
    byte[@filename][2]:=byte[@ascii_date][3]:=(month/10)+48 
    byte[@filename][3]:=byte[@ascii_date][4]:=(month-((month/10)*10))+48
    byte[@filename][4]:=byte[@ascii_date][6]:=(day/10)+48 
    byte[@filename][5]:=byte[@ascii_date][7]:=(day-((day/10)*10))+48 
    
    byte[@ascii_time][0]:=(hour/10)+48  
    byte[@ascii_time][1]:=(hour-((hour/10)*10))+48  
    byte[@ascii_time][3]:=(minute/10)+48
    byte[@ascii_time][4]:=(minute-((minute/10)*10))+48
    byte[@ascii_time][6]:=(second/10)+48
    byte[@ascii_time][7]:=(second-((second/10)*10))+48
    
        pst.str(string("Date: "))
        pst.str(@ascii_date)
        pst.newline
        pst.str(string("Time: "))
        pst.str(@ascii_time)
        pst.newline
        pst.str(string("File name: "))
        pst.str(@filename)
        pst.newline
        pst.newline
        
        return 1
    

    You'd want to subtract 2000 from your YEAR value before running the method, so that it's 2 digits instead of 4. Or modify the method to convert 4 digits instead of two.
    Good luck to ya! :)
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-25 20:37
    @Rforbes: Is there a particular reason you don't want to use filename[0]:=ascii_date[0]:=(year/10)+48 etc? I'm not saying the byte[] notation is wrong but IMO it's harder to read and less efficient in this case.
  • RforbesRforbes Posts: 281
    edited 2013-05-25 20:45
    @kuroneko- Uhhh..... only because I'm a newb! :) When I wrote that method I was playing around with stuff, learning about different ways to point to addresses and such. So, you're right- the way you wrote it would be better. I just haven't bothered to update mine and haven't thought about it in awhile. I tend to "fire and forget" utility type methods after I get them working.

    I just wanted to offer a different way to look at the problem here. Everyone always helps me out so I'm happy to attempt the same if I can. :)
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-05-25 21:22
    Here is what i have so far, I need help with line 60,66,68. thank you
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-05-25 21:33
    Mike Green wrote: »
    .pputc is different from .pputs

    The former accepts a character like "_" while the latter requires the address of a zero-byte terminated string which string("_") supplies.

    If you want the underlines in my 2nd example, use

    VAR byte filename[13]

    bytemove(@filename,string("00_00_00.txt"),13)
    filename[0] += month/10 ' two digit month value
    filename[1] += month//10
    filename[3] += day/10 ' two digit day value
    filename[4] += day//10
    filename[6] += (year//100)/10 ' rightmost two digits of year value
    filename[7] += (year//100)//10

    I am having trouble plugging this into my code
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-25 21:44
    This is not a complete piece of code. It's an example. You have to work in the VAR declaration into your other declarations and you have to work in the assignment statements into your other code. If you do so and you're still having problems, please describe them more fully. "having trouble" is not very helpful.
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-05-25 22:27
    Mike Green wrote: »
    .pputc is different from .pputs

    The former accepts a character like "_" while the latter requires the address of a zero-byte terminated string which string("_") supplies.

    If you want the underlines in my 2nd example, use

    VAR byte filename[13]

    bytemove(@filename,string("00_00_00.txt"),13)
    filename[0] += month/10 ' two digit month value
    filename[1] += month//10
    filename[3] += day/10 ' two digit day value
    filename[4] += day//10
    filename[6] += (year//100)/10 ' rightmost two digits of year value
    filename[7] += (year//100)//10

    Mike Green wrote: »
    .pputc is different from .pputs

    The former accepts a character like "_" while the latter requires the address of a zero-byte terminated string which string("_") supplies.

    If you want the underlines in my 2nd example, use

    VAR byte filename[13]

    bytemove(@filename,string("00_00_00.txt"),13)
    filename[0] += month/10 ' two digit month value
    filename[1] += month//10
    filename[3] += day/10 ' two digit day value
    filename[4] += day//10
    filename[6] += (year//100)/10 ' rightmost two digits of year value
    filename[7] += (year//100)//10

    Bytemove is a new command to me, I did read up on it in the manual. It appears to me that each byte in the filename array is written as a character from "00_00_00.txt" like so
    Filename[0]=0
    Filename[1]=0
    Filename[2]=_
    Filename[3]=0
    Filename[4]=0
    Filename[5]=_
    Filename[6]=0
    Filename[7]=0
    Filename[8]=.
    Filename[9]=t
    Filename[10]=x
    Filename[11]=t

    And then the following lines rewrite the zeros into the correct numbers
    Is this correct?
    Why 13 elements?
    If so how do I pull this together into one string?
    x:=string( )
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-26 01:38
    Tony_tsi wrote: »
    Why 13 elements?
    string("00_00_00.txt") points to a 12 character string followed by a terminating zero. The 13th character/byte has to be copied as well to keep the string intact.
    Tony_tsi wrote: »
    If so how do I pull this together into one string?
    x:=string( )
    You don't. It's already done. A string is a sequence of non-zero bytes terminated by a zero which we just prepared in the filename byte array. So all you need is x := @filename[0].
  • Tony_tsiTony_tsi Posts: 98
    edited 2013-06-01 21:16
    Thank you to everyone for your help. Here is the file, I wrote a quick demo to show how to use the file.tonys sd demo.zip
Sign In or Register to comment.