Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Firstly, I would like to thank the OP for posting Siemens answer back.

Secondly, I would like to point out the documentation for the 'struct' module in Python is : https://docs.python.org/2/library/struct.html

Now, though Siemens has provided an answer, but I think is not complete, for the following reasons:

  1. The byte order is from the least significant byte to the most significant byte (in plain speak, right to left) or from the most significant byte to the least significant byte (left to right)? This affects the unpack format hugely. (I could guess the order when they say "last 16 bytes" for DC lines and FACTS, BUT, god, what do they mean by "last"?)
  2. To unpack, it would be useful to know (and easier to debug) if all the byte types and length are known. Siemens has provided all byte lenght, but not all byte types. For example, what exactly is "1 byte copy id"? What is the data type? Is a double? A 1 byte string or 1 byte pad byte? Similarly, what is the byte type for the reserved bytes?
  3. Endianness of the packed bytes. Assuming it is native?

From Siemens' answer, I could make some guesses. But they are guesses at best, I don't know whether they would work due to the two points mentioned above.

Here we go.

import sliderPy

import struct

# this is the format to unpack, I am gussing it from Siemens' answer
# assuming Siemens means most significant to least significant
# int (i),
# int (i),
# copy id assumed unsigned long long (Q),
# 3 bytes reserved assumed 3 pad bytes (3x),
# int (i),
# int (i),
# int (i),
# 2 bytes ID assumed a single 2-byte string (2s)
# 1 byte OPF branch flow ID (s)
# 1 byte reserved assumed 1 pad byte (x)
CONST_STR_FMT = 'iiQ3xiii2ssx'

# just a simple wrapper to make debugging easier
# you could also use decorator instead
def unpackMapStr(mapStr, str_fmt=CONST_STR_FMT):
    '''
    Just a simple wrapper to make debugging easier.

    Parameter
    ---------
    mapStr : byte
        The map string byte returned from PSSE34.

    str_fmt : string
        The format for byte unpacking. 

        Default = CONST_STR_FMT

    Return
    ---------
    Tuple of the unpacked byte.

    Example
    ---------

    .. code:: python

        # pack a float into the double format bytes
        mapStr = struct.pack('d', 12.34)

        print(unpackMapStr(mapStr, 'd'))

        # note that the return is a tuple
        # you have to get the individual elements to make sense of it
        >>> (12.34,)
    '''

    return struct.unpack(str_fmt, mapStr)

if __name__ == '__main__':

    mydoc = sliderPy.GetActiveDocument()

    mydiagram = mydoc.GetDiagram()

    mycomponents = mydiagram.GetComponents()

    for mycomponent in mycomponents:

        if mycomponent.IsSelected() == True:

            try:

                print('Type : ' + str(mycomponent.GetComponentType()))

                # get the map string bytes
                byte_temp = mycomponent.GetMapString()

                str_temp = str(unpackMapStr(byte_temp))

                # if I guess right and it works, then you should see a tuple
                print('Map String : ' + str_temp)

                # Also note that the strings returned are in bytes
                # you may want to use the "decode" to convert them into 
                # something like 'utf-8'

            except:

                pass

Hope that you could get some idea from it. I don't know whether the code would work or not. But packing and unpacking bytes in Python is not a difficult thing, IF, the format is known.

Firstly, I would like to thank the OP for posting Siemens answer back.

Secondly, I would like to point out the documentation for the 'struct' module in Python is : https://docs.python.org/2/library/struct.html

Now, though Siemens has provided an answer, but I think is not complete, for the following reasons:

  1. The byte order is from the least significant byte to the most significant byte (in plain speak, right to left) or from the most significant byte to the least significant byte (left to right)? This affects the unpack format hugely. (I could guess the order when they say "last 16 bytes" for DC lines and FACTS, BUT, god, what do they mean by "last"?)
  2. To unpack, it would be useful to know (and easier to debug) if all the byte types and length are known. Siemens has provided all byte lenght, but not all byte types. For example, what exactly is "1 byte copy id"? What is the data type? Is a double? A 1 byte string or 1 byte pad byte? Similarly, what is the byte type for the reserved bytes?
  3. Endianness of the packed bytes. Assuming it is native?

From Siemens' answer, I could make some guesses. But they are guesses at best, I don't know whether they would work due to the two points mentioned above.

Here we go.

import sliderPy

import struct

# this is the format to unpack, I am gussing it from Siemens' answer
# assuming Siemens means most significant to least significant
# int (i),
# int (i),
# copy id assumed unsigned long long (Q),
# 3 bytes reserved assumed 3 pad bytes (3x),
# int (i),
# int (i),
# int (i),
# 2 bytes ID assumed a single 2-byte string (2s)
# 1 byte OPF branch flow ID (s)
# 1 byte reserved assumed 1 pad byte (x)
CONST_STR_FMT = 'iiQ3xiii2ssx'

# just a simple wrapper to make debugging easier
# you could also use decorator instead
def unpackMapStr(mapStr, str_fmt=CONST_STR_FMT):
    '''
    Just a simple wrapper to make debugging easier.

    Parameter
    ---------
    mapStr : byte
        The map string byte returned from PSSE34.

    str_fmt : string
        The format for byte unpacking. 

        Default = CONST_STR_FMT

    Return
    ---------
    Tuple of the unpacked byte.

    Example
    ---------

    .. code:: python

        # pack a float into the double format bytes
        mapStr = struct.pack('d', 12.34)

        print(unpackMapStr(mapStr, 'd'))

        # note that the return is a tuple
        # you have to get the individual elements to make sense of it
        >>> (12.34,)
    '''

    return struct.unpack(str_fmt, mapStr)

if __name__ == '__main__':

    mydoc = sliderPy.GetActiveDocument()

    mydiagram = mydoc.GetDiagram()

    mycomponents = mydiagram.GetComponents()

    for mycomponent in mycomponents:

        if mycomponent.IsSelected() == True:

            try:

                print('Type : ' + str(mycomponent.GetComponentType()))

                # get the map string bytes
                byte_temp = mycomponent.GetMapString()

                str_temp = str(unpackMapStr(byte_temp))

                # if I guess right and it works, then you should see a tuple
                print('Map String : ' + str_temp)

                # Also note that the strings returned are in bytes
                # you may want to use the "decode" to convert them into 
                # something like 'utf-8'

            except:

                pass

Hope that you could get some idea from it. I don't know whether the code would work or not. But packing and unpacking bytes in Python is not a difficult thing, IF, the format is known.