Shop OBEX P1 Docs P2 Docs Learn Events
Datalogger string hassles — Parallax Forums

Datalogger string hassles

CumQuaTCumQuaT Posts: 156
edited 2010-06-17 06:21 in Propeller 1
Hi all,

I'm working on a project that requires me to save number values into a text file on a flashcard (I'm using the memory stick datalogger) and after a LOT of hassles, finally got that part working fine.

Now I'm up to the bit where I need the prop to pull the values back out of the text file. I'm using the default USB Datalogger object to do the transfer and it has the following code in it to read data back from the Flashcard:

PUB Read(readLength,stringptr) | i    ' Read data from file (file must be preopened)
'This will send back the requested amount of data to the monitor.
  USB.rxflush
  USB.str(string("RDF ")) 
  USB.tx((readLength>>24) & $FF)
  USB.tx((readLength>>16) & $FF)
  USB.tx((readLength>> 8) & $FF)
  USB.tx( readLength      & $FF)
  USB.tx(CR)
  repeat i from 0 to readLength-1
    byte[noparse][[/noparse]stringptr++] := USB.rx




It seems to return a string value, but as far as I know, the prop doesn't handle strings, does it? My text document is laid out like this internally:

49
55
48
48
66
67
66
67
67
70
-
49
55
48
48
66
67
51
70
70
55
-




So yeah, 10 numbers, one on each line, then a hyphen, then another 10 numbers, etc, etc. Is anyone familiar enough with this particular bit of code that would know precisely how to use it?

Any help would be great. Thanks in advance!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Who are you, and why are you reading my signature?

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-06-16 15:19
    It does support strings (actually a series of bytes representing each character with a 0-value byte to terminate the string). It is the simplest way for compiler to handle strings, and really the only way with SPIN's complete lack of typecasting.

    You can create strings with the string() function (check out the string functions in the Manual), or just set aside a block of memory to put a string in it, just make sure it is 0-terminated.

    As for help for this specific problem, someone else will need to do so.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!

    Some of my objects:
    MCP3X0X ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
    Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
    String Manipulation Library - Don't allow strings to be the bane of the Propeller, bend them to your will!
    Fast Inter-Propeller Comm - Fast communication between two propellers (1.37MB/s @100MHz)!
  • T ChapT Chap Posts: 4,223
    edited 2010-06-16 15:27
    What you want to do with the info once you read it back in? You would first explain what your requirement is for anyone to offer specific help. What you are showing in the text file is a single byte value followed by CR per line, not sure where the '-' came from, but if you are working in chunks of 10 bytes with a '-' delimiter, you could read the first 10, do what you want to do with the info, then start over with the next set. You don't state what the potential maximum amount of numbers might be that need to get read in at once, but you have to consider the buffer size if you plan to read in a lot at once. If the file is really large (too large for the buffer in a single read), you could use SEEK to read in smaller amounts: SEEK 0 and read in 64 bytes for example, do the work, then SEEK to 64, read in 64 bytes, do the work, etc etc. You can read in a string into the Prop using an array created in VAR like stringptr[noparse][[/noparse] 64], where the read starts placing the values at stringptr[noparse][[/noparse] 0] and fills up the buffer, then it starts overwriting at the top again. You access the values in the array the same way... the following shows what the array will look like after reading the first few lines of your text file.
    stringptr[noparse][[/noparse] 0] == 49
    stringptr[noparse][[/noparse] 1] == 55
    stringptr[noparse][[/noparse] 2] == 48

    Not sure if this really answers you question but it looks like you are reading and writing the file, now trying to figure out how to work with the data after you read it.

    Post Edited (Todd Chapman) : 6/16/2010 3:42:50 PM GMT
  • CumQuaTCumQuaT Posts: 156
    edited 2010-06-17 00:22
    Thanks for the replies, but no, neither answers my question, really... I can't actually successfully read in a set of 10 numbers. I'm trying to take each of the 10 numbers in a set out of the text file and store them in a byte array in my prop program, but when I try and use the above method to bring the values in, my array still ends up holding 0, 0, 0, 0, 0, 0, 0, 0, 0, 0. I have a feelings it's a string->number conversion issue...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who are you, and why are you reading my signature?
  • T ChapT Chap Posts: 4,223
    edited 2010-06-17 01:26
    You should post your entire code involved, not just the snippet above.
  • TimmooreTimmoore Posts: 1,031
    edited 2010-06-17 01:46
    There is some things not clear, I think you are saying the file contains 2 character numbers per line, can they be longer? How long/big can the number be? Depending on the OS/program used to write the file, the end of line may be 1 character or 2 characters, it may be carriage return or newline or both.
    As Todd mentioned you need to post the rest of your code, how you call Read may have problems.
    One place to look is in obex look for a file called obex_format.spin it contains various functions for string to number translation which can be used to translate the strings once read from the file into numbers but if the array contains 0, you haven't read the string in correctly yet. I expect you first need to read the file in as a string, so the stringptr will contain 34, 39, .. i.e. 34 is the value for character "4", etc. then translate each line into a number.
  • CumQuaTCumQuaT Posts: 156
    edited 2010-06-17 02:19
    There is far too much code involved to post here. I can do it, but you'll be reading for a long time... I've posted the code I'm using to read in the data. There will always be two characters per line.

    The obex_format.spin object sounds interesting, that may have what I need. It's pretty much a string to number conversion that I need, I reckon, so I'll give that a go. Thanks for looking into it for me!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who are you, and why are you reading my signature?
  • T ChapT Chap Posts: 4,223
    edited 2010-06-17 03:31
    Are you sure the file you are reading from have the correct data written to it? You don't need to worry about conversion until you have something actually being read back. Display the returned values in hex and see what you are getting. There are plenty of hex> ascii converters online you can use if you just want to get a quick glance at what the ascii values are. Same for decimal.
  • CumQuaTCumQuaT Posts: 156
    edited 2010-06-17 04:14
    Thanks, I'll have a look into that, too. It's a fair point. I know that the data in the text file is correct, but as for my "reading in" process I still am not sure. That's why I posted the code above, as that is the method used in the USB DataLogger object I'm using, and I can't really find proper documentation on how to use it, so the original post was really asking if anyone had any idea what the correct usage of the method was, but I might just do some base-level experimentation with output from the method to see what I'm actually dealing with.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who are you, and why are you reading my signature?
  • T ChapT Chap Posts: 4,223
    edited 2010-06-17 06:21
    There are many possible things going on, without the code you are using anyone is really just guessing. Surely you can extract the few methods you are using to call the USB read and post it. There are also things such as your serial init including baud, pins, etc. How are you displaying what you are reading?


    Try this and see what happens. Put in a fake name to read that is not on the drive, you should get back a response that says something similar to file not found or other error. If you are displaying the real response, you will get some hex the you can post. Use ECS and HEX mode.

    PUB OpenFile(filename)
        ser.str(string("OPR"))
        ser.tx($20)
        ser.str(filename)
        ser.tx($0D)
    
    PUB  DIR
         ser.str(string("DIR"))
         ser.tx(CR)
    
    
    



    Either of these will produce a response. If you cannot get a response from CD or DIR then you have other problems to deal with before you can start reading files.

    Post Edited (Todd Chapman) : 6/17/2010 6:34:36 AM GMT
Sign In or Register to comment.