Message 1 of 5
Serial module not supported by AddIns on Python?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I created an add-on module to receive posture angles from serial devices (Arduino+MPU6050), but when I added serial-related code to the project, there was no response when I executed the add-on module.
I don't know where to see the error message.
Is serial not supported by add-on modules?
My AddIns code is as follows:
# Assuming you have not changed the general structure of the template no modification is needed in this file.
import adsk.core
import adsk.fusion
import adsk.cam
import traceback
import threading
import random
import json
import serial
handlers = []
class ThreadEventHandler(adsk.core.CustomEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# Make sure a command isn't running before changes are made.
if ui.activeCommand != 'SelectCommand':
ui.commandDefinitions.itemById('SelectCommand').execute()
# Make sure a command isn't running before changes are made.
eventArgs = json.loads(args.additionalInfo)
newEye = adsk.core.Point3D.create(float(eventArgs['Roll']), float(eventArgs['Pitch']), 1)
# newEye = adsk.core.Point3D.create(float(eventArgs['Value']), 0, 1)
camera.eye = newEye
app.activeViewport.camera = camera
# app.activeViewport.fit()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class CaptureCamThread(threading.Thread):
def __init__(self, event):
threading.Thread.__init__(self)
self.stopped = event
def run(self):
try:
ser = serial.Serial('COM4', 115200, timeout=1)
while not self.stopped.wait(2):
# while(True):
read = ser.readline()
read = read.decode()
if read != '':
RollPitch = read.split()
# print(type(RollPitch))
Roll = float(RollPitch[0])
Pitch = float(RollPitch[1])
args = {'Roll': Roll, 'Pitch': Pitch}
app.fireCustomEvent('CaptureCamThreadID', json.dumps(args))
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
global app
global ui
global view
global camera
global eye
try:
# This will run the start function in each of your commands as defined in commands/__init__.py
app = adsk.core.Application.get()
ui = app.userInterface
view = app.activeViewport
camera = view.camera
eye = camera.eye
global customEvent
customEvent = app.registerCustomEvent('CaptureCamThreadID')
onThreadEvent = ThreadEventHandler()
customEvent.add(onThreadEvent)
handlers.append(onThreadEvent)
global stopFlag
stopFlag = threading.Event()
myCaptureCamThread = CaptureCamThread(stopFlag)
myCaptureCamThread.start()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
try:
if handlers.count:
customEvent.remove(handlers[0])
stopFlag.set()
app.unregisterCustomEvent('CaptureCamThreadID')
ui.messageBox('Stop addin')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
This is a script, Like Addins, I encountered the same problem:
# Author-vgegok
# Description-
import adsk.core
import adsk.fusion
import adsk.cam
import traceback
import serial
import time
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
view = app.activeViewport
camera = view.camera
eye = camera.eye
for i in range(0, 100):
ser = serial.Serial('COM4', 115200, timeout=1)
# while(True):
time.delay(2)
read = ser.readline()
read = read.decode()
if read != '':
RollPitch = read.split()
# print(type(RollPitch))
Roll = float(RollPitch[0])
Pitch = float(RollPitch[1])
newEye = adsk.core.Point3D.create(Roll, Pitch, 1)
camera.eye = newEye
app.activeViewport.camera = camera
# app.activeViewport.fit()
ui.messageBox(str(eye.x))
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Some examples can be referred?