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