Python-based DTC writing method
Through the Python-based DTC writing method, DTC data is automatically parsed and written and a verification mechanism is set, the complex DTC configuration problem in the existing technology is solved, and fast and efficient DTC data processing and file quality improvement are achieved.
Patent Information
- Application Number
- CN202510242464.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-03
- Publication Date
- 2025-07-25
AI Technical Summary
The existing DTC configuration method has complex processes and low flexibility in single DTC data processing or small-scale development scenarios, making it difficult to quickly and efficiently configure and adjust DTC data.
Using Python-based DTC writing method, DTC data is automatically parsed and written through xlsx files, and an automatic verification mechanism is set to ensure that the file complies with the specifications, including using openpyxl or pandas library to read xlsx files, and the Intelhex library to read and modify hex files, and automatically checksum repair data.
It realizes rapid address positioning and writing DTC data into hex files, improving file quality and reliability, simplifying operational processes, suitable for small teams and short-cycle tasks, and improving efficiency and flexibility.
Abstract
Description
Technical Field
[0001] The present invention relates to a method for updating automotive software data, and particularly to a method for writing Diagnostic Trouble Code (DTC) based on Python. Background Art
[0002] With the development of automotive technology, the On-Board Diagnostic (OBD) system plays a key role in vehicle fault detection and diagnosis. The OBD system generates DTCs to provide standardized fault information for identifying vehicle problems.
[0003] However, in practice, the limitations of existing DTC configuration methods are gradually emerging. The configuration of DTCs is in the underlying software and integrated with the application layer through lib files. Modifying and regenerating the underlying software is usually very difficult; when adjusting or adding new DTCs, it is necessary to manually search for variable addresses through map files and then locate and fill in data in hex files. This repetitive and time-consuming process has become an obstacle to further optimization in the field of vehicle diagnosis, and there is an urgent need for a more efficient solution to improve the flexibility and accuracy of diagnostic code configuration.
[0004] Chinese Patent CN119249133A discloses a method for developing an automotive DTC fault diagnosis process. By constructing a DTC standard database, a diagnostic process database, and a circuit function loop library, and combining historical vehicle model data, the automated development of the DTC diagnosis process is achieved. This patent can effectively avoid the above problems in traditional Transmission Control Unit (TCU) development, but its main advantage lies in the intelligent recommendation and reuse of multi-vehicle model, multi-brand, and multi-DTC diagnosis processes, rather than the rapid processing of single-vehicle model or single-DTC requirements. As a result, when used in single-DTC data processing or small-scale development scenarios, the process is rather complex. Summary of the Invention
[0005] The object of the present invention is to solve the deficiencies that the existing DTC configuration method is rather complex and has low flexibility when used in single-DTC data processing or small-scale development scenarios, and to provide a method for writing DTC based on Python.
[0006] To solve the above deficiencies of the existing technology, the present invention provides the following technical solutions:
[0007] A method for writing DTC based on Python, characterized in that it includes the following steps:
[0008] Step 1: Create an xlsx file containing the DTC information to be written. The DTC information includes corresponding DTC indexes and DTC data;
[0009] Generate a map file containing the mapping relationship between DTC indexes and memory addresses, as well as the original hex file, as the hexadecimal file to be modified;
[0010] Step 2: Use Python to check whether each DTC index in the xlsx file exists in the map file. If so, execute Step 4; otherwise, execute Step 3;
[0011] Step 3: Exit the process to prompt the user to correct the xlsx file, and then return to Step 2;
[0012] Step 4: Use the intelhex library of Python to read the hex file prepared in Step 1, load it into a modifiable data structure in memory, and cache the start address, end address, and valid range;
[0013] Step 5: Obtain the start address cached in Step 4. Calculate the offset of the target address relative to the start address according to the target address to be written, and locate the write address; Write the DTC data in Intel Hex format following the byte order rule to the write address; Then calculate the checksum and check whether the hex file complies with the standard specification. If so, execute Step 6; otherwise, determine the source of the problem and perform automatic repair. If automatic repair is not possible, return to Step 1;
[0014] Step 6: Save the hex file obtained in Step 5 to the specified output path according to the file name specified by the user, or generate the file name and path according to the default rules to complete the DTC writing.
[0015] Further, in Step 2.1: Use the openpyxl library or pandas library of Python to read the xlsx file obtained in Step 1, extract the DTC data, and store it in a dictionary or other structured format;
[0016] Step 2.2: Parse the map file obtained in Step 1, use Python to check whether each DTC index in the xlsx file exists in the map file. If so, find the memory address corresponding to the DTC index, and check the parsed memory address, and then execute Step 4; otherwise, execute Step 3.
[0017] Further, in Step 5, the calculation of the checksum and the check whether the hex file complies with the standard specification. If so, execute Step 6; otherwise, determine the source of the problem and perform automatic repair. If automatic repair is not possible, return to Step 1 specifically as follows:
[0018] Determine the range of the area to be verified, calculate the checksum for this area range, and then verify whether the checksum matches the checksum stored in the hex file. If the checksum does not match, perform automatic repair. If automatic repair is not possible, return to step 1;
[0019] If the checksum matches, check whether the hex file complies with the standard specifications, including verifying whether the format of each line complies with the Intel Hex format, checking the address range, end flag, and data coverage area of the hex file; if so, execute step 6, otherwise use the built-in method in the intelhex library to repair the hex file. If it cannot be repaired, return to step 1.
[0020] Furthermore, it also includes step 7:
[0021] Step 7.1: Use an independent tool or script to reread the hex file obtained in step 5, verify whether the write address of each DTC data is correct, whether the written DTC data is consistent with the DTC data in the xlsx file in step 1, whether the file format complies with the Intel Hex format, and whether there are other problems that do not belong to the first three problems;
[0022] If the write address is incorrect, execute step 7.2; if the DTC data is inconsistent with the DTC data in the xlsx file in step 1, execute step 7.3; if the file format does not comply with the Intel Hex format, execute step 7.4; if there are other problems that do not belong to the first three problems, execute step 7.5; if there are no such problems, end the process and complete the DTC write;
[0023] Step 7.2: Return to step 1;
[0024] Step 7.3: Check whether the DTC data in step 5 is written in the correct byte order and make corresponding modifications according to the check result; then check the xlsx file and make corresponding modifications according to the check result to make the DTC data in the xlsx file consistent with the written data, and then return to step 2;
[0025] Step 7.4: For each line record written in step 5, recalculate the checksum and update it according to the check result, and then return to step 5;
[0026] Step 7.5: View the error message, locate the problem, make corrections, roll back to step 1, prepare the input file again, and execute the entire process again.
[0027] Compared with the prior art, the beneficial effects of the present invention are:
[0028] (1) A Python-based DTC writing method of the present invention can automatically parse and write DTC data, and is provided with an automatic verification mechanism to ensure that the file complies with the specifications at each stage, improving the file quality and reliability. The present invention has strong applicability, can quickly locate the address and write DTC data into the hex file, with higher efficiency.
[0029] (2) The present invention uses the xlsx file as the data source, and users can quickly adjust the DTC configuration by modifying the xlsx file without directly operating the underlying software or regenerating it.
[0030] (3) The present invention adds testing and verification, automatically rereads the hex file, checks whether the written DTC data is correct, and verifies the format of the hex file to ensure that the final result meets the expectations.
[0031] (4) The present invention does not rely on historical data, can independently complete the DTC configuration task, is suitable for small teams and short-cycle tasks, and is simple to operate and fast to implement. Detailed implementation manner
[0032] The present invention will be further described below in conjunction with exemplary embodiments.
[0033] A Python-based DTC writing method includes the following steps:
[0034] Step 1, prepare the input file;
[0035] Step 1.1, create an xlsx file containing the DTC information to be written, and the DTC information includes the corresponding DTC index and DTC data one by one;
[0036] The DTC index is the fault code, and the DTC data is the specific value or status information related to the fault;
[0037] Step 1.2, generate a map file containing the mapping relationship between the DTC index and the memory address;
[0038] Prepare the original hex file as the hex file to be modified;
[0039] Step 2, use Python to check whether each DTC index in the xlsx file exists in the map file. If so, execute Step 4; otherwise, execute Step 3. Specifically as follows:
[0040] Step 2.1, use the openpyxl library or pandas library of Python to read the xlsx file obtained in Step 1, extract the DTC data, and store it in a dictionary or other structured format for subsequent processing;
[0041] Step 2.2: Parse the map file. Use Python to check whether each DTC index in the xlsx file exists in the map file. If so, find the memory address corresponding to the DTC index, check the parsed memory address, and then execute Step 4; otherwise, execute Step 3.
[0042] Step 3: Exit the process to prompt the user to correct the xlsx file, and then return to Step 2.
[0043] Step 4: Use the intelhex library in Python to read the hex file prepared in Step 1.2, load it into a modifiable data structure in memory, and cache the start address, end address, and valid range.
[0044] Step 5: Write data.
[0045] Step 5.1: Obtain the start address cached in Step 4. Calculate the offset of the target address relative to the start address according to the target address to be written, and locate the write address; Target address = Start address + Offset.
[0046] Step 5.2: Write the DTC data in Intel Hex format following the byte order rule (such as little - endian or big - endian byte order) to the write address.
[0047] Then determine the range of the area to be verified (the range of written bytes or the data area of the entire hex file), calculate the checksum for this area range (the complement of the sum of bytes in each row, and directly obtain the overall checksum of the file through the built - in method hex.hexchecksum() of the intelhex library), and then verify whether the checksum matches the checksum stored in the hex file. If the checksum does not match (indicating an error in the writing process or the file structure is damaged), perform automatic repair. If automatic repair fails, return to Step 1.
[0048] If the checksum matches, check whether the hex file complies with the standard specifications, including verifying whether the format of each row conforms to the Intel Hex format, checking the address range, end flag, and data coverage area of the hex file. If so, execute Step 6; otherwise, use the built - in method in the intelhex library to repair the hex file. If repair fails, return to Step 1.
[0049] Step 6: Output the result.
[0050] Save the hex file obtained in Step 5.2 to the specified output path, provide the file name and path specified by the user, or generate the file name and path according to the default rules.
[0051] Step 7: Test and verify.
[0052] Step 7.1: Use an independent tool or script to reread the hex file obtained in Step 5, verify whether the write address of each DTC data is correct, whether the written DTC data is consistent with the DTC data in the xlsx file of Step 1, whether the file format conforms to the Intel Hex format, and whether there are other problems that do not belong to the first three problems;
[0053] If the write address is incorrect, execute Step 7.2; if the DTC data is inconsistent with the DTC data in the xlsx file of Step 1, execute Step 7.3; if the file format does not conform to the Intel Hex format, execute Step 7.4; if there are other problems that do not belong to the first three problems, such as exceptions during tool operation, file cannot be loaded, device memory limit, etc., execute Step 7.5; if there are no such problems, end the process and complete DTC writing;
[0054] Step 7.2: Explain that the map file generation in Step 1 is incorrect or the hex file writing in Step 5 is incorrect, so it is necessary to return to Step 1;
[0055] Step 7.3: Check whether the DTC data in Step 5 is written in the correct byte order (little endian or big endian), and make corresponding modifications according to the check results; then check the xlsx file and make corresponding modifications according to the check results to make the DTC data in the xlsx file consistent with the written data, and then return to Step 2;
[0056] Step 7.4: For each line record written in Step 5, recalculate the checksum and update according to the checksum result, and then return to Step 5;
[0057] Step 7.5: View the error message, locate the problem, make corrections, roll back to Step 1, re-prepare the input file, and re-execute the entire process.
Claims
1. A Python-based DTC writing method, characterized in that, It includes the following steps: Step 1: Create an xlsx file containing the DTC information to be written, where the DTC information includes corresponding DTC indexes and DTC data; Generate a map file containing the mapping relationship between DTC indexes and memory addresses, and the original hex file, as the hexadecimal file to be modified; Step 2: Use Python to check whether each DTC index in the xlsx file exists in the map file. If so, execute Step 4; otherwise, execute Step 3; Step 3: Exit the process to prompt the user to correct the xlsx file, and then return to Step 2; Step 4: Use the intelhex library of Python to read the hex file prepared in Step 1, load it into a modifiable data structure in memory, and cache the start address, end address, and valid range; Step 5: Obtain the start address cached in Step 4. According to the target address to be written, calculate the offset of the target address relative to the start address to locate the write address; write the DTC data in Intel Hex format following the byte order rule to the write address; then calculate the checksum, and check whether the hex file complies with the standard specification. If so, execute Step 6; otherwise, determine the source of the problem and perform automatic repair. If the automatic repair fails, return to Step 1; Step 6: Save the hex file obtained in Step 5 to the specified output path according to the file name specified by the user, or generate the file name and path according to the default rule to complete the DTC writing.
2. The DTC writing method based on Python according to claim 1, characterized in that, Specifically, Step 2 is: Step 2.1: Use the openpyxl library or pandas library of Python to read the xlsx file obtained in Step 1, extract the DTC data, and store it in a dictionary or other structured format; Step 2.2: Parse the map file obtained in Step 1, use Python to check whether each DTC index in the xlsx file exists in the map file. If so, find the memory address corresponding to the DTC index, and check the parsed memory address, and then execute Step 4; otherwise, execute Step 3.
3. A Python-based DTC writing method according to claim 1 or 2, characterized in that In Step 5, the specific content of calculating the checksum, checking whether the hex file complies with the standard specification. If so, execute Step 6; otherwise, determine the source of the problem and perform automatic repair. If the automatic repair fails, return to Step 1 is: Determine the area range to be checked, calculate the checksum for this area range, and then verify whether the checksum matches the checksum stored in the hex file. If the checksum does not match, perform automatic repair. If the automatic repair fails, return to Step 1; If the checksum matches, check whether the hex file complies with the standard specification, including verifying whether the format of each line complies with the Intel Hex format, checking the address range, end flag, and data coverage area of the hex file; if so, execute Step 6; otherwise, use the built-in method in the intelhex library to repair the hex file. If the repair fails, return to Step 1.
4. A Python-based DTC writing method according to claim 3, characterized in that, It also includes Step 7: Step 7.1: Use an independent tool or script to reread the hex file obtained in Step 5, verify whether the write addresses of each DTC data are correct, whether the written DTC data is consistent with the DTC data in the xlsx file in Step 1, whether the file format conforms to the Intel Hex format, and whether there are other problems that do not belong to the previous three problems; If the write address is incorrect, execute Step 7.2; if the DTC data is inconsistent with the DTC data in the xlsx file in Step 1, execute Step 7.3; if the file format does not conform to the Intel Hex format, execute Step 7.4; If there are other problems that do not belong to the previous three problems, execute Step 7.5; if there are no such problems, end the process and complete the DTC writing; Step 7.2: Return to Step 1; Step 7.3: Check whether the DTC data in Step 5 is written in the correct byte order and make corresponding modifications according to the check results; then check the xlsx file and make corresponding modifications according to the check results to make the DTC data in the xlsx file consistent with the written data, and then return to Step 2; Step 7.4: For each row record written in Step 5, recalculate the checksum and update it according to the checksum result, and then return to Step 5; Step 7.5: View the error message, locate the problem, make corrections, roll back to Step 1, prepare the input file again, and execute the entire process again.
Citation Information
Patent Citations
Automobile DTC fault diagnosis process development method
CN119249133A