l0rdjcd

Raspi - [[ Automatic FANUC-CNC-File-Splitter ]] - Works with 16i, 18i, 31i & 0i

Jun 23rd, 2021 (edited)
1,633
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. ########################################################################
  3. #   This program extracts all programs individually from
  4. #   the *.ALL into individual files.
  5. #
  6. #   Please report errors, criticism or suggestions. THANKS
  7. #
  8. #   Tested to FANUC version
  9. #       0i, 16i, 18i, 31i
  10. #
  11. #   Coded by Sebastian Staitsch
  12. #       bash: 5.0.3(1)
  13. #
  14. #   Contact:
  15. #      s.staitsch@gmail.com
  16. #
  17. #   Version 2.1
  18. #      last modified: 24.06.2021 19:07:36
  19. ########################################################################
  20.  
  21.  
  22. #### FANUC-FILE-SPLITTER-FUNCTION ####
  23. fanuc_split(){
  24.     del_crlf(){
  25.          cat $file | tr -d '\r' > .tmp ; rm $file ; mv .tmp $file
  26.     }
  27.  
  28.     loop(){
  29.         sed -i '1d' $file
  30.         while [ "$(cat $file | wc -l)" -gt 1 ]; do
  31.             name=$(egrep -o -m1 '^<{1}\S*>{1}|^O[0-9]{1,}' $file)
  32.             start=$(egrep -no -m1 '^O{1}[0-9]{1,}|^<' $file | egrep -o '^.*:' | tr -d ':' | tail -1)
  33.             end=$(egrep -no -m2 '^O{1}[0-9]{1,}|^<|^%' $file | egrep -o '^.*:' | tr -d ':' | tail -1)
  34.             (( end-- ))
  35.             echo $name
  36.             echo "%" > maschine_$file/$name
  37.             sed -n ''$start','$end'p' $file >> maschine_$file/$name
  38.             echo "%" >> maschine_$file/$name
  39.             sed -E -i 's/\s$//' maschine_$file/$name
  40.             (( end-- ))
  41.             sed -i '1,'$end'd' $file
  42.         done
  43.         for old in `ls maschine_$file/ | egrep '^<' ` ; do
  44.             new=$(echo $old | tr -d '<>')
  45.             mv maschine_$file/$old maschine_$file/$new
  46.         done
  47.         rm $file
  48.     }
  49.     #### START PROCESS ####
  50.     for file in *ALL ; do
  51.         cp $file $file.BAK
  52.         del_crlf
  53.         mkdir maschine_$file/
  54.         loop
  55.     done
  56. }
  57.  
  58. #### ONBOARD-LED ####
  59. led_on(){
  60.     sudo sh -c 'echo 0 > /sys/class/leds/led1/brightness'
  61. }
  62. led_off(){
  63.     sudo sh -c 'echo 1 > /sys/class/leds/led1/brightness'
  64. }
  65. led_blink(){
  66. for i in {1..10}; do
  67.     sleep .1
  68.     led_on
  69.     sleep .1
  70.     led_off
  71. done
  72. }
  73.  
  74. ######## MAIN-PROGRAM ########
  75.  
  76. while true; do
  77.     clear
  78.     echo "Plugin your Device now!"
  79.     echo
  80.     #### ENDLESSLOOP CHECK DEVICE PLUGGED ####
  81.     until [ -n "$(lsblk | egrep -o 'sd.1')" ]; do
  82.         sleep 1
  83.         led_on
  84.         sleep 1
  85.         led_off
  86.         continue
  87.     done
  88.  
  89.  
  90.     #### IF FOUND DEVICE ####
  91.     sdax=$(lsblk | egrep -o 'sd.[0-9]')
  92.     led_blink
  93.     led_on
  94.     if [ ! -e $HOME/fanuc_cnc ]; then
  95.         mkdir $HOME/fanuc_cnc/
  96.         chmod u+rwx $HOME/fanuc_cnc/
  97.     fi
  98.  
  99.  
  100.     #### ERROR - NO CNC-FILES FOUND ####
  101.     adr_mount=$(lsblk | egrep 'sd.[0-9]' | egrep -o '/.*')
  102.     if [ ! -e $adr_mount/*ALL ]; then
  103.         echo "No supported Files found!"
  104.         echo "Please unplug Device and restart Programm"
  105.         until [ -z "$(lsblk | grep -E -o 'sd.[0-9]')" ]; do
  106.             : #NOTHING
  107.         done
  108.         bash
  109.     fi
  110.  
  111.  
  112.     #### MOUNT DEVICE & COPY FILES ####
  113.     echo "Process started"
  114.     echo
  115.     echo "Please wait a moment."
  116.     echo " - LED is ON      - Programm ist working"
  117.     echo " - LED is BLINKING    - Work succes!"
  118.     echo " - LED is OFF     - Program Error and stops"
  119.     sleep 3
  120.     led_on
  121.     sudo mount --rbind $adr_mount /mnt
  122.     sudo cp /mnt/* $HOME/fanuc_cnc/
  123.     sudo rm /mnt/*
  124.     sudo chmod a+rwx $HOME/fanuc_cnc/*
  125.  
  126.  
  127.     #### UMOUNT DEVICE ####
  128.     adr_umount=$(lsblk | egrep 'sd.[0-9]' | egrep -o '/.*')
  129.     sudo umount $adr_umount
  130.     echo "Files copied"
  131.     sleep 1
  132.  
  133.     #### START MAIN-PROCESS ####
  134.     cd $HOME/fanuc_cnc/
  135.     echo "starting File-Splitter"
  136.     sleep 1
  137.     fanuc_split
  138.     echo
  139.     echo "FANUC-File-Splitter succes!"
  140.     echo
  141.     led_blink
  142.     cd $HOME
  143.     clear
  144.  
  145.  
  146.     #### MAIN-PROCESS SUCCES - WAIT FOR UNPLUG ####
  147.     echo "Process finish!"
  148.     echo "Please unplug Device now"
  149.     until [ -z "$(lsblk | egrep -o 'sd.[0-9]')" ]; do
  150.         sleep 1
  151.         led_on
  152.         sleep 1
  153.         led_off
  154.         continue
  155.     done
  156.  
  157.  
  158.     #### DEVICE UNPLUGGED, RESTART ####
  159.     led_blink
  160.     echo
  161.     echo "Device removed"
  162.     sleep .5
  163.     echo "Programm restarts now..."
  164.     sleep 1
  165. done
  166.  
RAW Paste Data