Write to serial port with Ruby [SOLVED!]

Moderator: CPhillips

Write to serial port with Ruby [SOLVED!]

Postby Bas9999 » Fri Feb 13, 2015 5:30 pm

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:
Code: Select all
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:
Code: Select all
$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!
0
Last edited by Bas9999 on Sat Mar 28, 2015 10:12 am, edited 1 time in total.

Bas9999 
 

Re: Write to serial port with Ruby

Postby Anton_S » Sun Mar 01, 2015 1:41 pm

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
0

Anton_S 
PluginStore Author
PluginStore Author
 

Re: Write to serial port with Ruby

Postby driven » Sun Mar 01, 2015 2:34 pm

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
0
learn from the mistakes of others, you may not live long enough to make them all yourself...

driven 
PluginStore Author
PluginStore Author
 

Re: Write to serial port with Ruby

Postby Anton_S » Sun Mar 01, 2015 9:45 pm

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
0

Anton_S 
PluginStore Author
PluginStore Author
 

Re: Write to serial port with Ruby

Postby Bas9999 » Tue Mar 03, 2015 9:16 pm

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!
0

Bas9999 
 

Re: Write to serial port with Ruby

Postby Anton_S » Fri Mar 13, 2015 2:21 am

Hello Bas9999,

I managed to tweak ffi and rubyserial (suggested by driven) into the plugins for SU. Download
https://googledrive.com/host/0B3qg8f4WrNdHdHVyLVFuaHRjOTA/ffi%20+%20rubyserial.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:
Code: Select all
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 here.

Tell me how it goes as I don't think I have any serial ports to test myself.

Anton
0
Last edited by Anton_S on Fri May 22, 2015 11:40 pm, edited 3 times in total.

Anton_S 
PluginStore Author
PluginStore Author
 

Re: Write to serial port with Ruby

Postby Bas9999 » Thu Mar 19, 2015 9:11 pm

Hello Anton_S

I've tested it with Sketchup 2015... :ecstatic: , it works like a charm!

This is the code to read data for the serial port so far...

Code: Select all
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...
0

Bas9999 
 

Re: Write to serial port with Ruby

Postby mptak » Fri Mar 20, 2015 12:14 am

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

mptak 
 

Re: Write to serial port with Ruby

Postby Anton_S » Fri Mar 20, 2015 1:06 am

I'm glad it works with SU2014 ;)
1

Anton_S 
PluginStore Author
PluginStore Author
 

Re: Write to serial port with Ruby

Postby Bas9999 » Fri Mar 20, 2015 8:28 am

mptak wrote: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)
0

Bas9999 
 

Re: Write to serial port with Ruby

Postby Bas9999 » Fri Mar 20, 2015 8:48 am

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:
Code: Select all
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:

Code: Select all
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
1

Bas9999 
 

Re: Write to serial port with Ruby [SOLVED!]

Postby Bas9999 » Sat Mar 28, 2015 3:07 pm

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:

Code: Select all
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:

Code: Select all
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:
Code: Select all
$serialdata = @serial.read(string_size)


so it's only:

Code: Select all
ontick{
  MSketchyPhysics3.closeControlPanel if frame == 1
  $serialdata = eval(@serial.read(1024))
}
1

Bas9999 
 

Re: Write to serial port with Ruby [SOLVED!]

Postby picpic020960 » Fri May 22, 2015 8:40 am

Hello

fine !

but the link above is dead

https://googledrive.com/host/0B3qg8f4Wr ... serial.zip

help and thanks
0

picpic020960 
 

Re: Write to serial port with Ruby [SOLVED!]

Postby Anton_S » Fri May 22, 2015 11:42 pm

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%20+%20rubyserial.zip
0

Anton_S 
PluginStore Author
PluginStore Author
 

Re: Write to serial port with Ruby [SOLVED!]

Postby picpic020960 » Sun May 24, 2015 6:49 pm

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 ?
0

picpic020960 
 

Re: Write to serial port with Ruby [SOLVED!]

Postby TIG » Sun May 24, 2015 6:57 pm

Here's a v2014 version
SU_14_15 - Ruby Serial SEND Example with Sketchup Sketchyphysics[v2014].skp
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Write to serial port with Ruby [SOLVED!]

Postby Anton_S » Sun May 24, 2015 7:18 pm

picpic020960 wrote: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:
Code: Select all
# 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

Anton_S 
PluginStore Author
PluginStore Author
 

Re: Write to serial port with Ruby [SOLVED!]

Postby tomasz » Thu Jan 02, 2020 6:59 pm

Anton_S wrote: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?
0

tomasz 
SU2TH & SU2KT Developer
 

Re: Write to serial port with Ruby [SOLVED!]

Postby tomasz » Thu Jan 02, 2020 7:42 pm

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
0

tomasz 
SU2TH & SU2KT Developer
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to SketchyPhysics

Who is online

Users browsing this forum: No registered users and 3 guests

Visit our sponsors: