PDA

View Full Version : Help 2 understand NV area - data types


korda
05-26-2008, 04:20 PM
Can you provide any guideline on how NV area stores/reads saved data. This includes byte order, types and etc. I wonder is PC's C# integer can be stored in NV, read the same way by a node's script and also sent back to PC as an integer... variable types declaration in Phyton is very loose, and it is not explained in the PDF documentation on how to save and read back data correctly.
Also, each memory location in RFE - is it a byte or two bytes? If default type in Phyton is 16-bit int, is that the same for a memory in RFE?

mgenti
05-26-2008, 06:13 PM
For some quick over information on the NV params I would take a look at this thread:
http://forums.synapse-wireless.com/showthread.php?t=19

Byte order should be handled by the XML-RPC libraries on both ends and should not be something to worry about in your application code. SNAPpy does not have type declarations but automatically stores the internal type along with the value when you save an NV param. When the NV param is read back it retrieves the type and value automatically. This allows you to use the value just like you did when you saved it.

If you are using a programming language that is typed, liked C#, than I would recommend having your function accept a "object" as the parameter type. Then you can convert this parameter in your application to the type you want. This way if you are saving different types of values into the same NV param ID you can still handle that in the same function.

As far as the memory locations are concerned, these are not recommended to be written to by user programs. Currently the only place to store information is in the NV param area. Writing directly to memory locations is not advised due to where we store SNAP may change with different revisions.

kbanks
05-27-2008, 10:19 AM
Can you provide any guideline on how NV area stores/reads saved data. This includes byte order, types and etc.

You should only access the NV Params using the provided API. Internal details about memory locations used, formatting, byte order (etc.) are subject to change over time (and in fact we are on our second internal format).

I wonder is PC's C# integer can be stored in NV, read the same way by a node's script and also sent back to PC as an integer... variable types declaration in Phyton is very loose, and it is not explained in the PDF documentation on how to save and read back data correctly.

If you have data that is not a SNAPpy int (16 bit, signed), or a boolean, you will have to store it in NV as a string.

Also, each memory location in RFE - is it a byte or two bytes? If default type in Phyton is 16-bit int, is that the same for a memory in RFE?

The RF Engine uses an eight-bit microcontroller, a single memory location is one byte.

SNAPpy ints actually occupy three bytes of memory at runtime, and 4 bytes in NV (again, subject to change without notice)

korda
05-27-2008, 10:40 AM
So far I have not seen Snapy scripts using any variable types, except "Global" during declaration. And as a function parameters, variables named but not specified of their type.
So the question remains - how the function saveNvParameters (128, abc)
knows how many bytes to save and what type?

kbanks
05-27-2008, 11:14 AM
So the question remains - how the function saveNvParameters (128, abc) knows how many bytes to save and what type?

In SNAPpy, like in full Python, the type of variables is automatically determined by what you put in them.

foo = 123 # foo becomes an integer
foo = "123" # foo becomes a string

Behind the scenes, variable foo has both a type and a value.

SNAPpy ints are always two bytes. SNAPpy strings include an internal "length" field as well (in addition to the type and value).

To summarize: SNAPpy knows the types and sizes of all its variables, and records that additional information into NV along with the actual value.

NOTE - Your example needs to be

saveNvParam(128, "abc")

korda
05-27-2008, 11:36 AM
Thanks, in case when I receive from RPC call two variables of int type (32-bit ), how do I increment the address for consecutive savings? Is the example below correct?
...
int abc1 = 123456789;// PC 32-bit integer
int abc1 = 123456789;// PC 32-bit integer
...
#py script (saving):
saveNvParameter(128, "abc1")
saveNvParameter(132, "abc2")
...
#py script (reading) - how do I declare abc1, abc2 in py to retain 32-bit int properties for sending it back to C# and to local UART0?
abc1 = readNvParameter(128)
abc2 = readNvParameter(132)

kbanks
05-27-2008, 01:02 PM
Thanks, in case when I receive from RPC call two variables of int type (32-bit ), how do I increment the address for consecutive savings? Is the example below correct?
...
int abc1 = 123456789;// PC 32-bit integer
int abc1 = 123456789;// PC 32-bit integer
...
#py script (saving):
saveNvParameter(128, "abc1")
saveNvParameter(132, "abc2")

No, your example is wrong on several counts. It appears I made things more confusing instead of less confusing with my last post. Let me try again...

First of all, the "ID" parameter for the NV calls is just that: an identifier. It does not correspond to a memory address within the NV memory area.

You are correct in starting your IDs at 128, but you should just go ahead and number your NV parameters consecutively (128, 129, 30, etc.), regardless of how "big" they actually are.

saveNvParam(128, "abc1")
saveNvParam(129, "abc2")
saveNvParam(130, 0)
saveNvParam(131, 32767)

Second, when I told you to put the second paramter in quotes, it was because I thought you literally wanted to save the value "abc".

foo = 123

saveNvParam(128, foo)

You have just saved the numberic value 123.

saveNvParam(128, "foo")

You have just saved the letters "f" "o" "o" to NV.

foo = "bar"

saveNvParam(128, foo)

You have just saved the letters "b" "a" "r" to NV.

Thirdly, you cannot pass 32-bit integers to SNAPpy. SNAPpy only has 16-bit integers. If you need SNAPpy to "hold onto" some big numbers for you, they will have to be encoded as strings. For example

abc1num = 12345678 # notice this number will not fit in a SNAPpy integer
abc1str = str(abc1num)

Now we have an eight-character string, which SNAPpy can accept (and write to, and later read back from NV).

rpc(addr, 'functionName', abc1num) # this will not work

rpc(addr, 'functionName', abc1str) # this will

Note that SNAPpy cannot natively do "math" on strings

#py script (reading) - how do I declare abc1, abc2 in py to retain 32-bit int properties for sending it back to C# and to local UART0?
abc1 = readNvParameter(128)
abc2 = readNvParameter(132)

As should be clear from the discussion above, you cannot. SNAPpy only deals with booleans, 16-bit integers, and strings. As for preserving those types, consider:

saveNvParam(128, foo)
bar = loadNvParam(128)

Variable bar will automatically have the same type (boolean, int, or string) as the originally saved variable foo.

Conversion of 32 bit integers to strings, and strings back to 32 bit integers will have to be done on the PC side (Portal script or your Gateway client).

korda
05-27-2008, 02:51 PM
Thanks for the examples above, however, I have more questions.
When you refer to strings in PY - do you mean ASCII string or BYTE array?
Can you make practical example for the C# client code and PY to explain this ?

mgenti
05-27-2008, 03:18 PM
In SNAPpy everything that is surrounded by quotes will be considered a string, even if it contains binary data or an integer. Here is some example output using the Python interactive shell:

>>> type('\x00\x01\x02')
<type 'str'>
>>> type('SNAPpy')
<type 'str'>
>>> type('2')
<type 'str'>
>>> type(2)
<type 'int'>


When you see something like:

>>> a = "SNAPpy"
>>> print a[2]
A

The variable "a" is a string and "a[2]" is preforming a string slice operation to retrieve the third character (starting with 0).

mgenti
05-27-2008, 03:24 PM
When interfacing C# with the SNAP Gateway you will see some data represented as a byte array type, such as the network address. The conversion back and forth between these two types should be handled for you by the SNAP Gateway. You can do some internal conversion with the following functions in C#:
http://msdn.microsoft.com/en-us/library/system.text.encoding(VS.71).aspx