简体   繁体   中英

serial read changes input & doesn't read whole line

Large strings sent over serial are not correctly read using an Arduino Uno and the Arduino IDE. When the typed command is too long, not the whole data is returned and sometimes is is even random numbers.

99,3,0,1,0,0,0 Results correctly in (not sure why there is an ending , tho): receivedValues are: 99,3,0,1,0,0,0,

99,3,0,0,0,0,0 Results correctly in: receivedValues are: 99,3,0,0,0,0,0,

Where is starts to go wrong:

99,3,0,100,200,300,400 Results in: receivedValues are: 99,3,0,100,200,44,144,

99,123456789 Results in: receivedValues are: 99,21,0,1,200,200,244,

99,3,0,1,200,200,2000 Results in: receivedValues are: 99,3,0,1,200,200,208,

Here is the part of my code that is relevant:

uint8_t receivedValues[7] = {0, 0, 0, 0, 0, 0, 0};

#define HEADER 0 // 99
#define CMD   1 // LICHT (2)   || GEUR (3)
#define DATA0 2 // KLEUR INDEX || GEUR INDEX
#define DATA1 3 // H           || ON / OFF
#define DATA2 4 // S
#define DATA3 5 // V
#define DATA4 6 // BlendTime

#define CHECK 99

#define ON 1
#define OFF 0

bool messageReceived = false;
bool startBlending = false;

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

void loop()
{
  ParseSerial();
  if (messageReceived)
  {
    printMessage();
    // CheckCommand();
    messageReceived = false;
  }
}


void ParseSerial()
{
  int serialIndex = 0;
  if (Serial.available() > 8)
  {
    while (0 < Serial.available())
    {
      String bufferString;
      int bufferInt;

      bufferString = Serial.readStringUntil(',');
      bufferInt = bufferString.toInt();
      receivedValues[serialIndex] = bufferInt;

      serialIndex++;
    }
    if (receivedValues[HEADER] == CHECK)
    {
      Serial.print("receivedValues[0]: ");
      Serial.println(receivedValues[0]);
      messageReceived = true;
    }
    if (receivedValues[HEADER] != CHECK)
    {
      Serial.println("not a good package");
    }
    Serial.flush();
  }
  else
  {
    Serial.flush();
  }
}

void printMessage()
{
  Serial.print("receivedValues are: ");
  for (int i = 0; i < 7; i++)
  {
    Serial.print(receivedValues[i]);
    Serial.print(",");
  }
  Serial.println();
}

If the header of the message does not start with 99 it will write not a good package . What is noticeable it that when I do enter a command with 99 at the beginning ONCE, it will write not a good package twice most of the time.

receivedValues is declared as uint8_t receivedValues[7] . The maximum value for uint8_t is 255. If you try to store a larger number, it will be truncated.

If you want to store larger numbers, you need to pick a wider integer type for your array, eg

uint32_t receivedValues[7] = {0, 0, 0, 0, 0, 0, 0};

Which will work up to UINT32_MAX , which is 0xFFFFFFFF ( 4294967295 )

I found the solution to this problem. Changing uint8_t was not the answer.

A byte of 8 binary bits can represent 28 = 256 numbers: 0 - 255. The serial connection sends data byte by byte, so if you want to send a number larger than 255, you'll have to send multiple bytes

Link to source: https://forum.arduino.cc/index.php?topic=492055.0

I ended up sending a smaller number (minutes) than 255, and then in the Arduino code multiply it again (to seconds).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM