初始化工程
This commit is contained in:
21
source/CBB/Param.py
Normal file
21
source/CBB/Param.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
参数管理模块代码生成
|
||||
|
||||
此脚本提供参数管理模块代码生成相关类与功能函数。
|
||||
|
||||
|
||||
|
||||
'''
|
||||
|
||||
import numpy as np
|
||||
from pandas import DataFrame
|
||||
|
||||
|
||||
def code_cbb_param(sheet: DataFrame) -> str|None:
|
||||
""" 参数页代码生成 """
|
||||
pass
|
||||
|
||||
def code_cbb_params(sheets: dict[str, DataFrame]) -> str|None:
|
||||
""" 完整参数模块配置代码生成 """
|
||||
pass
|
||||
44
source/PV_Inverter/CodeGenerator.py
Normal file
44
source/PV_Inverter/CodeGenerator.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import numpy as np
|
||||
from pandas import DataFrame
|
||||
|
||||
|
||||
def code_param_arm(sheet: DataFrame) -> str|None:
|
||||
""" 生成所需代码内容 """
|
||||
|
||||
result = ""
|
||||
result += "#include <stdint.h>\n"
|
||||
result += "#include <stdbool.h>\n"
|
||||
result += "\n"
|
||||
object_list = []
|
||||
|
||||
serises_variable = sheet['变量名'].dropna()
|
||||
serises_name = sheet['协议结构名称'].dropna()
|
||||
serises_name[serises_variable.index[-1]+1] = 'endline'
|
||||
for index in range(serises_name.shape[0]-1):
|
||||
meta_object = {}
|
||||
meta_object['name'] = serises_name.iloc[index]
|
||||
meta_object['range'] = (serises_name.index[index], serises_name.index[index+1]-1)
|
||||
meta_object['member'] = sheet.loc[serises_name.index[index]: serises_name.index[index+1]-1]
|
||||
|
||||
code_object = f"typedef struct tag_{meta_object['name']}" + "\n"
|
||||
code_object += "{" + "\n"
|
||||
list_codes = []
|
||||
for id, item in meta_object['member'].iterrows():
|
||||
if item['变量名'] is np.nan:
|
||||
continue
|
||||
code_veriable = f" uint16_t "
|
||||
code_veriable += f"a" if item['数据长度'] > 1 else ""
|
||||
code_veriable += f"{item['变量名']}"
|
||||
code_veriable += f"[{item['数据长度']}];" if item['数据长度'] > 1 else ";"
|
||||
code_comment = f"/*{item['地址']} {item['数据项名称']}*/"
|
||||
list_codes.append((index, code_veriable, code_comment))
|
||||
code_len_max = max((len(line[1]) for line in list_codes)) + 2
|
||||
code_len_max = code_len_max if code_len_max > 65 else 65
|
||||
for id, line_prefix, line_suffix in list_codes:
|
||||
code_object += line_prefix + " " * (code_len_max - len(line_prefix)) + line_suffix + "\n"
|
||||
code_object += "}" + f"{meta_object['name']};" + "\n"
|
||||
|
||||
result += code_object + "\n"
|
||||
|
||||
|
||||
return result
|
||||
63
source/main.py
Normal file
63
source/main.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import re
|
||||
import argparse
|
||||
import subprocess
|
||||
import pandas as pd
|
||||
from pathlib import Path
|
||||
|
||||
from PV_Inverter.CodeGenerator import code_param_arm
|
||||
from CBB.Param import code_cbb_params
|
||||
|
||||
def save_file(content: str, path_file: Path):
|
||||
if not path_file.parent.exists():
|
||||
path_file.parent.mkdir(parents=True)
|
||||
|
||||
return path_file.write_text(content)
|
||||
|
||||
def main(args: argparse.Namespace) -> bool:
|
||||
""" 核心执行函数 """
|
||||
global type_list, dev_list
|
||||
file_excel = Path(args.path)
|
||||
code_folder = Path(args.output) if args.output else (file_excel.parent / "result")
|
||||
|
||||
# 读取Excel文件
|
||||
if file_excel.exists():
|
||||
if file_excel.suffix in ['.dec', '.xlsx']:
|
||||
dataframes = pd.read_excel(file_excel, sheet_name=None)
|
||||
elif file_excel.suffix == '.xls':
|
||||
dataframes = pd.read_excel(file_excel, sheet_name=None, engine='xlrd')
|
||||
else:
|
||||
raise ValueError("File format Unsupported.")
|
||||
else:
|
||||
raise ValueError("File does not exist.")
|
||||
|
||||
if args.type == 'COMM_ARM':
|
||||
# 光伏逆变器-ARM数据结构体生成
|
||||
content_code = code_param_arm(dataframes['ARM地址表'])
|
||||
elif args.type == 'CBB_Param':
|
||||
content_code = code_cbb_params(dataframes)
|
||||
save_file(content_code, code_folder / 'param_arm_struct.h')
|
||||
|
||||
if __name__ == "__main__":
|
||||
type_list = ['COMM_DSP', 'COMM_ARM', 'CBB_Param']
|
||||
dev_list = ['PV_INV30', 'PV_INV40', 'PV_INV50']
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("path")
|
||||
parser.add_argument(
|
||||
"-t", "--type",
|
||||
choices=type_list,
|
||||
help=f"Specify the Code Generate type [{', '.join(type_list)}]",
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
"-d", "--device",
|
||||
choices=dev_list,
|
||||
help=f"Specify the device type [{', '.join(dev_list)}]",
|
||||
required=True
|
||||
)
|
||||
|
||||
parser.add_argument("-i", "--ignore")
|
||||
parser.add_argument("-o", "--output")
|
||||
args = parser.parse_args()
|
||||
|
||||
main(args)
|
||||
|
||||
Reference in New Issue
Block a user