使用gcc和termios将uint8_t数组传递给Arduino

时间:2013-10-19 09:20:23

标签: c++ gcc arduino termios uint8t

我目前正在开发一个项目,该项目需要使用termios作为串行通信手段,在g ++编译程序和Arduino ATMega2560之间进行串行通信。 g ++程序发送一个由Arduino解析的5个uint8_t值的数组。然后,Arduino使用这些字节打开条带上的特定LED。

以下是使用g ++编译的C ++程序的代码:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <termios.h>
    #include <inttypes.h>
    #include <fcntl.h>
    #include <fstream>
    #include <iostream>

    #define BAUD B115200
    #define ARDUINO "/dev/ttyACM0"

    using namespace std;

    struct termios old_io, new_io;
    int arduinoFD, c, res;

    int main (int argc, char* argv[]) {

   arduinoFD = open(ARDUINO, O_RDWR | O_NOCTTY);
   if (arduinoFD < 0){perror(ARDUINO); exit(EXIT_FAILURE);}

   new_io.c_cflag = BAUD | CRTSCTS | CS8 | CLOCAL | CREAD;
   new_io.c_iflag = IGNPAR | ICRNL;
   new_io.c_oflag = 0;
   new_io.c_lflag = ICANON;
   cfsetospeed(&new_io, BAUD);
   cfsetispeed(&new_io, BAUD);
   tcflush(arduinoFD, TCOFLUSH);

       //Byte that tells the arduino to start parsing.
   uint8_t* STARTCMD = (uint8_t*)malloc(1);
   STARTCMD[0]=0x0A;
   write(arduinoFD, STARTCMD, 1);

       //Enable debugging.
   STARTCMD[0]=(uint8_t)'d';
   write(arduinoFD, STARTCMD, 1);

       while(true){
          //Allocate array for LED info.
          uint8_t* testWrite = (uint8_t*)malloc(5);
          for(uint8_t i = 0; i < 240; i++){
             //Loop through all LEDs, setting their (R,G,B) to (220,220,220).
             testWrite[0] = 0x73; // 's'
     testWrite[1] = 0xc8; // Red - 220
     testWrite[2] = 0xc8; // Green - 220
     testWrite[3] = 0xc8; // Blue - 220
     testWrite[4] = (uint8_t)i; // Led Address - i

             //Print out the values to stdout.
     char* outPrint = (char*)malloc(17);
     sprintf(outPrint, "R%03dG%03dB%03dL%03d\n",
         testWrite[1], testWrite[2], testWrite[3], testWrite[4]);
     fwrite(outPrint,17,1,stdout);

             //Send the values from the buffer to the arduino, then sleep for 24 milliseconds.
     write(arduinoFD, testWrite, 4);
     usleep(24*1000);
      }
          //Deallocate the buffer and reallocate a space to send the update value.
      free(testWrite);
      testWrite = (uint8_t*)malloc(1);
      testWrite[0] = (uint8_t)'z';
      write(arduinoFD, testWrite, 1);

          //Deallocate and sleep for 550ms.
      free(testWrite);
      usleep(550*1000);     
       }
    }

这是Arduino草图:         #include

    Adafruit_NeoPixel strip = Adafruit_NeoPixel(240, 6, NEO_GRB + NEO_KHZ800);

    int pinRangeStart = 0;
    int pinRangeStop = 0;
    char inByte;
    uint8_t* colorBytes;
    boolean debug = false;

    void setup(){
      Serial.begin(115200);
      while(!Serial);

      Serial.print("Desktop LED Ambience\n");

      strip.begin();
      strip.show();
    }

    void loop(){  
      while(Serial.available() > 0){
        while(Serial.read() != 0x0A);
        Serial.print("Start Byte read!");

        while(true){
          //inByte is the first of 5 bytes to be read. The other four are (R,G,B,L) where
         //R = Red
         //G = Green
         //B = Blue
         //L = LED Number.
          inByte = Serial.read();
          switch(inByte){
            case('r'): {
              for(int i = 0 ; i < strip.numPixels(); i++)
                strip.setPixelColor(i, strip.Color(0,0,0));
              strip.show();
              Serial.println("Reset!");
              Serial.flush();
              break;
            }
            case('d'): {
              Serial.print("Debugging ");
              debug =! debug;
              if(!debug)  
                Serial.println("DISABLED");
              else
                Serial.println("ENABLED");
              break;
            }
            case('s'): {
              colorBytes = new uint8_t[4];
              colorBytes[0] = Serial.read(); // Red
              colorBytes[1] = Serial.read(); // Green
              colorBytes[2] = Serial.read(); // Blue
              colorBytes[3] = Serial.read(); // LED Number
              if(debug){
                Serial.println("Set lights without updating.");
                Serial.print("R=");
                Serial.println(colorBytes[0]);
                Serial.print("G=");
                Serial.println(colorBytes[1]);
                Serial.print("B=");
                Serial.println(colorBytes[2]);
                Serial.print("LED=");
                Serial.println(colorBytes[3]);
              }
              uint32_t newColor = strip.Color(colorBytes[0], colorBytes[1], colorBytes[2]);
              strip.setPixelColor(colorBytes[3], newColor);
              break;
            }
            case('z'): {
              strip.show();
              Serial.println("Updating Lights");
              break;
            }
          }
        }
      }  
    }

g ++程序的输出可以显示为一组字节,按{}分组,表示对write()的单独调用。

    {0x0a}
    {0x64}
    {0x73 0xc8 0xc8 0xc8 0x00}
    {0x73 0xc8 0xc8 0xc8 0x01}
    {...}
    {0x73 0xc8 0xc8 0xc8 0xee}
    {0x73 0xc8 0xc8 0xc8 0xef}
    {0x7a}

arduino正好接收前两个字节并正确解释每组5的第一个字节被发送为0x73(也称为's'),但组中的后续字节未正确读取并且风被Arduino解释为255的值。

预期输出为:

    Desktop LED Ambience
    Start Byte read!
    Debugging ENABLED
    Set lights without updating.
    R=200
    G=200
    B=200
    LED=0
    Set lights without updating.
    R=200
    G=200
    B=200
    LED=1
    ...
    Set lights without updating.
    R=200
    G=200
    B=200
    LED=238
    Set lights without updating.
    R=200
    G=200
    B=200
    LED=239
    Updating Lights

这是实际输出:

    Desktop LED Ambience
    Start Byte read!
    Debugging ENABLED
    Set lights without updating.
    R=255
    G=255
    B=255
    LED=255
    Set lights without updating.
    R=255
    G=255
    B=255
    LED=255
    ...
    Set lights without updating.
    R=255
    G=255
    B=255
    LED=255
    Set lights without updating.
    R=255
    G=255
    B=255
    LED=255
    Updating Lights

有人会碰巧在我的代码中知道是什么导致了这种情况吗?起初我以为总线已经饱和,所以我尝试将波特率降低到19600,但它没有解决任何问题。

编辑:另一个问题是,在设置LED四次或五次迭代后,绿色通道会随机退出,因此所有Arduino得到的都是一个更加歪曲的{'s',255,0,255,255}。 / p>

1 个答案:

答案 0 :(得分:1)

您的内循环while(true)不评估Serial.available。因此,即使没有可用的数据,它也会尝试读取。这是它得到255而不是预期值。有很多方法可以解决这个问题。一种方法是阻止数据可用。

...
while (true) {
    while (!Serial.available()) {}
...