处理 - Serial.Write()发送不需要的字符'98'

时间:2014-07-11 19:55:48

标签: serial-port arduino processing

我通过使用串行USB的处理草图控制arduino Uno。我在处理界面中有一个按钮,它运行precoat()方法,该方法调用其他一些方法并运行一些serial.write()命令。我认为所有相关的代码都在下面。所有阀门最初都设置为假。当我调用预涂层方法时,它应该在setPrecoat()中写入58,然后在setPumpSpeed()中写入9170。也许arduino看到一串字节589170,但我可以处理好。问题是,由于某种原因,它实际上写了58 98 9170.我不能为我的生活找出98来自哪里。如果我分别调用setPrecoat()方法和setPumpSpeed()方法,它们分别编写58和9170。如果我将pumpSet从150调整到其他位置,98仍会插入,9170会相应更改。有没有想过为什么写98?我能想到的唯一一件事就是49 * 2是98和49是我加入i以将它从int 0更改为char 1但是我无法看到49将如何制作它到串行输出。特别是考虑到myPort.write(98)实际上会发送一个' b'不是' 98'。

void precoat()
{
  autoCount = 0;
  timer = 0;
  setDataLogOn();  //no writing in this method so I didn't include it
  setPrecoat();
  pumpSet = 150;
  setPumpSpeed(pumpSet);
}

void setPrecoat()          
{
  //sets all modes false and then sets precoat mode to true.  Modes are used elsewhere.
  precoatMode = bodyFeedMode = waterRinseMode = airRinseMode = bodyFeedFillMode = precoatFillMode = precoatRinseMode = cleanMode = false;
  precoatMode = true;
  valvesRed();   //draws some red boxes
  stroke(BGPenR, BGPenG, BGPenB);                  //outline color for boxes matches BG
  fill(dataLogGreenR, dataLogGreenG, dataLogGreenB);  //sets color to green
  rect(0*27+370, 10, 22, 22);                             //draws one green box
  valveText();  //prints text on the boxes just drawn
  for (int i = 0;i<8;i++)       //valve state is a Boolean array which stores the state of each valve
  {
    if (i == 4 || i == 7)   //checks 5th and 8th array element.  If false, writes a 5 or 8 which sets them to true via the arduino
    {
      if (valveState[i]==false)
      {
        myPort.write(i+'1');     //edited for clarity i+'1' is more straight forward than i+49
      }
    }
    else
    {
      if (valveState[i] == true)  //checks other array element.  If true, writes their corresponding number which sets them to false via the arduino
      {
        myPort.write(i+'1');  //edited for clarity i+'1' is more straight forward than i+49

      }
    }
  }
}

void setPumpSpeed(int pump)  //sets the pump speed.  Writing 9 triggers a method on the arduino which accepts the next three chars as the input for a PWM pin.
{
  pumpRed();    //sets all pump boxes red
  fill(dataLogGreenR, dataLogGreenG, dataLogGreenB);
  stroke(BGPenR, BGPenG, BGPenB); 
  if (pump == 30)
  {
    //myPort.write("9031");
    myPort.write('9');
    myPort.write('0');
    myPort.write('3');
    myPort.write('1');
    rect((boxes)*27+370, 10, 35, 22);   //pump 30 box set to green
  } 
  else if (pump == 124)
  {
    //myPort.write("9136");
    myPort.write('9');
    myPort.write('1');
    myPort.write('3');
    myPort.write('6');    
    rect((boxes)*27+410, 10, 35, 22);    //pump 124 box set to green
  }
  else if (pump == 150)
  {
    //myPort.write("9170");
    myPort.write('9');
    myPort.write('1');
    myPort.write('7');
    myPort.write('0');    
    rect((boxes)*27+450, 10, 35, 22);  //pump 150 box set to green
  }
  else 
  {
    myPort.write("9000");
    //myPort.write('9');
    //myPort.write('0');
    //myPort.write('0');
    //myPort.write('0');    
    rect((boxes)*27+490, 10, 35, 22);  //pump 0 box set to green
  }
}

编辑:已实施i +&#39; 1&#39;而不是代码中的i + 49。问题仍然存在,但看起来更清晰。

0 个答案:

没有答案
相关问题