FLo-ABB / CognexNativePy

Python library for controlling Cognex cameras using native commands.
https://pypi.org/project/CognexNativePy/
MIT License
4 stars 0 forks source link

Running sample #10

Closed itsniz-b closed 1 month ago

itsniz-b commented 1 month ago

I'm trying to run the sample code with some slight modifications but i get the following error - print(f'{isinstance(row_or_row_offset, (int, float))} {isinstance(row_offset_or_col_offset, float)} { ^ SyntaxError: unterminated string literal (detected at line 210) -

from CognexNativePy import NativeInterface

def main(): try:

Create a socket connection to the Cognex In-Sight vision system and log in

    native_interface = NativeInterface('169.254.7.251', 'admin', '')
    execution_and_online = native_interface.execution_and_online
    file_and_job = native_interface.file_and_job
    image = native_interface.image
    settings_and_cells_values = native_interface.settings_and_cells_values

    # Load the job if it is not already loaded
    job_name = "Main.job"
    if file_and_job.get_file() != job_name:
        if execution_and_online.get_online() == 1:
            execution_and_online.set_online(0)
        file_and_job.load_file(job_name)

    # Get the last image from the camera and save it as a BMP file
    with open('image.bmp', 'wb') as f:
        f.write(image.read_image()["data"])

    # Get the value of the cell B010 (spreadsheet view)
    print(settings_and_cells_values.get_value("B", 397))
    # Set the value of the cell D019 (spreadsheet view) to 53
    #settings_and_cells_values.set_integer_value("D", 19, 53)
    # Set the value of the symbolic tag "Pattern_1.Horizontal_Offset" to 69.3 (EasyBuilder view)
    #settings_and_cells_values.set_float_value("Pattern_1.Horizontal_Offset", 69.3)
    # Get the information of the settings and cells values
    #print(settings_and_cells_values.get_info())

    # Close the socket connection
    native_interface.close()

except Exception as e:
    print(f"Error: {e}")

if name == 'main': main()

This is the code I'm running^ I'm new to python and not exactly sure how to fix this issue. Any help would be greatly appreciated

FLo-ABB commented 1 month ago

The error refers the line 210 of the file SettingsAndCellsValues.py, the "set_region" function, but you don't use this function in your snippet... I test your code and it works without any error :

from CognexNativePy import NativeInterface

def main():

    try:
        # Create a socket connection to the Cognex In-Sight vision system and log in
        native_interface = NativeInterface('169.254.7.251', 'admin', '')
        execution_and_online = native_interface.execution_and_online
        file_and_job = native_interface.file_and_job
        image = native_interface.image
        settings_and_cells_values = native_interface.settings_and_cells_values

        # Load the job if it is not already loaded
        job_name = "Main.job"
        if file_and_job.get_file() != job_name:
            if execution_and_online.get_online() == 1:
                execution_and_online.set_online(0)
            file_and_job.load_file(job_name)

        # Get the last image from the camera and save it as a BMP file
        with open('image.bmp', 'wb') as f:
            f.write(image.read_image()["data"])

        # Get the value of the cell B010 (spreadsheet view)
        print(settings_and_cells_values.get_value("B", 397))
        # Set the value of the cell D019 (spreadsheet view) to 53
        # settings_and_cells_values.set_integer_value("D", 19, 53)
        # Set the value of the symbolic tag "Pattern_1.Horizontal_Offset" to 69.3 (EasyBuilder view)
        # settings_and_cells_values.set_float_value("Pattern_1.Horizontal_Offset", 69.3)
        # Get the information of the settings and cells values
        # print(settings_and_cells_values.get_info())

        # Close the socket connection
        native_interface.close()

    except Exception as e:
        print(f"Error: {e}")

if __name__ == '__main__':
    main()
andrewchastain commented 1 month ago

I had a similar issue, running Python 3.11. It appeared that there was an issue with using a single quote for the multi line literal in SettingsAndCellsValues.py. Putting all of the multi-line literals on a single line or using a triple quote fixed the issue for me.

FLo-ABB commented 1 month ago

I see, in fact this print statements shouldn't be there (forget to delete this debug print). I fix it. Thank you to raise it up! 👍