Я работаю над инструментом настройки, который берет таблицу ввода-вывода и преобразует данные в определенный формат. Например, если в электронной таблице указан такой тип данных, как DOL, инструмент идентифицирует его и извлекает соответствующий функциональный блок из библиотеки ПЛК.
Для пояснения: все функциональные блоки уже созданы в библиотека. Задача инструмента состоит в том, чтобы:
Извлечь необходимые функциональные блоки из библиотеки.
Поместить их в сетку релейной логики.
Заполнить их входы и выходы на основе Список ввода-вывода из электронной таблицы.
Измените или сгенерируйте XML-файл, необходимый для импорта этой конфигурации в программу ПЛК.
Поскольку XML-файл, экспортированный из PLCnext Engineer, очень большой , Я изучение наиболее эффективного способа изменения определенных его разделов. Если у кого-то есть идеи или предложения о том, как упростить этот процесс, я буду очень признателен за ваш вклад. Спасибо!
from lxml import etree
import os
# Function to modify the XML with the given number of DOL blocks
def adjust_dol_blocks(xml_file, num_blocks):
try:
tree = etree.parse(xml_file)
except etree.XMLSyntaxError as e:
print(f"Error parsing XML file: {e}")
return
root = tree.getroot()
# Define namespaces, including the xsi namespace
namespace = {
"ns": "www.iec.ch/public/TC65SC65BWG7TF10",
"xsi": "http://www.w3.org/2001/XMLSchema-instance"
}
# Find the BodyContent section with xsi:type="LD"
body_content = root.xpath(".//ns:BodyContent[@xsi:type='LD']", namespaces=namespace)
if not body_content:
print("Error: Could not find the BodyContent element with xsi:type='LD'.")
return
body_content = body_content[0] # Take the first match if multiple
# Find all elements within the BodyContent
rungs = body_content.xpath(".//ns:Rung", namespaces=namespace)
# Current number of DOL blocks in the XML
current_blocks = len(rungs)
if current_blocks < num_blocks:
# Add new DOL blocks if needed
for i in range(current_blocks, num_blocks):
new_rung = create_new_rung(i, namespace)
body_content.append(new_rung)
elif current_blocks > num_blocks:
# Remove excess DOL blocks if needed
for i in range(current_blocks - 1, num_blocks - 1, -1):
body_content.remove(rungs)
# Save the modified XML back to the file
new_file_path = "modified_" + os.path.basename(xml_file)
tree.write(new_file_path, pretty_print=True, xml_declaration=True, encoding="utf-8")
print(f"Modified XML file saved as {new_file_path}")
def create_new_rung(index, namespace):
# This function creates a new Rung with a unique DOL block
rung = etree.Element("{www.iec.ch/public/TC65SC65BWG7TF10}Rung", evaluationOrder=str(index + 1))
rel_position = etree.SubElement(rung, "{www.iec.ch/public/TC65SC65BWG7TF10}RelPosition", x="26", y=str(4 + index * 32)) # Adjust Y for spacing
# Add a new FbdObject (DOL block)
fbd_object = etree.SubElement(rung, "{www.iec.ch/public/TC65SC65BWG7TF10}FbdObject",
xsi_type="Block", globalId=f"ID_NewProgram_Code_{index + 1}",
typeName="DOL", instanceName=f"DOL{index + 1}")
# Adding the required internal structure (like AddData)
add_data = etree.SubElement(fbd_object, "{www.iec.ch/public/TC65SC65BWG7TF10}AddData")
data = etree.SubElement(add_data, "{www.iec.ch/public/TC65SC65BWG7TF10}Data", name="objectInformation", handleUnknown="preserve")
return rung
def main():
# Prompt for file path and validate it
xml_file = input("Enter the full file path of the XML file: ").strip()
if not os.path.isfile(xml_file):
print(f"Error: The file '{xml_file}' does not exist. Please check the path and try again.")
return
# Prompt for the number of DOL blocks
try:
num_blocks = int(input("Enter the desired number of DOL blocks: ").strip())
except ValueError:
print("Error: Please enter a valid integer for the number of blocks.")
return
# Call the function to adjust the DOL blocks
adjust_dol_blocks(xml_file, num_blocks)
# Run the main function
if __name__ == "__main__":
main()
< /code>
Это мой код в настоящее время, который принимает вставленный XML -файл и попросите пользователя номера блоков. Но в конечном итоге я хочу, чтобы это было просто автоматически. и чисто основание на том, сколько, например, тегов DOL в файле CSV.
Подробнее здесь: https://stackoverflow.com/questions/793 ... d-on-csv-i
Как автоматизировать размещение функционального блока ПЛК и модификацию XML на основе ввода CSV? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как получить заголовки запроса на модификацию при обратном вызове Response
Anonymous » » в форуме Android - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-
-
-
React игнорирует/не выполняет модификацию DOM с помощью JQuery или JavaScript
Anonymous » » в форуме Jquery - 0 Ответы
- 11 Просмотры
-
Последнее сообщение Anonymous
-