【问题标题】:serial communication Blender Game Engine to arduino?串行通信搅拌机游戏引擎到arduino?
【发布时间】:2013-06-27 09:31:05
【问题描述】:

我需要帮助将串行数据从搅拌机游戏引擎传送到 arduino

我正在从搅拌机进行键盘输入并尝试与 arduino 通信,但它不起作用。

这是搅拌机代码

import serial

ser = serial.Serial("COM6", 9600)
x=ser.write(1)   
print(x)
ser.close()

逻辑

Key "a" --> Python 脚本

这是我尝试从搅拌机进行通信的 arduino 代码。

int led = 2;

void setup() {
    Serial.begin(9600);
    pinMode(led, OUTPUT);
}

void loop() {
    if ( Serial.available())
    {
        char ch = Serial.read();
        if(ch >= '0' && ch <= '9')
        {
        digitalWrite(led, HIGH);
        }
    }
}

实际上,当 Blender Game Engine (BGE) 运行时,我按下 Key 'a' blender 与 arduino 通信并且 LED 亮起。

我做错了吗?

谁能帮我解决这个问题?

【问题讨论】:

    标签: serial-port arduino communication blender


    【解决方案1】:

    在您的搅拌机 Python 代码中,您发送的是一个整数:

    x=ser.write(1)   
    

    而在您的 arduino 代码中,您正在检查 '0''9' 之间的 ASCII 数字,即 48 和 57 之间的数字

    if(ch >= '0' && ch <= '9')
    

    尝试将您的python代码更改为ser.write('1')或将您的arduino代码更改为ch &gt;= 0 &amp;&amp; ch &lt;= 9,它应该可以工作。

    此外,在将您的代码绑定为 Blender 中的脚本之前,您应该首先在 Blender 之外测试您的 Python 脚本。只需使用命令行运行:python script.py,在脚本所在的目录中。

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 2019-10-19
      • 2015-05-13
      • 2019-12-22
      • 2015-07-05
      • 2015-01-26
      • 2015-07-02
      • 2015-04-23
      • 2011-10-21
      相关资源
      最近更新 更多