Arduino从Python读取串口而不打开串口监视器

时间:2014-11-18 02:27:42

标签: java python arduino minecraft

我正在为MineCraft编写一个mod,我有一个Python脚本将健康值传递给Arduino Uno。打印值显示正面结果。但是,Arduino行为不正常,以免串行监视器打开,即使它显然接收到数据。我想知道这个利基是否有任何工作?

这是Python:

import serial   #to open Serial port connected to Arduino
import sys  #to send arguments to Arduino

ser = serial.Serial('/dev/ttyACM0', 9600)   #declares specific Serial port at 9600 baud rate
ser.write(str(sys.argv[1]))         #sends arguments to Arduino via Serial

发送给Arduino并阅读:

/*
    reads health from Serial input
    returns health as String
*/
String getHealth()
{
  String readHealth;

  readHealth = "";  //start empty String

  while(Serial.available())
  {
    delay(3);   //wait three milliseconds

    if(Serial.available() > 0)  //if there is Serial to read
    {
      char c = Serial.read();   //read the character
      readHealth += c;  //add the character to the String
    }
  }

  return readHealth;
}

/*
    takes in health read
    powers appropriate number of LEDs
    returns void
*/
void toLight(String readHealth)
{
  int health = readHealth.toInt();  //convert readHealth to integer

  for(int thisRed = 0; thisRed < ELEMENTS; thisRed++)   //for every LED
  {
    if(thisRed < health)    //turn on the LED if health is greater than index
    {
      digitalWrite(red[thisRed], HIGH);
    }
    else    //turn off the LED if health is less than or equal to index
    {
      digitalWrite(red[thisRed], LOW);
    }
  }
  return;
}

我让Java执行Python代码并将Health作为参数传递。 Python继续将它传递给Arduino。 Arduino成功读取值,将它们转换为int,并点亮适当数量的LED,但仅限于串行监视器打开时。串行监视器是否有任何方法可以持续打开?

0 个答案:

没有答案