I am trying to send command to a serial port (COM70) to control an external device. I am doing this trought the python component in grasshopper, using the pySerial module (http://pyserial.sourceforge.net/) which I can load and that supports IronPython and Mono. However, when I am trying to open a serial port (serialInstance.open()) I get a unauthorizedAccessException. This seems to be a .NET problem and I am at a loss at how to begin solving this. Any help is greatly appreciated. The COM port is a virtual com port from a software called Com0Com.
PS. if someone knows another way of easily sending strings to a com port from python (not Firefly or another GH component) then I would be delighted to hear about it.
UnauthorizedAccessExecption is a .NET exception, but it is typically an indicator that something else is going wrong. I'm not familiar with Com0Com; does it have a test application to make sure it is actually working.
It looks like pyserial should work just fine. If you are looking for alternatives, you could always try the SerialPort class in .NET in your python script.
Under the hood, this may very well be what pyserial is calling. I noticed in the Open function for this class, it has a descriptiong of what could possibly cause an UnauthorizedAccessException
Thanks Steve, this helps.
The Com0Com works, I can use the firefly component in GH to send commands to the serialport and everything works, but this has of course its limitations.
Ok, it seems I figured something out. The Garbage Collection seems to be off. The first time I open the port, everything is peachy (line 8). However, if I dont use ser.close() to close the port, the port will remain open, blocking future port connections. In the attached PNG, the Serial ID will jump one every time I run a test.
Also, if I do this lets say a few times, it will crash Rhino and when I start again, I have to disconnect the device I am forwarding to, if I dont Grasshopper will not start again without a reboot (hangs in the load screen, when trying to load the HAL robotic control plugin for GH, even though AFAIK this has no Serial port systems whatsoever.
I will try catching the exceptions and see if I can work around this, but I thought you would like to know.
This works, but now the ser.write is proving to be problematic, damn thing wanting to send bytes in stead of a string....
initialize = 'x'
ser = serial.Serial()
ser.baudrate = 115200
ser.port = 1
and so the work continues
ahem....Newline might work better with the backslash....
And it works =)
Last edited by aalto; 04-14-2012 at 12:54 PM.
Tags for this Thread