Shop OBEX P1 Docs P2 Docs Learn Events
Array Help — Parallax Forums

Array Help

eagletalontimeagletalontim Posts: 1,399
edited 2012-09-29 22:41 in Propeller 1
This is following my power meter logger project..... I am attempting to store the date and time and a generated number in an array then when it is called for by my computer program, it will dump all the information to the computer for processing. Once all the information is sent to the computer, the array is cleared and the "pointer" is reset back to 0. I am trying to find the most efficient way to do this but am having a huge brain fart at the current moment :p

The process so far is....
When the Prop boots up, it will default to January 1, 2012 at 12:00AM (0101121200).
When the computer program starts up, it sends a serial signal to the PROP to update it's date and time to the computer's system time.
Every 2 seconds, the computer program requests other information from the PROP to simply display on the screen for user information purposes.
Every hour, the PROP dumps the Watt Hours Used into an Array which should be stored something like ("0929121628:1234", "next one", "another one")... Not sure if this is the best way.
At every top of the hour, the computer program sends a signal to the PROP to collect the Array data stored at the current time. Each array element is broken down and stored on my off site server's database.
If the information was successfully update on the offsite server, the Array is reset and the system time is sent back to the PROP to "sync" the time again.
The whole cycle is restarted over again.

My question is about the long array value that needs to be stored.... Since an example value would be "0929121628:1234", would the array variable be a LONG or WORD or something else?

Or....

Is there a better way to do the process above?

