Shared Memory Python example

For HyperVisor and ArSim there is the possibility for using shared memory.
On the AR side this is provided with libraries ArIscShm and ArIscEvent
On the GPOS / Host side there is the brisc.dll

The AS help comes with a ST example for AR and a c example for GPOS/host

This is a Python script that is performing the same task as the c example.
It’s not perfect, but it works. Tested with AS 6.0 and Python 3.11

Also thanks to @corne.geerts , I used parts of his code.

import ctypes
from ctypes import cdll, c_char_p, c_void_p, POINTER, c_long
import time
import keyboard
Handle = ctypes.c_int
AR_EVENT_NAME = "AR_Event"
GPOS_EVENT_NAME = "GPOS_Event"
# Execute ShmEcho sample program first by setting start=TRUE. Then execute this script
if __name__ == "__main__":
    try:
        c_lib = ctypes.CDLL(".\\brisc.dll") # x64\\brisc.dll with python3
        ShmOpen = c_lib.briscShmOpen
        ShmOpen.restype = ctypes.POINTER(ctypes.c_int) # pointer to int
        # open shared memory
        Handle = ShmOpen()
        print("Handle:",Handle)
        # get size of memory
        ShmGetSize = c_lib.briscShmGetSize
        Size = ShmGetSize(Handle)
        print ("Size: ", Size)
        # get address of memory
        ShmGetAddress = c_lib.briscShmGetAddress
        ShmGetAddress.restype = ctypes.POINTER(ctypes.c_char * Size) 
        DataAddress = ShmGetAddress(Handle)
        # create GPOS event
        print("Create event GPOS: ", end="")
        ShmCreateEvent = c_lib.briscEventCreate
        ShmCreateEvent.restype = ctypes.POINTER(ctypes.c_long)
        hEventGPOS = ShmCreateEvent(GPOS_EVENT_NAME.encode('utf-8') )        
        print ("hEventGPOS: ",hEventGPOS)
        # open AR event
        print("Open event made by AR: ", end="")
        ShmOpenEvent = c_lib.briscEventOpen
        ShmOpenEvent.restype = ctypes.POINTER(ctypes.c_long)
        start_time = time.time()
        while time.time() - start_time < 2:
            hEventAR = ShmOpenEvent(AR_EVENT_NAME.encode('utf-8'))
            if hEventAR != 0:
                break
            print("... ", end="")
            time.sleep(1)
        print("hEventAR: ",hEventAR)
        # wait for the GPOS event to be triggered by AR
        BriscEventWait = c_lib.briscEventWait
        BriscEventWait.argtypes = [ctypes.POINTER(ctypes.c_long),ctypes.c_ulong]
        if hEventGPOS != 0:
            Triggered = BriscEventWait(hEventGPOS,2000)
        # get string value from shared memory
        data = ctypes.string_at(DataAddress, 10)
        print(data.decode('utf-8'))
        # write back to memory
        new_data = "Hello AR".encode('utf-8') + b'\0'
        if len(new_data)<Size:
            ctypes.memmove(DataAddress, new_data, len(new_data))
        # trigger the event created by AR
        if hEventAR != 0:
            BriscEventTrigger = c_lib.briscEventTrigger
            BriscEventTrigger.argtypes = [POINTER(c_long)]
            BriscEventTrigger(hEventAR)
        print("Press any key to continue...")
        keyboard.read_event()
    finally:
        c_lib.briscShmClose(Handle)
        if hEventGPOS != 0:
            c_lib.briscEventClose(hEventGPOS)
            print("Event Closed")
10 Likes

cool. I will definitely try it, thanks for sharing :slight_smile:

1 Like

Thanks for sharing :slight_smile:

2 Likes

Thanks! Looking forward to hear your experience!