OneCompiler

common_def

108

import win32com.client as wc
import clr
import modelPath

def setup(maport_config_file):
try:
# Add CLR references
clr.AddReference(
"ASAM.XIL.Implementation.TestbenchFactory, Version=2.1.0.0, Culture=neutral, PublicKeyToken=fc9d65855b27d387")
clr.AddReference("ASAM.XIL.Interfaces, Version=2.1.0.0, Culture=neutral, PublicKeyToken=bf471dff114ae984")
clr.AddReference(
"ASAM.XIL.Implementation.FrameworkFactory, Version=2.1.0.0, Culture=neutral, PublicKeyToken=d64b0277da8a2c74")

    # Import XIL API.NET classes from the .NET assemblies
    from ASAM.XIL.Implementation.TestbenchFactory.Testbench import TestbenchFactory
    from ASAM.XIL.Interfaces.Testbench.Common.Error import TestbenchPortException
    from ASAM.XIL.Interfaces.Testbench.MAPort.Enum import MAPortState

    MyTestbenchFactory = TestbenchFactory()
    MyTestbench = MyTestbenchFactory.CreateVendorSpecificTestbench("dSPACE GmbH", "XIL API", "2018-B")
    MyMAPortFactory = MyTestbench.MAPortFactory
    MyValueFactory = MyTestbench.ValueFactory
    print("Creating MAPort instance...")

    # Create an MAPort object using the MAPortFactory
    DemoMAPort = MyMAPortFactory.CreateMAPort("DemoMAPort")
    print("...done.\n")

    # Load the MAPort configuration
    print("Configuring MAPort...")
    DemoMAPortConfig = DemoMAPort.LoadConfiguration(maport_config_file)
    # Apply the MAPort configuration
    DemoMAPort.Configure(DemoMAPortConfig, False)
    print("...done.\n")

    if DemoMAPort.State != MAPortState.eSIMULATION_RUNNING:
        # Start the simulation
        print("Starting simulation...")
        DemoMAPort.StartSimulation()
        print("...done.\n")

    return DemoMAPort, MyValueFactory

except TestbenchPortException as ex:
    print("A TestbenchPortException occurred:")
    print("CodeDescription: %s" % ex.CodeDescription)
    print("VendorCodeDescription: %s" % ex.VendorCodeDescription)
    raise

except Exception as e:
    print("An unexpected error occurred:", e)
    raise

def readFromExcel(row, col):
global ApplicationExcel
try:
# Dispatch Excel application
ApplicationExcel = wc.Dispatch("Excel.Application")
ApplicationExcel.Visible = False

    # Open the workbook
    workbook = ApplicationExcel.Workbooks.Open(modelPath.ExcelFilePath)

    # Access the first sheet
    sheet = workbook.Sheets(1)

    # Read the value from the specified cell
    value = sheet.Cells(row, col).Value
    print("Value:", value)

    # Close the workbook without saving
    workbook.Close(SaveChanges=False)
    # Quit the Excel application
    ApplicationExcel.Quit()

    return value
except Exception as e:
    print("An error occurred:", e)
    if 'ApplicationExcel' in locals():
        ApplicationExcel.Quit()

def writeToExcel(row, col, value):
global ApplicationExcel
try:
# Dispatch Excel application
ApplicationExcel = wc.Dispatch("Excel.Application")
ApplicationExcel.Visible = False

    # Open the workbook
    workbook = ApplicationExcel.Workbooks.Open(modelPath.ExcelFilePath)

    # Access the first sheet
    sheet = workbook.Sheets(1)

    # Write the value to the specified cell
    sheet.Cells(row, col).Value = value
    print(f"Written value '{value}' to cell ({row}, {col})")

    # Save the workbook
    workbook.Save()
    # Close the workbook
    workbook.Close()
    # Quit the Excel application
    ApplicationExcel.Quit()
except Exception as e:
    print("An error occurred:", e)
    if 'ApplicationExcel' in locals():
        ApplicationExcel.Quit()

def getUsedRange():
global ApplicationExcel
try:
# Dispatch Excel application
ApplicationExcel = wc.Dispatch("Excel.Application")
ApplicationExcel.Visible = False

    # Open the workbook
    workbook = ApplicationExcel.Workbooks.Open(modelPath.ExcelFilePath)

    # Access the first sheet
    sheet = workbook.Sheets(1)

    # Get the used range
    used_range = sheet.UsedRange
    num_rows = used_range.Rows.Count
    num_cols = used_range.Columns.Count

    print(f"Number of used rows: {num_rows}")
    print(f"Number of used columns: {num_cols}")

    # Close the workbook without saving
    workbook.Close(SaveChanges=False)
    # Quit the Excel application
    ApplicationExcel.Quit()

    return num_rows, num_cols
except Exception as e:
    print("An error occurred:", e)
    if 'ApplicationExcel' in locals():
        ApplicationExcel.Quit()

def KL30_off():
path=modelPath.kl30
value = readFromExcel(4,6,)
return path,value

def KL30_on():
path = modelPath.kl30
val = readFromExcel(5, 6, )
if val == "left":
value = 1
elif val == "right":
value = 2
elif val == "both":
value = 3
else:
value=0
return path,value

def can_write():
signal_name = readFromExcel(6,5)
#msg_name = readFromExcel(6,4)
value = readFromExcel(6,6)

path=modelPath.canWrite+signal_name+"/Value"
return path,value

def SLML_STATUS_FD_LEFT():
global path
signal_name = readFromExcel(7,5)
value=readFromExcel(7,6)

if signal_name == "Low_beam":
    path = modelPath.canReadSLML+"/"+signal_name+"Sts_L"
elif signal_name =="High_beam":
    path = modelPath.canReadSLML+"/"+signal_name+"Sts_L"
return path, value

Call the functions to read and write values

readFromExcel(1, 4)

writeToExcel(1, 4, "New Value")

getUsedRange()