• Login
sketchucation logo sketchucation
  • Login
🤑 30% Off | Artisan 2 on sale until April 30th Buy Now

Write to serial port with Ruby [SOLVED!]

Scheduled Pinned Locked Moved SketchyPhysics
19 Posts 7 Posters 20.4k Views
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 Bas9999 13 Feb 2015, 16:30

    Hi all,

    I've made some projects to control 3D objects with an Arduino with connected hardware, like potentiometers and gyroscopes (making mini games 😉 ), just trough the serial interface.

    Therefore i am using:

    • SketchUp 8
    • Sketchup 8 plugin - SketchyPhysics v3.5.6
    • Sketchup 8 plugin - Supy v1.6 for Python v2.5
    • Python 2.5
    • Python 2.5 library – PySerial v2.5

    But... does SuPy v1.6 work in SU2014 / SU2015?
    I know there are Ruby changes with the 2.x version that break scripts, but this was from SU2014 to SU 2015 right?

    In SketchUp 8 you just could do:

    onstart{
      Py.exec("import serial")
      Py.exec("ser = serial.Serial('COM8', 9600)")
    }
     
    ontick{
      Py.exec("serialdata = ser.readline()")
      $serialdata = Py.eval("serialdata")
    }
     
    onend{
      Py.exec("ser.close()")
    }
    

    And in the controller box you could put:

    $serialdata.to_f
    

    So...i was wondering if there is any Ruby GEM needed to make serial things happen?, and wat would be a sample code?

    Please let me know!

    1 Reply Last reply Reply Quote 0
    • A Offline
      Anton_S
      last edited by 1 Mar 2015, 12:41

      I found source code for SuPy and I will see if it could be compiled under 2.0.0. If compiling goes with success, I'll post SuPy compatible with SU2014 and SU2015.

      Anton

      1 Reply Last reply Reply Quote 0
      • D Offline
        driven
        last edited by 1 Mar 2015, 13:34

        have either of you had a look at RubySerial
        http://artoo.io/blog/2014/07/18/introducing-rubyserial/#.VPMWVUKUmDA
        it's on my 'long' list but I haven't chased it up yet...
        john

        learn from the mistakes of others, you may not live long enough to make them all yourself...

        1 Reply Last reply Reply Quote 0
        • A Offline
          Anton_S
          last edited by 1 Mar 2015, 20:45

          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

          1 Reply Last reply Reply Quote 0
          • B Offline
            Bas9999
            last edited by 3 Mar 2015, 20:16

            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!

            1 Reply Last reply Reply Quote 0
            • B Offline
              Bas9999
              last edited by 19 Mar 2015, 20:11

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

              1 Reply Last reply Reply Quote 0
              • M Offline
                mptak
                last edited by 19 Mar 2015, 23:14

                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.

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Anton_S
                  last edited by 20 Mar 2015, 00:06

                  I'm glad it works with SU2014 😉

                  1 Reply Last reply Reply Quote 0
                  • B Offline
                    Bas9999
                    last edited by 20 Mar 2015, 07:28

                    @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)

                    1 Reply Last reply Reply Quote 0
                    • B Offline
                      Bas9999
                      last edited by 20 Mar 2015, 07:48

                      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?

                      SU_14_15_Ruby Serial Sketchup Sketchyphysics screen.jpg

                      I think i will start to create the 204/2015 walktrough soon!, this will be the banner for the
                      projects:
                      SketchyPhysics serial banner.jpg


                      SU_14_15 - Ruby Serial SEND Example with Sketchup Sketchyphysics.skp

                      1 Reply Last reply Reply Quote 0
                      • B Offline
                        Bas9999
                        last edited by 28 Mar 2015, 14:07

                        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:
                        sp su15 serial recieve error baudrate.png

                        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))
                        }
                        
                        1 Reply Last reply Reply Quote 0
                        • P Offline
                          picpic020960
                          last edited by 22 May 2015, 07:40

                          Hello

                          fine !

                          but the link above is dead

                          https://googledrive.com/host/0B3qg8f4WrNdHdHVyLVFuaHRjOTA/ffi%20+%20rubyserial.zip

                          help and thanks

                          1 Reply Last reply Reply Quote 0
                          • A Offline
                            Anton_S
                            last edited by 22 May 2015, 22:40

                            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

                            1 Reply Last reply Reply Quote 0
                            • A Offline
                              Anton_S
                              last edited by 22 May 2015, 22:42

                              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

                              1 Reply Last reply Reply Quote 0
                              • P Offline
                                picpic020960
                                last edited by 24 May 2015, 17:49

                                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 ?
                                
                                1 Reply Last reply Reply Quote 0
                                • TIGT Offline
                                  TIG Moderator
                                  last edited by 24 May 2015, 17:57

                                  Here's a v2014 version SU_14_15 - Ruby Serial SEND Example with Sketchup Sketchyphysics[v2014].skp

                                  TIG

                                  1 Reply Last reply Reply Quote 0
                                  • A Offline
                                    Anton_S
                                    last edited by 24 May 2015, 18:18

                                    @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

                                    1 Reply Last reply Reply Quote 0
                                    • T Offline
                                      tomasz
                                      last edited by 2 Jan 2020, 17:59

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

                                      I will try to figure out why it does that.
                                      Does anyone have had a success with 'rubyserial' in SU2019?

                                      Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                                      1 Reply Last reply Reply Quote 0
                                      • T Offline
                                        tomasz
                                        last edited by 2 Jan 2020, 18:42

                                        I have found the answer. It is ffi gem crashing in Win10 in SU2017...18...19

                                        Link Preview Image
                                        SketchUp crashes when FFI is loaded in Windows 10

                                        Hi, I’m trying to load the FFI gem version (ffi-1.9.17-x64-mingw32) in Windows 10, Sketchup 2017 (maintenance build - 17.2.2555). Edit note: In my old post, I had only loaded the ffi_c.so file corresponding to ruby ver…

                                        favicon

                                        SketchUp Community (forums.sketchup.com)

                                        Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                                        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