algorithmic modeling for Rhino
Hi Folks,
I'm trying to program a serial protocol using the ghpython script component inside grasshopper. i am relatively new to python scripting in the context of grasshopper, so i have a few questions. but first allow me to explain what i'm trying to do: i have a serial device i want to talk to, but i have to do it using some sort of handshaking. for instance, when i send a command/data, i need to wait for an appropriate response before sending another. i have used andy payne's general serial components from firefly, but i don't think they'll work for what i want to do, and in general, i want to know how to do this from scratch. i'm using the pyserial library to do the comm, and i can get it to work within one script. here's an example of a working (mostly) port open/close script (x=input param for baud, y=input param for port name, z=boolean input param for open/close):
import serial
myPort=serial.Serial()
myPort.baudrate = x
myPort.port = y
if z == True:
try:
myPort.open()
except:
print "Something went wrong. Cannot open port."
if myPort.isOpen() == True:
print myPort.name + " is open"
if z != True:
try:
myPort.close()
except:
print "Something went wrong. Cannot close port."
if myPort.isOpen != True:
print myPort.name + " is closed"
this all works well and good. here are my questions:
1) I can open the port and then close it. however, if i try to re-open it, i get an access denied error. it seems rhino is holding the port open, as i have to re-start rhino to get it working again. i read through the discussions and didn't see any definitive answers to this problem. any advice?
2) I'd like to share this port with other components (or at least break up the functions of opening/closing the port and read/write, not unlike how the firefly components are organized), but i have no idea how to share an object instance between components. i did see that there is a sticky dict and tried to add myPort to it, but i kept getting errors in the other component when i try to use the object's methods. for instance:
Component 1 Script:
import serial
import scriptcontext
myPort=serial.Serial("COM4", 9600)
scriptcontext.sticky['myPort']=myPort
Component 2 Script:
import serial
import scriptcontext
myPort=scriptcontext.sticky['myPort']
print myPort.read()
but i get messages like:
Runtime error (MissingMemberException): 'Serial' object has no attribute '_port_handle'
any assistance would be greatly appreciated!!
best,
~BB~
Tags:
*Correction*
It appears that the port is successfully used by another component by using the sticky dict method. The reason for the errors was the locked-up port which is still an issue. In any case, is this the only way to pass global variables or objects to other components besides using patch cords?
Hi Brett
Are "Patch cords" Grasshopper definition connections? If so, yes they are a way.
Also any external module variable that contains a collection would be a way, of which the sticky dictionary is an implementation. Also an external class field would be a way.
Do you feel you need more ways?
Could you please post the file when asking for help?
- Giulio
_______________
giulio@mcneel.com
ha, yes, patch cords=wires/definition connections! sorry, that's max/msp talk. and no, that's plenty of ways. i was just curious whether or not there's a preferred way to share objects. the dict method is working for now, so i'll stick with it.
in any case, i think i solved most of my issues:
1) sharing the port between multiple components is not a problem using the scriptcontext.sticky dictionary.
2) splitting the management of the port among several components gets rid of the IOErrors. One component creates the serial port from a port name and baudrate (this is shared in the dict), another opens and closes the port and still another reads and writes.
still fussing with it...
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by