第一次运行后Arduino串行通信失败

时间:2014-02-25 23:42:37

标签: c++ vb.net visual-studio serial-port arduino

我目前正在创建一个可视化的基本程序,通过串口通信来控制我的Arduino,问题是我将第一个字符串从VB发送到arduino后,if (Serial.available() )下次我发送字符串时不再有效...

这是从VB发送到Arduino的字符串的示例: 5,10,0,0,0050,0100,00,1,0,0050,0100,01,2,0,0050,0100,02

这是Arduino代码:

int functions[3][5];
int fLength, numF = 0;
int minF, minFIdx = 98;
String functionString = "";
uint8_t dataPin  = 2;    // Yellow wire on Adafruit Pixels
uint8_t clockPin = 3;    // Green wire on Adafruit Pixels
Adafruit_WS2801 strip = Adafruit_WS2801((uint16_t)11, (uint16_t)9, dataPin, clockPin);


void setup()
{
  //Start Comm on BAUD 9600
 Serial.begin(9600); 
 delay(2);
 strip.begin();
 strip.show();
}
void loop()
{

  //If serial communication exists, build string
  if (Serial.available())
  {
    Serial.print("Serial On");
      delay(5);
    while (Serial.available())
    {
      functionString.concat(Serial.read());
    }
    //sort all data into arrays
    int fSet = 2;
    int numStrings = 0;
    for (int z = 0; getValue(functionString, ',', z); z++)
    {
      numStrings++;
    }

    fLength = stringToNumber(getValue(functionString, ',', 0));
    numF = stringToNumber(getValue(functionString, ',', 1));
    for (int j = 0; j < numF; j++)
    {
       for (int i = 0; i < fLength; i ++)
      {
         functions[j][i] = stringToNumber(getValue(functionString, ',', fSet));
         if (fSet < numStrings -1)
         {
            fSet++; 
         }
      } 
    }
    functionString = "";
  }
  //sort and find the minimum priority w/ index of function if min is 98
  if (minF >= 98)
  {
    for (int u = 0; u < numF; u++)
    {
      if (functions[u][4] < minF)
      {
         minF = functions[u][7]; 
         minFIdx = u;
      }
    }
  }
  //select case for the function index pulled prior
  switch (minFIdx)
  {
   case 0:
   Serial.print("Case 0");
    break;
   case 1:
   Serial.print("Case 1");
    break;
   case 2:
   Serial.print("Case 2");
    break; 
  }
}

如果您查看Serial.print("Serial On");行,它只会在我第一次发送字符串时返回“Serial On”,因此我假设它只进入if (Serial.available())一次。让它再次返回“Serial On”的唯一方法是将代码重新上传到arduino,在VB端无需任何操作,所以我认为问题出在arduino代码的某个地方,下面是一个快照拍摄VB程序,在“发送”它应该返回“Serial On”

~~~~~~~~~~~ EDIT ~~~~~~~~~~~~~~~~~~~~~~~~~

刚才有一个想法,无论如何,我自己的调试命令在中断我的串行通信? Serial.print("Serial On");有谁知道这是否可行???

~~~~~~~~~~~~~~~编辑2 ~~~~~~~~~~~~~~~~~~~~

我能够发现问题,唯一的问题是没有意义,由于某种原因,下面指定的代码段停止所有串行通信,进出arduino .....任何人都可以告诉我为什么这是我怎么能修复它?

for (int z = 0; getValue(functionString, ',', z); z++)
    {
      numStrings++;
    }
编辑3 ~~~~~~~~~~~~~~~~~~~~~

以下是你们正在寻找的getValue()代码

String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

Visual Basic GUI

0 个答案:

没有答案