简体   繁体   中英

How to reliably send large strings via serial port to arduino

I'm trying to send a large string to Arduino as constant as possible, But whatever i try it is never reliable, On c# I have:

private void OnTimedEvent(Object myObject, EventArgs myEventArgs)
{
    string start = "0:" + 255 * (l / 100) + ":0;";
    start = string.Concat(Enumerable.Repeat(start, 24));
    start += ".";

    char[] end = start.ToCharArray();
    port.Write(new string(end));
    Invalidate();
}

And on arduino i have:

#include <FTRGBLED.h>
#include <Vect3d.h>

int clockPin = 13;
int dataPin = 11;

Tinker::Vect3d<float> recCol(0,0,0);

RGBLEDChain led(25, clockPin, dataPin);

int count = 0, ledc = 0;

void setup() {
  led.begin();
  Serial.begin(19200);
}

void loop() {
  if(Serial.available() > 0){
    char c = Serial.read();
    if(c == '.')
    {    
      ledc = 0;  
      recCol[0] = 0;
      recCol[1] = 0;
      recCol[2] = 0;
      led.update();
    }
    else
    {
      if(c == ';')
      { 
        FTLEDColour col = { recCol[0] , recCol[1] , recCol[2] };
        led.setLED(ledc, col);
        recCol[0] = 0;
        recCol[1] = 0;
        recCol[2] = 0;
        ledc++;
        count = 0;
      }

      if(c == ':')
      { 
        count++; 
      }
      else 
      {
        switch(count){
          case 0:
            recCol[0] = recCol[0] * 10 + (c - '0');
            break; 
          case 1:
            recCol[1] = recCol[1] * 10 + (c - '0');
            break;
          case 2:
            recCol[2] = recCol[1] * 10 + (c - '0');
            break;
        }
      }
    } 
  }
}

When the string is created fully static in c# all the lights change, When I try to add some non static bits such as the 255 * var, it suddenly only works for the first LED and flashes random colours, not greenfrom 0 - 255.

First of all, have you confirmed the string being sent is as you expect it to be?

I would start by writing it to debug.writeline to verify.

Second, if you use the Arduino terminal and type the string to it, does it respond as expected?

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