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

    Write to serial port with Ruby [SOLVED!]

    Scheduled Pinned Locked Moved SketchyPhysics
    19 Posts 7 Posters 20.4k Views 7 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.
    • A Offline
      Anton_S
      last edited by

      I'm glad it works with SU2014 😉

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

        @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

          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

            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

              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

                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

                  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

                    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

                      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

                        @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

                          @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

                            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