Write to serial port with Ruby [SOLVED!]
-
Nice find, John!
I find it very convincing as it has been kept up to date with latest Ruby version and doesn't require installation of Python. However, it does require Ruby FFI gem, which needs to have some tweaking before it can be used with SU.
Anton
-
Hi Anton_S & driven,
Wow, i really hope you can make this work!, so that we can use the new SU version to communicate with a serial port, we can make lots of cool things with Arduino & Raspberry.
If i need to test something, let me know!
Thanks in forward!
-
Hello Anton_S
I've tested it with Sketchup 2015...
, it works like a charm!
This is the code to read data for the serial port so far...
onstart{ @serial = Serial.new('COM8', 9600) } ontick{ MSketchyPhysics3.closeControlPanel if frame == 1 $serialdata = eval(@serial.read(1024)) logLine("Serial data; " + $serialdata.to_s) logLine("") logLine("") logLine("") logLine("") logLine("") logLine("") } onend{ @serial.close }
Thank you!, i see if i can make a script that SENDS data to the Arduino / Raspberry...
-
I see you tested it with SU2015...this confuses me. Were you using the 64 bit version of sketchup? Which Sketchy Physics download did you use? Thanks.
-
I'm glad it works with SU2014
-
@mptak said:
I see you tested it with SU2015...this confuses me. Were you using the 64 bit version of sketchup? Which Sketchy Physics download did you use? Thanks.
No, i used the 32-bit version of SU2015, sketchyphysics does not work on 64-bit (yet)
-
Hi All,
This is an example how to SEND data to the Arduino with SU 14/15, SketchyPhysics and Ruby Serial
First the Arduino programming, here is the code to lit the integrated LED on digital 13, when there is a serial "1" recieved, the LED goes out when there is a "0" recieved.
Arduino code:
String readString; void setup() { pinMode(13, OUTPUT); Serial.begin(9600); } void loop() { while (Serial.available()) { delay(3); if (Serial.available() >0) { char c = Serial.read(); readString += c; } } if (readString.length() >0) { if (readString == "1") { digitalWrite(13, HIGH); } if (readString == "0") { digitalWrite(13, LOW); } readString = ""; } }
Now...once programmed, create a sketchyphysics solid and paste this code in it:
onstart{ @serial = Serial.new('COM8', 9600) } onclick{ @m = Sketchup.active_model.materials.add "My material" group.material = @m @m.color="Yellow" logLine("LED ON!") @serial.write('1') } onunclick{ @m = Sketchup.active_model.materials.add "My material" group.material = @m @m.color="White" logLine("LED OFF!") @serial.write('0') } ontick{ MSketchyPhysics3.closeControlPanel if frame == 1 } onend{ @serial.close }
How does this work?
When you run the script and click on the solid, the color will change to yellow and over the serial port a "1" will be send, de Arduino will recieve this "1" and will put on the LED light, when you release the mouse button, the model changes color back to white and a "0" will be sent, the Arduino recieves this and will put the LED light out!Have fun!
Ps. Is there any code to read out a textbox or something (userinput possible?), where the user can specify the COMPORT and BAUDRATE?
I think i will start to create the 204/2015 walktrough soon!, this will be the banner for the
projects:
SU_14_15 - Ruby Serial SEND Example with Sketchup Sketchyphysics.skp
-
Hi Anton_S
*EDIT SOLVED!!! SEE BELOW
Could you assist on this one?
When for example i use a diffrent baudrate (on purpose) i still get this error:
The original code was:
ontick{ MSketchyPhysics3.closeControlPanel if frame == 1 string_size = 1024 $serialdata = eval(@serial.read(string_size)) }
I changed it to this, but that doesn't work:
ontick{ MSketchyPhysics3.closeControlPanel if frame == 1 string_size = 1024 begin $serialdata = eval(@serial.read(string_size)) rescue RubySerial;;Exception => e @serial.close if @serial MSketchyPhysics3;;SketchyPhysicsClient.physicsReset UI.messagebox("ERROR!") end }
How to catch this error?
*EDIT SOLVED!!!
I just changed the line to:$serialdata = @serial.read(string_size)
so it's only:
ontick{ MSketchyPhysics3.closeControlPanel if frame == 1 $serialdata = eval(@serial.read(1024)) }
-
Hello
fine !
but the link above is dead
https://googledrive.com/host/0B3qg8f4WrNdHdHVyLVFuaHRjOTA/ffi%20+%20rubyserial.zip
help and thanks
-
Hello Bas9999,
I managed to tweak ffi and rubyserial (suggested by driven) into the plugins for SU. Download
https://googledrive.com/host/0B3qg8f4WrNdHdHVyLVFuaHRjOTA/ffi + rubyserial.zip
ffi + rubyserial.zip and extract into the plugins folder. It should work on Windows with SU2014 and SU2015. The advantage of it is that it doesn't require Python to run.Here is RubySerial usage Wiki: https://github.com/hybridgroup/rubyserial
This is how I think your code should look like:onstart { @serial = Serial.new('COM8', 9600) } ontick { string_size = 1024 $serialdata = eval( @serial.read(string_size) ) } onend { @serial.close }
Note: If particular serial port does not exist, an error will be raised causing simulation to reset.
If you want ffi and rubyserial to work with SU2013 and prior, you should upgrade SU ruby msvcrt to 1.8.6 or later. Do this by going to SketchUp Program Files, to the path of SketchUp.exe, and replace msvcrt-ruby18.dll with the new one, which can be downloaded [url=https://googledrive.com/host/0B3qg8f4WrNdHdHVyLVFuaHRjOTA/msvcrt-ruby18.zip:196vcuuf]here[/url:196vcuuf].
Tell me how it goes as I don't think I have any serial ports to test myself.
Anton
-
That's strange. For some reason SketchyUcation can't parse the url properly. Try using this unparsed link instead: https://googledrive.com/host/0B3qg8f4WrNdHdHVyLVFuaHRjOTA/ffi + rubyserial.zip
-
Thanks
the download is OK
but other pb : i want to test the bas9999 example but
it's SU2015 version.
On my Vista dsktop only SU2014 supported.
Can everybody downgrade bas9999 sketchup model file from SU2015 to SU2014
More thanks
NB : maybe the serial port work with ruby without SU-Physics ?
if yes , how to ?
-
Here's a v2014 version SU_14_15 - Ruby Serial SEND Example with Sketchup Sketchyphysics[v2014].skp
-
@picpic020960 said:
NB : maybe the serial port work with ruby without SU-Physics ?
if yes , how to ?Yes, Ruby Serial works without SketchyPhysics.
It's easy to use:
# Require the library require 'rubyserial' # Instantiate serial my_serial = Serial.new(address, baude_rate = 9600, data_bits = 8) # Get data and do whatever you want with it size = 1024 data = my_serial.read(size) # Set data data = "" my_serial.write(data) # Close serial once you're done using it my_serial.close my_serial = nil
See this link for more info: https://github.com/hybridgroup/rubyserial
-
@anton_s said:
Yes, Ruby Serial works without SketchyPhysics.
I installed the 'rubyserial' gem, but the initialization of Serial.new("COM3")
crashes for me in SU2019 Win 10.rubyserial-0.6.0.gem
ffi-1.11.3-x64-mingw32.gemI will try to figure out why it does that.
Does anyone have had a success with 'rubyserial' in SU2019? -
I have found the answer. It is ffi gem crashing in Win10 in SU2017...18...19
https://forums.sketchup.com/t/sketchup-2017-crashes-when-ffi-is-loaded-in-windows-10/40372
Advertisement