sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Stop simulation immediatly / End script!

    Scheduled Pinned Locked Moved SketchyPhysics
    3 Posts 2 Posters 7.2k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      Bas9999
      last edited by

      I wanted to implement a better error handler for the script here: http://sketchucation.com/forums/viewtopic.php?f=61%26amp;t=61033

      What i did was to bring up the error on purpose, to let SketchyPhysics open a COM port that does not exist with

      @serial = Serial.new('COM7', 9600)
      

      Gives:
      sketchyphysics COM port - file not found error.png

      Hey look... it sees the COM port as a file...NEAT!, so i looked up some code, where you can check if a file exists:

      File.exist?('file.ext')
      

      Gives Boolean True or False as return

      So, a COM port can also be checked if it exists with:

      File.exist?('COM7')
      

      So i put this line together:

      @serial = Serial.new('COM7', 9600) if File.exist?('COM7')
      

      Now...when runing the simulation and the COM port is not found the simulation just starts and gives the error:
      sketchyphysics COM port - file not found error 02.png

      Ok, so we need to STOP the script immediatly, so i looked up how to stop a script from another example, i came up with this line:

      MSketchyPhysics3;;SketchyPhysicsClient.physicsReset
      

      Now putting it all together and include a nice messagebox, this is the part i have now:

      onstart{
        if File.exist?('COM7')
          @serial = Serial.new('COM7', 9600)
        else
          UI.messagebox('Serial Port not found!')
          MSketchyPhysics3;;SketchyPhysicsClient.physicsReset
        end
      }
      

      But what happens now is, that we get the message the COM port is not found that is ok, but right after that, another error pops up, SketchyPychics still executes the function: ONEND!!

      onend{
        @serial.close
      }
      

      sketchyphysics COM port - file not found error 03.png

      So my question is: How to abort the script/simulation immediatly?

      *EDIT

      I could do this:

      onend{
        @serial.close if File.exist?('COM7')
      }
      

      but's not charming to implement the check everytime...

      1 Reply Last reply Reply Quote 0
      • A Offline
        Anton_S
        last edited by

        Simply check if variable @serial exists before calling @serial.close:
        @serial.close if @serial

        You can do this way:

        
        onStart {
          begin
            @serial = Serial.new('COM7', 9600)
          rescue RubySerial;;Exception => e
            MSketchyPhysics3;;SketchyPhysicsClient.physicsReset
            UI.messagebox("Serial port not found!")
          end
        }
        
        onUpdate {
          # ...
        }
        
        onEnd {
          @serial.close if @serial
        }
        

        Also, here is reference to all script functions:
        SP Script Overview

        1 Reply Last reply Reply Quote 0
        • B Offline
          Bas9999
          last edited by

          Hi Anton_S

          Thank you, so this is the piece of code i have now... wich includes a messagebox to ask the COM port and exit if CANCEL is pressed.

          The code is lumped together with some examples found here and there, so if there are any tweaks let me know!

          sp su15 serial recieve example.png

          # DomoticX Virtual 3D Device Port SU15 interface.
          
          # Wat moet er gebeuren bij het starten van de simulatie?
          onstart{
            options = ["Serial Port;", "Baudrate;"]
            default = ["COM8", "9600"]
            list = ["", "4800|9600|14400|19200|28800|38400|57600|115200"]
            serialsettings = UI.inputbox(options, default, list, "Serial port settings;")
          
            if serialsettings != FALSE
              $comport = serialsettings[0]
              $baudrate = serialsettings[1].to_i
          
              begin
                @serial = Serial.new($comport, $baudrate)
              rescue RubySerial;;Exception => e
                @serial.close if @serial
                MSketchyPhysics3;;SketchyPhysicsClient.physicsReset
                UI.messagebox("Serial port not found!")
              end
            else
              MSketchyPhysics3;;SketchyPhysicsClient.physicsReset
            end
          }
          
          # Wat moet er gebeuren tijdens de simulatie?
          ontick{
            MSketchyPhysics3.closeControlPanel if frame == 1
          
            string_size = 1024
            $serialdata = eval(@serial.read(string_size))
          
            logLine("COM poort; " + $comport.to_s)
            logLine("BAUD rate; " + $baudrate.to_s)
            logLine("Seriele data; " + $serialdata.to_s)
            logLine("")
            logLine("")
            logLine("")
            logLine("")
            logLine("")
          }
          
          # Wat moet er gebeuren als de de simulatie stopt?
          onend{
            # Sluit de seriele poort als deze open staat.
            @serial.close if @serial
          }
          

          Ps. The simulation works fine... but sometimes when i STOP and START the simulation again i get this error, when i start it for the second time it works perfectly again, any ideas to prevent this?

          sp su15 serial recieve error.png

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement