Stop simulation immediatly / End script!
-
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:
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:
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 }
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...
-
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 -
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!
# 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?
Advertisement