+ Reply to Thread
Results 1 to 4 of 4

Thread: Serial.py error

  1. #1

    Serial.py error

    Hi,

    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.

    br, A

  2. #2
    Senior Member Steve Baer's Avatar
    Join Date
    Apr 2010
    Location
    Seattle
    Posts
    1,464
    Blog Entries
    19
    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.
    http://msdn.microsoft.com/en-us/library/30swa673.aspx
    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
    http://msdn.microsoft.com/en-us/libr...port.open.aspx

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

    br, A

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

    br, A

    EDIT:

    This works, but now the ser.write is proving to be problematic, damn thing wanting to send bytes in stead of a string....

    Python Code:
      import serial
      initialize = 'x'
       
      ser = serial.Serial()
      ser.baudrate = 115200
      ser.port = 1
      print ser
      try:
          ser.open()
          print 'hurray'
      except:
          print 'bugger'
          ser.close()
      try:
          ser.write(b'x\r\n')
      except:
          print 'bugger2'
      ser.close()

    and so the work continues

    EDIT2:

    ahem....Newline might work better with the backslash....

    And it works =)
    Attached Images
    Last edited by aalto; 04-14-2012 at 12:54 PM.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts