阿杜伊诺玛雅Python

2024-05-19 10:28:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我和我的朋友正试图在Maya中实现一个Arduinoboard控制器。我们不是自己写代码的,也找不到编写代码的人。我们不是程序员,但我们知道如何阅读和编辑Python代码。我们的问题可能真的很简单,但我们不知道如何解决它。我们在谷歌上搜索过它,并在多个论坛上阅读过,但没有任何运气。基本上,我们没有足够的知识来了解其他人的问题/解决方案如何适用于我们的特定代码。你知道吗

错误消息:

Error message can be seen here

代码如下:

import socket
import serial
import time

MAYA_ADDR = ('127.0.0.1', 1923)
ARDUINO_PORT = 'COM3'

if __name__ == '__main__':
    print('Welcome to the arduino-maya serial driver.')
    print('Press CTRL-C to exit.')
    print('')
    print('[INFO] Maya address: {0}:{1}'.format(*MAYA_ADDR))
    print('[INFO] Arduino port: {0}'.format(ARDUINO_PORT))

mayaconn = None
while mayaconn is None:
    try:
        mayaconn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        mayaconn.connect(MAYA_ADDR)
    except:
        mayaconn = None
        print('[ERROR] Maya connection refused.')
        print('[ERROR] (Maybe you didn\'t load the plugin in Maya?)')
        print('[INFO] Trying again in 5 seconds...')
        time.sleep(5)

print('[INFO] Maya connection established.')

arduinoconn = serial.Serial(ARDUINO_PORT)
print('[INFO] Arduino connection established.')

oldValues = [-1]*3
print("Hellu")
while True:
    values = arduinoconn.readline().split()
    for i, v in enumerate(values):
        vInt = 0
        try:
            vInt = int(v)
        except:
            pass
        else:
            if oldValues[i] != vInt:
                cmd = 'arduinoUpdateChannel ' + str(i) + ' ' + v
                mayaconn.send(cmd)
                time.sleep(.002)
            oldValues[i] = vInt
arduinoconn.close()
print('[INFO] Arduino connection closed.')

mayaconn.close()
print('[INFO] Maya connection closed.')

Tags: 代码importinfotimeportserialsocketconnection
1条回答
网友
1楼 · 发布于 2024-05-19 10:28:51

似乎您的程序以前在Python2上运行得很好,现在您使用的是Python3。您可以使用Python2,也可以在Python3中修复此错误,您应该将相应的行更改为

cmd = 'arduinoUpdateChannel ' + str(i) + ' ' + str(v)

(将字节转换为字符串)

相关问题 更多 >

    热门问题