Template PLCnext CLI: Sederhanakan Pengembangan C++ Anda
Artikel ini berlaku untuk versi PLCnext CLI 22.0.0 LTS.
Jika Anda memprogram perangkat PLCnext Control menggunakan C++, Anda mungkin telah menggunakan kode template yang diinstal dengan PLCnext CLI.
Di Visual Studio dan Eclipse, Anda harus memilih template proyek yang ingin Anda gunakan saat membuat proyek baru dengan PLCnext project wizard (yang diinstal dengan PLCnext IDE extension).
Saat menghasilkan proyek C++ baru melalui command-line dengan PLCnext CLI, Anda juga harus menentukan template proyek.
Tiga template proyek default adalah:
project The project is a template for user programs. They are managed by the PLM (Program Library Manager). acfproject The acfproject is a template for component based platform development. The resulting component will be managed by the ACF (Application Component Framework). consumablelibrary The consumable library is a template, that creates a library which can be used by other projects.
Informasi di atas dapat dilihat dengan menjalankan perintah plcncli new. Informasi lebih lanjut tentang jenis proyek ini dapat ditemukan di PLCnext Info Center:
- Delimitation of ACF and PLM
- Creating and using shared libraries
Artikel ini menjelaskan cara mengaktifkan dan menggunakan template proyek tambahan yang diinstal dengan PLCnext CLI, dan cara membuat serta menggunakan template proyek Anda sendiri.
Tetapi terlebih dahulu, mari kita lihat bagaimana sistem Template PLCnext CLI bekerja.
Sistem Template PLCnext CLI
PLCnext CLI mencakup pengaturan default yang disebut TemplateLocations, yang memberitahukannya di mana menemukan kode template. Anda dapat melihat nilai pengaturan ini dengan perintah berikut:
user@machine:~$ plcncli get setting TemplateLocations { "setting": { "TemplateLocations": "./Templates/Templates.xml" } }
Jalur ke file xml relatif terhadap jalur instalasi plcncli.
Jika Anda menjelajahi direktori ./Templates, Anda akan melihat file Templates.xml, dan sejumlah sub-direktori.
Menampilkan isi file Templates.xml ...
user@machine:~/plcncli/Templates$ cat Templates.xml
<?xml version="1.0" encoding="utf-8"?> <Templates xmlns="https://www.phoenixcontact.com/schema/clitemplates"> <Include type="Template">ProjectTemplate/TemplateDescription.xml</Include> <Include type="Template">ProgramTemplate/TemplateDescription.xml</Include> <Include type="Format">ProjectTemplate/FormatTemplates.xml</Include> <Include type="Template">ComponentTemplate/TemplateDescription.xml</Include> <Include type="Template">BaseTemplates/BaseTemplateDescription.xml</Include> <Include type="Template">BaseTemplates/CodeTemplateDescription.xml</Include> <Include type="Fields">BaseTemplates/FieldTemplates.xml</Include> <Include type="Types">BaseTemplates/TypeTemplates.xml</Include> <Include type="Format">BaseTemplates/FormatTemplates.xml</Include> <Include type="Template">AcfProjectTemplate/TemplateDescription.xml</Include> <Include type="Template">BaseProjectTemplate/TemplateDescription.xml</Include> <Include type="Template">AcfComponentTemplate/TemplateDescription.xml</Include> <Include type="Template">BaseComponentTemplate/TemplateDescription.xml</Include> <Include type="Template">ConsumableLibraryTemplate/TemplateDescription.xml</Include> </Templates>
... Anda dapat melihat bahwa ada referensi ke file TemplateDescription.xml berbagai jenis di direktori di bawah direktori Templates. File-file ini berisi petunjuk yang memberitahukan PLCnext CLI apa yang harus dilakukan ketika template tersebut digunakan.
File deskripsi template di atas mengimplementasikan tiga template proyek default yang sudah Anda kenal.
Cara Mengaktifkan Template Tambahan
Ada satu template proyek “tersembunyi” yang diinstal secara default dengan PLCnext CLI, yang tidak tersedia secara default:
minimumproject This is the same as acfproject, but without the capability to create Port variables in the Global Data Space.
Kami dapat membuat template ini tersedia untuk PLCnext CLI dengan menambahkan file MinimalAcfTemplates.xml – yang mungkin sudah Anda perhatikan di direktori Templates – ke pengaturan lokasi template:
user@machine:~$ plcncli set setting TemplateLocations ./Templates/MinimalAcfTemplates.xml --add
File XML baru telah ditambahkan ke lokasi template:
user@machine:~$ plcncli get setting TemplateLocations { "setting": { "TemplateLocations": "./Templates/Templates.xml;./Templates/MinimalAcfTemplates.xml" } }
... dan saat perintah plcncli new dijalankan, minimumproject sekarang muncul dalam daftar opsi. Template ini sekarang dapat digunakan untuk membuat proyek C++ baru dari command line.
Catatan: Menambahkan template proyek ke PLCnext CLI tidak secara otomatis menambahkan template proyek ke wizard proyek Visual Studio atau Eclipse.
Cara Membuat Template PLCnext CLI Anda Sendiri
Pembuatan template PLCnext CLI kustom akan divisualisasikan menggunakan contoh sederhana. Sebuah template PLCnext CLI akan dibuat yang menghasilkan aplikasi konsol “Hello World!” dalam C++.
Prosedurnya adalah sebagai berikut:
-
Buat direktori untuk template proyek PLCnext CLI baru.
Di Linux:
user@machine:~/plcncli/Templates$ mkdir ExeTemplate && cd ExeTemplate
-
Buat file yang berisi kode template C++, yang dapat digunakan sebagai titik awal untuk setiap proyek baru dari jenis ini.
user@machine:~/plcncli/Templates/ExeTemplate$ touch Main.cpp
Kode C++ yang digunakan dalam contoh ini adalah:
#include int main() { std::cout << "Hello World!" << std::endl; return 0; }
-
Buat file sumber cmake template.
user@machine:~/plcncli/Templates/ExeTemplate$ touch CMakeLists.txt
Kode CMake yang digunakan dalam contoh ini adalah:
cmake_minimum_required(VERSION 3.13) project($(name)) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() ################# create target ####################################################### file(GLOB_RECURSE Headers CONFIGURE_DEPENDS src/*.h src/*.hpp src/*.hxx) file(GLOB_RECURSE Sources CONFIGURE_DEPENDS src/*.cpp) add_executable(${CMAKE_PROJECT_NAME} ${Headers} ${Sources}) ####################################################################################### ################# set install directories ############################################# string(REGEX REPLACE "^.*\\(([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*$" "\\1" _ARP_SHORT_DEVICE_VERSION ${ARP_DEVICE_VERSION}) set(BIN_INSTALL_DIR ${ARP_DEVICE}_${_ARP_SHORT_DEVICE_VERSION}/${CMAKE_BUILD_TYPE}) ####################################################################################### ################# project include-paths ############################################### target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE $) ####################################################################################### ################# include arp cmake module path ####################################### list(INSERT CMAKE_MODULE_PATH 0 "${ARP_TOOLCHAIN_CMAKE_MODULE_PATH}") ####################################################################################### ################# set link options #################################################### # WARNING: Without --no-undefined the linker will not check, whether all necessary # # libraries are linked. When a library which is necessary is not linked, # # the firmware will crash and there will be NO indication why it crashed. # ####################################################################################### target_link_options(${CMAKE_PROJECT_NAME} PRIVATE LINKER:--no-undefined) ####################################################################################### ################# add link targets #################################################### find_package(ArpDevice REQUIRED) find_package(ArpProgramming REQUIRED) target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE ArpDevice ArpProgramming) ####################################################################################### ################# install ############################################################# install(TARGETS ${CMAKE_PROJECT_NAME} RUNTIME DESTINATION ${BIN_INSTALL_DIR}) unset(_ARP_SHORT_DEVICE_VERSION) #######################################################################################
Perhatikan bahwa kode CMake mencakup placeholder $(name), yang akan diganti dengan nama proyek yang diteruskan ke perintah plcncli new.
-
Opsional: Tambahkan file README ke direktori template baru, yang juga dapat digunakan untuk membuat file README di setiap proyek baru yang dibuat dari template ini.
-
Buat file konfigurasi proyek PLCnext CLI.
File ini digunakan untuk menyimpan informasi konfigurasi proyek untuk PLCnext CLI, misalnya daftar target yang harus dibangun proyek.
Untuk contoh ini, file .proj disalin dari direktori ConsumableLibraryTemplate dan diedit untuk menghasilkan hal berikut:
<?xml version="1.0" encoding="utf-8"?> <ProjectSettings xmlns="https://www.phoenixcontact.com/schema/cliproject"> <Type>exeproject</Type> <Version>0.1</Version> <Name>$(name)</Name> </ProjectSettings>
Bidang Type harus berisi nama unik dari template proyek ini, yang juga akan disertakan dalam file deskripsi template di langkah berikutnya.
Bidang Name berisi placeholder $(name), yang akan diganti dengan nama proyek selama pembuatan setiap proyek baru.
-
Buat file deskripsi template.
Untuk contoh ini, file TemplateDescription.xml disalin dari direktori ConsumableLibraryTemplate dan diedit untuk menghasilkan hal berikut:
<?xml version="1.0" encoding="utf-8"?> <TemplateDescription name="exeproject" isRoot="true" basedOn="baseproject" supportedFirmwareVersions="19.0.0.16199" requiredCliVersion="19.0.0.660" version="1.0" xmlns="https://www.phoenixcontact.com/schema/clitemplates" identifier="ProjectSettingsIdentifier"> <File name="plcnext.proj" template=".proj"/> <File name="CMakeLists.txt" template="CMakeLists.txt"/> <File name="README.md" template="README.md"/> <File name="$(name)Main.cpp" template="Main.cpp" path="src"/> <Description>Create a new stand-alone executable project.</Description> <Example> <Arguments> <Argument name="name" value="MyExe"/> </Arguments> <Description>creates a new stand-alone executable project in the directory 'MyExe'</Description> </Example> </TemplateDescription>
Bidang-bidang dalam deskripsi template meliputi:
-
name="exeproject" : Memungkinkan proyek dibuat dari template ini menggunakan perintah plcncli new exeproject.
-
File name="A" template="B" path="C" : Membuat file dalam proyek baru, di jalur yang ditentukan, berdasarkan file template yang ditentukan. Nama file juga dapat mencakup placeholder seperti $(name), yang akan diganti dengan nama proyek selama pembuatan setiap proyek baru.
- Bidang
Description dan Example, yang ditampilkan di bagian yang relevan dari sistem bantuan plcncli.
-
Buat file Templates baru, yang akan digunakan untuk memberitahukan PLCnext CLI tentang template baru kami.
Untuk contoh ini, file MinimalAcfTemplates.xml di direktori Templates disalin ke file bernama CustomTemplates.xml, dan file tersebut diedit untuk menghasilkan hal berikut:
<?xml version="1.0" encoding="utf-8"?> <Templates xmlns="https://www.phoenixcontact.com/schema/clitemplates"> <Include type="Template">ExeTemplate/TemplateDescription.xml</Include> </Templates>
-
Daftarkan template kustom dengan PLCnext CLI
user@machine:~$ plcncli set setting TemplateLocations ./Templates/CustomTemplates.xml --add
Jalur relatif terhadap direktori instalasi PLCnext CLI.
-
Coba!
Daftar perintah bash berikut menunjukkan fitur-fitur template baru:
plcncli new # The help text includes the new template and description plcncli new exeproject --help # Displays the example showing how to use this template plcncli new exeproject --name "HelloWorld" # Creates a new project based on the template cd HelloWorld plcncli set target -n AXCF2152 -v 2022 --add # Sets the build target plcncli build # Builds the project with the default template code scp bin/AXCF2152_22.0.3.129/Release/HelloWorld admin@192.168.1.10:~ # Copies the executable to the device ssh admin@192.168.1.10 ./HelloWorld # Runs the executable!
Kontribusi
Di masa depan, direncanakan bahwa kontribusi ke proyek PLCnext CLI Templates di Github akan mungkin. Sampai saat itu, Anda dapat membuka sebuah issue dengan ide untuk template baru, atau dengan contoh template yang telah Anda buat sendiri.
Pertanyaan atau komentar?
Posting komentar di bawah, atau ajukan pertanyaan di PLCnext Community Forum.