Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Using context manager multiple times after each other for
disconnecting and connecting to/from server should work without any
errors.
"""
handle_name = "TestHandle"
value = "Test Value"
with self.plc:
self.assertTrue(self.plc.is_open)
self.plc.write_by_name(handle_name, value, constants.PLCTYPE_STRING)
self.assertFalse(self.plc.is_open)
with self.plc:
self.assertTrue(self.plc.is_open)
self.plc.read_by_name(handle_name, constants.PLCTYPE_STRING)
self.assertFalse(self.plc.is_open)
def test_write_string(self):
value = "Test String 1234."
ads.write(
self.endpoint,
index_group=constants.INDEXGROUP_DATA,
index_offset=1,
value=value,
plc_datatype=constants.PLCTYPE_STRING,
)
# Retrieve list of received requests from server
requests = self.test_server.request_history
# Assert that the server received a request
self.assertEqual(len(requests), 1)
# Assert that the server received the correct command
self.assert_command_id(requests[0], constants.ADSCOMMAND_WRITE)
# Check the value received by the server
received_value = requests[0].ams_header.data[12:]
# String should have been sent null terminated
sent_value = (value + "\x00").encode("utf-8")
@self.plc.notification(constants.PLCTYPE_STRING)
def callback(handle, name, timestamp, value):
self.assertEqual(value, "Hello world!")
def test_multiple_connect(self):
"""
Using context manager multiple times after each other for
disconnecting and connecting to/from server should work without any
errors.
"""
handle_name = "TestHandle"
value = "Test Value"
with self.plc:
self.assertTrue(self.plc.is_open)
self.plc.write_by_name(handle_name, value, constants.PLCTYPE_STRING)
self.assertFalse(self.plc.is_open)
with self.plc:
self.assertTrue(self.plc.is_open)
self.plc.read_by_name(handle_name, constants.PLCTYPE_STRING)
self.assertFalse(self.plc.is_open)
def adsSyncReadByName(adr, dataName, plcDataType):
# type: (AmsAddr, str, Type) -> Any
"""Read data synchronous from an ADS-device from data name.
:param pyads.structs.AmsAddr adr: local or remote AmsAddr
:param string dataName: data name
:param int plcDataType: type of the data given to the PLC, according to
PLCTYPE constants
:rtype: PLCTYPE
:return: value: **value**
"""
# Get the handle of the PLC-variable
hnl = adsSyncReadWriteReq(
adr, ADSIGRP_SYM_HNDBYNAME, 0x0, PLCTYPE_UDINT, dataName, PLCTYPE_STRING
)
# Read the value of a PLC-variable, via handle
value = adsSyncReadReq(adr, ADSIGRP_SYM_VALBYHND, hnl, plcDataType)
# Release the handle of the PLC-variable
adsSyncWriteReq(adr, ADSIGRP_SYM_RELEASEHND, 0, hnl, PLCTYPE_UDINT)
return value
:param pyads.structs.AmsAddr address: local or remote AmsAddr
:param int indexGroup: PLC storage area, according to the INDEXGROUP
constants
:param int index_offset: PLC storage address
:param value: value to write to the storage address of the PLC
:param int plc_data_type: type of the data given to the PLC,
according to PLCTYPE constants
"""
sync_write_request = _adsDLL.AdsSyncWriteReqEx
ams_address_pointer = ctypes.pointer(address.amsAddrStruct())
index_group_c = ctypes.c_ulong(index_group)
index_offset_c = ctypes.c_ulong(index_offset)
if plc_data_type == PLCTYPE_STRING:
data = ctypes.c_char_p(value.encode("utf-8"))
data_pointer = data # type: Union[ctypes.c_char_p, ctypes.pointer]
data_length = len(data_pointer.value) + 1 # type: ignore
else:
if type(plc_data_type).__name__ == "PyCArrayType":
data = plc_data_type(*value)
elif type(value) is plc_data_type:
data = value
else:
data = plc_data_type(value)
data_pointer = ctypes.pointer(data)
data_length = ctypes.sizeof(data)
error_code = sync_write_request(
"""Get the handle of the PLC-variable.
:param int port: local AMS port as returned by adsPortOpenEx()
:param pyads.structs.AmsAddr address: local or remote AmsAddr
:param string data_name: data name
:rtype: int
:return: handle: PLC-variable handle
"""
handle = adsSyncReadWriteReqEx2(
port,
address,
ADSIGRP_SYM_HNDBYNAME,
0x0,
PLCTYPE_UDINT,
data_name,
PLCTYPE_STRING,
)
return handle
data_length = sizeof(nData)
err_code = adsSyncReadWriteReqFct(
pAmsAddr,
nIndexGroup,
nIndexOffset,
nReadLength,
pointer(readData),
data_length,
data,
)
if err_code:
raise ADSError(err_code)
if plcReadDataType == PLCTYPE_STRING:
return readData.value.decode("utf-8")
if type(plcReadDataType).__name__ == "PyCArrayType":
return [i for i in readData]
if hasattr(readData, "value"):
return readData.value
return readData
:param int port: local AMS port as returned by adsPortOpenEx()
:param pyads.structs.AmsAddr adr: local or remote AmsAddr
:param int ads_state: new ADS-state, according to ADSTATE constants
:param int device_state: new machine-state
:param data: additional data
:param int plc_data_type: plc datatype, according to PLCTYPE constants
"""
sync_write_control_request = _adsDLL.AdsSyncWriteControlReqEx
ams_address_pointer = ctypes.pointer(address.amsAddrStruct())
ads_state_c = ctypes.c_ulong(ads_state)
device_state_c = ctypes.c_ulong(device_state)
if plc_data_type == PLCTYPE_STRING:
data = ctypes.c_char_p(data.encode("utf-8"))
data_pointer = data
data_length = len(data_pointer.value) + 1
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,
)