Manufaktur industri
Industri Internet of Things | bahan industri | Pemeliharaan dan Perbaikan Peralatan | Pemrograman industri |
home  MfgRobots >> Manufaktur industri >  >> Manufacturing Technology >> Teknologi Industri

Template CLI PLCnext

Artikel ini berlaku untuk PLCnext CLI versi 22.0.0 LTS.

Jika Anda memprogram perangkat Kontrol PLCnext menggunakan C++, Anda mungkin telah menggunakan template kode yang diinstal dengan PLCnext CLI.

Di Visual Studio dan Eclipse, Anda harus memilih template proyek yang ingin Anda gunakan saat membuat proyek baru dengan wizard proyek PLCnext (diinstal dengan ekstensi IDE PLCnext).

Saat membuat proyek C++ baru pada baris perintah 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 plcncli new memerintah. Informasi lebih lanjut tentang jenis proyek ini dapat ditemukan di Pusat Info PLCnext:

  • Pembatasan ACF dan PLM
  • Membuat dan menggunakan pustaka bersama

Artikel ini menjelaskan cara mengaktifkan dan menggunakan templat proyek tambahan yang diinstal dengan PLCnext CLI, dan cara membuat dan menggunakan templat proyek Anda sendiri.

Tapi pertama-tama, mari kita lihat bagaimana sistem PLCnext CLI Template bekerja.

Sistem Template CLI PLCnext

PLCnext CLI menyertakan pengaturan default yang disebut TemplateLocations , yang memberitahunya di mana menemukan templat kode. 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 plcncli jalur instalasi.

Jika Anda menelusuri ./Templates direktori, Anda akan melihat Templates.xml file, dan sejumlah sub-direktori.

Mencantumkan isi Templates.xml berkas ...

user@machine:~/plcncli/Templates$ cat Templates.xml 
<?xml version="1.0" encoding="utf-8"?>
<Templates xmlns="http://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 TemplateDescription.xml berbagai jenis file dalam direktori di bawah Templates direktori. Ini berisi instruksi yang memberi tahu PLCnext CLI apa yang harus dilakukan ketika template itu digunakan.

File deskripsi template yang ditampilkan di atas menerapkan tiga template proyek default yang sudah Anda kenal.

Cara mengaktifkan template tambahan

Ada satu templat 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.

Kita dapat membuat template ini tersedia untuk PLCnext CLI dengan menambahkan MinimalAcfTemplates.xml file - yang mungkin sudah Anda perhatikan di Templates direktori - ke pengaturan lokasi template:

user@machine:~$ plcncli set setting TemplateLocations ./Templates/MinimalAcfTemplates.xml --add

File XML baru sekarang telah ditambahkan ke lokasi template:

user@machine:~$ plcncli get setting TemplateLocations

{
  "setting": {
    "TemplateLocations": "./Templates/Templates.xml;./Templates/MinimalAcfTemplates.xml"
  }
}

... dan ketika plcncli new perintah dijalankan, minimumproject sekarang muncul dalam daftar opsi. Template ini sekarang dapat digunakan untuk membuat proyek C++ baru dari baris perintah.

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 didemonstrasikan menggunakan contoh sederhana. Sebuah template CLI PLCnext akan dibuat yang menghasilkan "Hello World!" aplikasi konsol di C++.

Prosedurnya adalah sebagai berikut:

  1. Buat direktori untuk template proyek PLCnext CLI yang baru.

    Di Linux:

    user@machine:~/plcncli/Templates$ mkdir ExeTemplate && cd ExeTemplate
    
  2. Buat file yang berisi kode C++ template, yang dapat digunakan sebagai titik awal untuk setiap proyek baru 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;
    }   
    
  3. 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 menyertakan placeholder $(name) , yang akan diganti dengan nama proyek yang diteruskan ke plcncli new perintah.

  4. 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.

  5. Buat file konfigurasi proyek CLI PLCnext.

    File ini digunakan untuk menyimpan informasi konfigurasi proyek untuk PLCnext CLI, mis. daftar target untuk proyek yang harus dibangun.

    Untuk contoh ini, .proj file disalin dari ConsumableLibraryTemplate direktori dan diedit untuk menghasilkan yang berikut:

    <?xml version="1.0" encoding="utf-8"?>
    <ProjectSettings xmlns="http://www.phoenixcontact.com/schema/cliproject">
      <Type>exeproject</Type>
      <Version>0.1</Version>
      <Name>$(name)</Name>
    </ProjectSettings>
    

    Type kolom harus berisi nama unik template proyek ini, yang juga akan disertakan dalam file deskripsi template pada langkah berikutnya.

    Name bidang berisi placeholder $(name) , yang akan diganti dengan nama proyek selama pembuatan setiap proyek baru.

  6. Buat file deskripsi template.

    Untuk contoh ini, TemplateDescription.xml file disalin dari ConsumableLibraryTemplate direktori dan diedit untuk menghasilkan yang 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="http://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>
    

    Kolom 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 di proyek baru, di jalur yang ditentukan, berdasarkan file template yang ditentukan. Nama file juga dapat menyertakan placeholder seperti $(name) , yang akan diganti dengan nama proyek selama pembuatan setiap proyek baru.
    • Description dan Example bidang, yang ditampilkan di bagian yang relevan dari plcncli sistem bantuan.
  7. Buat file Template baru, yang akan digunakan untuk memberitahu PLCnext CLI tentang template baru kita.

    Untuk contoh ini, MinimalAcfTemplates.xml file di Templates direktori disalin ke file bernama CustomTemplates.xml , dan file itu diedit untuk menghasilkan yang berikut:

    <?xml version="1.0" encoding="utf-8"?>
    <Templates xmlns="http://www.phoenixcontact.com/schema/clitemplates">
             <Include type="Template">ExeTemplate/TemplateDescription.xml</Include>
    </Templates>
    
  8. Daftarkan template khusus dengan PLCnext CLI

    user@machine:~$ plcncli set setting TemplateLocations ./Templates/CustomTemplates.xml --add
    

    Jalurnya relatif terhadap direktori instalasi PLCnext CLI.

  9. Cobalah!

    Daftar perintah bash berikut menunjukkan 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 [email protected]:~  # Copies the executable to the device
    ssh [email protected]
    ./HelloWorld                                  # Runs the executable!
    

Berkontribusi

Di masa depan, direncanakan bahwa kontribusi untuk proyek Template CLI PLCnext di Github akan dimungkinkan. Sampai saat itu, Anda dapat membuka masalah dengan ide untuk template baru, atau dengan contoh template yang Anda buat sendiri.

Pertanyaan atau komentar?

Kirimkan komentar di bawah, atau ajukan pertanyaan di Forum Komunitas PLCnext.


Teknologi Industri

  1. Template Kelas C++
  2. Template C++
  3. Python - Pemrosesan XML
  4. Pengaturan VLAN di Teknologi PLCnext
  5. GRPC jarak jauh menggunakan grpcurl
  6. Cara menggunakan Konektor Alibaba Cloud
  7. Akses ke server web PlcNext di DHCP
  8. Gunakan PLCdi sebelah WakeOnLan (WoL) PC Anda
  9. Clustermanagement di PLCnext?
  10. Dasbor Tableau PLCnext