Comments

  • Mike GMike G Posts: 2,702
    edited 2012-09-29 15:06
    eagletalontim, "0929121628:1234" is a string or byte array not a long or a word. I'd save the byte array in a buffer, then use a pointer array to keep track of the starting position of each string in the buffer. Make sure you zero terminate the string.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-09-29 15:11
    I am not quite sure how to save anything to a buffer. Since I MUST send the data over to the computer in the following format : "0929121628:1234", how would I save it in a buffer instead of an array and loop through the buffer to send each item?
  • Mike GMike G Posts: 2,702
    edited 2012-09-29 15:39
    Abstract data structure
    http://www.parallaxsemiconductor.com/an003
    CON
      _clkmode   = xtal1 + pll16x                           
      _xinfreq   = 5_000_000
    
      ONE_K_BUFFER  = 1024
      MAX_ARRAY_LEN = 16
      NULL          = 0
      CR            = 13
    
    VAR
    
    DAT
      buff    byte  $0[ONE_K_BUFFER]
      count   byte  $0
      times   long  $0[MAX_ARRAY_LEN]
      ptr     long  $0
     
    
    OBJ
      pst           : "Parallax Serial Terminal"
    
       
    PUB Main | i
    
      ptr := @buff
      i := 0
      
      pst.Start(115_200)
      pause(500)
    
      AddData(string("0929121628:1234"))
      AddData(string("1111111:2222"))
      AddData(string("3333333333:4444444"))
    
      repeat count
        pst.str(times[i++])
        pst.char(CR)
    
      Clear
      pst.char(CR)
    
      AddData(string("11111111111:1111"))
      AddData(string("22222222222:2222"))
      AddData(string("33333333333:3333"))
    
      i := 0
      repeat count
        pst.str(times[i++])
        pst.char(CR)
    
    
    PUB AddData(value)
      'Save the starting address of the byte array
      times[count++] := ptr
    
      ' Move the byte array to HUB memory
      bytemove(ptr, value, strsize(value))
      ptr += strsize(value) 
    
      'Zero terminate
      byte[ptr++] :=  NULL
    
    
    PUB GetData(idx)
      return times[idx]
    
    PUB Clear
      count := 0
      ptr := buff
    
    PRI pause(Duration)  
      waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
      return
      
    
  • Mike GMike G Posts: 2,702
    edited 2012-09-29 16:10
    FYI, I edited the code in AddData. I had ptr += strsize(value) + ptr becuase I changed := to += but did not remove the + ptr. It should have been ptr += strsize(value)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-09-29 16:44
    This might be a good time to use the EEPROM on the Prop board. Most Propeller boards come with 64K EEPROMs. By storing to EEPROM, you wouldn't lose your data with a power loss. You'd also have 32K more storage room than you'd have using only RAM.

    There's an EEPROM datalogging section in the PEK sticky.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-09-29 17:53
    I did think about using the EEPROM, but I am still learning a few other things about the prop with this project and don't want to "cloud" my brain with too much to absorb :P I have tinkered with writing and reading from the EEPROM with success, but don't understand the "addresses" and storing bytes, longs, or strings then retrieving them based on what address they were stored at. I simply can't understand Bytes vs Longs vs Words :( I know enough to make my programs work....
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-09-29 18:01
    One other thing.... If I store a Byte variable as "00" will that still come out as "00" or will it come out as "0"?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-09-29 18:51
    One other thing.... If I store a Byte variable as "00" will that still come out as "00" or will it come out as "0"?

    I don't blame you for not wanting to make your program more complicated than needed for now.

    As far as storing "00", it depends on what you mean by "00".

    If you want them to be two ASCII zero characters (48), then it wouldn't fit in a single byte. Bytes are used to store characters (one byte per chardacter). Based on your earlier posts, it looks like you want to store your data as ASCII characters. This will require one byte for each digit.

    I know there are several threads discussing ASCII characters versus normal variable numbers. I'll try to find some of them and post links here.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-09-29 19:22
    I was hoping to store them as literal values, not Ascii characters? The question has come about because I am updating the time from the computer to the prop only once per hour. Between this time the Prop has a time keeping function that updates all the time variables : Month, Day, Year, Hour, Minute, Second. Each one is it's own variable. When storing the entire date / time as a "string" into the array, I need to bring all the variables together into one "string". I am not sure how to do this and ensure each variable has 2 digits.

    not sure how but something like this : AddData(string(Time_Count_Month & Time_Count_Day & Time_Count_Year & Time_Count_Hours & Time_Count_Minutes & Time_Count_Hours & ":" & pulseCount))

    I don't know how to "add" variables together to make a string. Example :

    Variable1 := 12
    Variable2 := 34

    Ouput = Variable1 & Variable2
    ' 1234
  • Mike GMike G Posts: 2,702
    edited 2012-09-29 19:47
    A byte is made up of 8 bits. A byte can hold 256 different bit states; 0x00 - 0xFF or 0 to 255. The meaning of the bit states is up to you, the programmer, but there are standards.

    Let say you have a byte and the byte equals 0x41 or 65. By the way, 0x41 is hexadecimal notation, in Spin it's $41. So you have this number 65 but what does is mean? Well, it can mean 65 units to the right of 0. It can also mean the character A.

    A long time ago in a galaxy far far away... someone said how the heck do I send an "A" so the guy on the other end of this wire understands I mean "A".

    Alakazam! American Standard Code for Information Interchange (ASCII)
    http://en.wikipedia.org/wiki/ASCII
    not sure how but something like this : AddData(string(Time_Count_Month & Time_Count_Day & Time_Count_Year & Time_Count_Hours & Time_Count_Minutes & Time_Count_Hours & ":" & pulseCount))

    I don't know how to "add" variables together to make a string. Example :

    Variable1 := 12
    Variable2 := 34

    Ouput = Variable1 & Variable2
    ' 1234

    I believe you are struggling with the concept of character encoding.

    BTW, ASCII is not the only encoding scheme but very common.

    However, if you simply want to convey two integers then no need to encode the values.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-09-29 20:26
    So I am sort of half right when it comes to a byte value of no higher than 255 (0 to 255 = 256). Since most of what I work with is numbers, I have troubles deciding between Bits, Bytes, Longs, or Words.

    From what I understand (or think I understand) :

    Bit = 0 or 1
    Byte = 0 to 255
    Long = Not sure
    Word = 65000 or something like that. (max size from SX28 experience)
    String = Text (NA Variable type on Prop)

    In PHP, to append to a string you would do this :

    Variable1 = "01";
    Variable2 = "23";
    Ouput = Variable1 . Variable2;
    echo Output

    This would output on the screen : 0123 with the leading zero. I have yet to figure out how to bring together 2 variables in Spin like this. Since I am working with VB6, I have limitations to what I can send and receive from the PROP when it comes to long variables, but sending multiple variables all at once slows down the VB program which makes it "hang" for a split second. This is why I went to using Arrays to store the pulse count from the meter every hour. I could easily loop through the arrays and send each value to the VB program then clear the array once all values were sent successfully. The problem I have run into is saving the values to an array and tying them to a specific time. I need to do this incase my computer is not on and the software is not pulling information for a period of time. One other way I have thought of doing this is to have a multi-dimensional array so I can store 2 values per pointer.... Example :

    myArray[0][0] := DayOfValue
    myArray[0][1] := TimeOfValue
    myArray[0][2] := pulseCount

    Since VB6 has the DatePart("y", Now) ability which outputs what number day it is in the year (1 to 365), I could store the DayOfValue as a number between 1 and 365. Not sure how it does on leap years though.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-09-29 21:08
    A word is 16 bits and a long is 32 bits.

    I don't think you really need to worry about storing your data as character strings. I'd think you'd be better off storing them as a byte, word or long and just let the serial driver convert them when it's time to send them to the PC.

    The "dec" method will convert the variable to ASCII characters for you. You may need to find a serial object that has a zero padding option. Tim Moore's four port serial object has zero padding.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-09-29 21:32
    I have been using the dec method in the fullduplexserialplus object since that seems to be the only way I can send numbers to my VB program.

    Come to find out, I cannot use multi dimensional arrays in Spin the way i figured :(http://forums.parallax.com/showthread.php?99888-Multi-dimensional-arrays-in-Propeller

    Could someone help me on understanding on how it can be achieved in Spin? I don't quite understand the posts in that thread above or how they are supposed to work. I was hoping to have an array with 255 possible pointers and 3 addition dimensions per pointer...if that makes any sense.

    If this cannot be achieved, I have also found this thread : http://forums.parallax.com/showthread.php?124308-Newb-question-Append-to-a-variable&highlight=append+variable
    I can apparently merge variables together which would allow me to make the array something like this : myArray("DayOfYear:Time_Counter_Hours:pulseCount", "next one saved.....")

    I think the variable append method may be a better method in the long run since I could simply store the "varAll" to the EEPROM for long term storage.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-09-29 21:38
    Duane Degn wrote: »
    A word is 16 bits and a long is 32 bits.

    So I am confused on how you can get the "max variable value" based on the variable type (Byte, Long, Word). If a Long is 16 Bits and a Bit is 1 or 0, then I supposed the max value for a long is 1,111,111,111,111,111 (16ea 1's)? Sorry to sound so much like a newb, but this is one thing I just can't quite grasp.... Just like in school, I failed pre-algebra twice till I had a teacher who said the right thing and everything clicked :p
  • Mike GMike G Posts: 2,702
    edited 2012-09-29 21:49
    You are struggling with types and encoding. Two very different but related concepts.

    The first demo posted will handle a jagged string.

    This demo will handle an integer name value collection.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2012-09-29 22:41
    That is pretty interesting. Did not think about using number variables :) Nice way to accomplish that as well. I may be able to use that in many different places!
Sign In or Register to comment.