Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def adsSyncSetTimeoutEx(port, nMs):
# type: (int, int) -> None
"""Set Timeout.
:param int port: local AMS port as returned by adsPortOpenEx()
:param int nMs: timeout in ms
"""
adsSyncSetTimeoutFct = _adsDLL.AdsSyncSetTimeoutEx
cms = ctypes.c_long(nMs)
err_code = adsSyncSetTimeoutFct(port, cms)
if err_code:
raise ADSError(err_code)
data = plc_data_type(value)
data_pointer = ctypes.pointer(data)
data_length = ctypes.sizeof(data)
error_code = sync_write_request(
port,
ams_address_pointer,
index_group_c,
index_offset_c,
data_length,
data_pointer,
)
if error_code:
raise ADSError(error_code)
bytes_read = ctypes.c_ulong()
bytes_read_pointer = ctypes.pointer(bytes_read)
error_code = sync_read_request(
port,
ams_address_pointer,
index_group_c,
index_offset_c,
data_length,
data_pointer,
bytes_read_pointer,
)
if error_code:
raise ADSError(error_code)
# If we're reading a value of predetermined size (anything but a string),
# validate that the correct number of bytes were read
if (
check_length
and data_type != PLCTYPE_STRING
and bytes_read.value != data_length.value
):
raise RuntimeError(
"Insufficient data (expected {0} bytes, {1} were read).".format(
data_length.value, bytes_read.value
)
)
if return_ctypes:
return data
write_length = ctypes.sizeof(write_data)
err_code = sync_read_write_request(
port,
ams_address_pointer,
index_group_c,
index_offset_c,
read_length,
read_data_pointer,
write_length,
write_data_pointer,
bytes_read_pointer,
)
if err_code:
raise ADSError(err_code)
# If we're reading a value of predetermined size (anything but a string),
# validate that the correct number of bytes were read
if (
check_length
and read_data_type != PLCTYPE_STRING
and bytes_read.value != read_length.value
):
raise RuntimeError(
"Insufficient data (expected {0} bytes, {1} were read).".format(
read_length.value, bytes_read.value
)
)
if return_ctypes:
return read_data
return callback(notification, data)
c_callback = NOTEFUNC(wrapper)
err_code = adsSyncAddDeviceNotificationReqFct(
port,
pAmsAddr,
nIndexGroup,
nIndexOffset,
ctypes.byref(attrib),
c_callback,
nHUser,
ctypes.byref(pNotification),
)
if err_code:
raise ADSError(err_code)
callback_store[(adr, pNotification.value)] = c_callback
return (pNotification.value, hnl)
else:
data = plc_data_type(data)
data_pointer = ctypes.pointer(data)
data_length = ctypes.sizeof(data)
error_code = sync_write_control_request(
port,
ams_address_pointer,
ads_state_c,
device_state_c,
data_length,
data_pointer,
)
if error_code:
raise ADSError(error_code)