Python 如何将值从blender发送到arduino?

Python 如何将值从blender发送到arduino?,python,serial-port,Python,Serial Port,我是blender和python的新手。我想从arduino接收到blender 3d的模拟值。为此,我使用了此代码。我在上找到了此代码 根据他的代码,Blender软件发送字符并告诉arduino发送值。但当我尝试在Blender中运行代码时,它会打印错误“无法写入COM3!”。连接到COM3端口并没有问题。我使用的是Arduino mega 2560 由于上述问题,arduino无法将值发送到搅拌机。我哪里出错了 port = 'COM3' pin = 0 try: Game

我是blender和python的新手。我想从arduino接收到blender 3d的模拟值。为此,我使用了此代码。我在上找到了此代码

根据他的代码,Blender软件发送字符并告诉arduino发送值。但当我尝试在Blender中运行代码时,它会打印错误“无法写入COM3!”。连接到COM3端口并没有问题。我使用的是Arduino mega 2560

由于上述问题,arduino无法将值发送到搅拌机。我哪里出错了

port = 'COM3' 
pin = 0 

try: 
    GameLogic.SerialPort 
except: 
    try: 
        GameLogic.SerialPort = serial.Serial(port,9600,timeout=1)  
        print 'Port ' + port + 'found!' 

        #ok, we have a port. Let's test if we can write to it 
        try: 
            GameLogic.SerialPort.write('x') 
            GameLogic.serialRead = False 
            GameLogic.serialOk = True 

        except: 
            GameLogic.serialOk = False 
            print 'could not write to port ' + port + '!' 


    except: 
        GameLogic.serialOk = False 
        print 'no port ' + port + ' found!' 



# do stuff if serial port is OK 
if GameLogic.serialOk: 
    if GameLogic.serialRead: 
        valo = GameLogic.SerialPort.readline() 
        val = float(valo)/100 
        GameLogic.serialRead = False     
    else: 
        GameLogic.SerialPort.write('a') 
        GameLogic.serialRead = True     


    # BLENDER stuff 
    if GameLogic.serialRead == False: 
        objs = GameLogic.getCurrentScene().getObjectList() 
        cube = objs["OBbox"] 
        cube.setPosition([1.0,1.0,val])


Arduino code


int analogPin=0;
int analogVal=0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if ( Serial.available()) {
    char ch = Serial.read();
    if ('a'==ch) {
      analogVal = analogRead(analogPin);
      Serial.println(analogVal);
    }
  }
}

我将arduino连接到“COM3”并安装“py-serial”,我尝试从arduino发送数据,但不使用请求/响应方法,它工作正常。但它总是发送数据。为了避免这种情况,我只包括py-serial。我需要安装任何其他软件吗?没有人知道你做错了什么,因为你接受了所有异常。