Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of Cli_SetDateTime, Cli_GetDateTime #114

Closed
Lamelynx opened this issue Oct 9, 2018 · 1 comment
Closed

Implementation of Cli_SetDateTime, Cli_GetDateTime #114

Lamelynx opened this issue Oct 9, 2018 · 1 comment
Milestone

Comments

@Lamelynx
Copy link
Contributor

Lamelynx commented Oct 9, 2018

I managed to implement Cli_GetPlcDateTime, Cli_SetPlcDateTime to a PLC S7-400 in my project and want to share.
Maybe someone with more experience can implement this into python-snap7 wrapper.

def get_plc_datetime(self):
    """
    Get date and time from PLC.

    (Wrapper for function Cli_GetPlcDateTime(), this is not yet implemented in main snap7/python project)

    :return: date and time as DateTime
    """

    type_ = ctypes.c_int32
    buffer = (type_ * 9)()

    result = self.library.Cli_GetPlcDateTime(self.pointer, ctypes.byref(buffer))
    snap7.common.check_error(result, context="client")

    return datetime.datetime(
        year = buffer[5] + 1900,
        month = buffer[4] + 1,
        day = buffer[3],
        hour = buffer[2],
        minute = buffer[1],
        second = buffer[0],
        microsecond = 0
    )

def set_plc_datetime(self, dt):
    """
    Set date and time in PLC

    (Wrapper for function Cli_SetPlcDateTime(), this is not yet implemented in main snap7/python project)

    :param dt: Date and time as datetime object
    :return:
    """

    type_ = ctypes.c_int32
    buffer = (type_ * 9)()
    buffer[0] = dt.second
    buffer[1] = dt.minute
    buffer[2] = dt.hour
    buffer[3] = dt.day
    buffer[4] = dt.month - 1
    buffer[5] = dt.year - 1900

    code = self.library.Cli_SetPlcDateTime(self.pointer, ctypes.byref(buffer))
    snap7.common.check_error(code, context="client")

    return code
@gijzelaerr
Copy link
Owner

Thanks! That looks already quite good already. If you want you can try to add it to the code yourself and issue a pull request. Do you know if the emulator supports these calls? If so we can also add it to the test suite.

@gijzelaerr gijzelaerr added this to the 0.11 milestone Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants