获取指定char数组arduino的前n个元素 [英] getting the first n elements of a specified char array arduino

查看:374
本文介绍了获取指定char数组arduino的前n个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是从序列中读取一些字符串,例如234124!3455addg#5867,如果程序看到了!它应该开始将其添加到char数组中,并且如果它看到#,则应返回该char数组的前4个元素(对于我的示例),返回值应为3455.我该如何解决呢?我使用String类进行此操作,但需要将其实现为char数组. 我在arduino上还很陌生,所以请清楚谢谢. 这是我的代码:

My aim is reading some string from serial for example 234124!3455addg#5867 if the program sees ! it should start to add it to a char array and if it sees # it should return the first 4 elements of that char array for my example the return should be 3455. How can I solve it? I made this using String class but I need to implement it to char array. I am quite new on arduino so please be clear thank you. Here is my code:

const char *s = "123123123!0037selam#aaaaSDSDa";
const char *CHAR1 = "!";
const char *CHAR2 = "#";

char *target = NULL;
char *start, *end;

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

void loop() {
    if ( start = strstr( s, CHAR1 ) )
    {
        start += strlen( CHAR1 );
        if ( end = strstr( start, CHAR2 ) )
        {
            target = ( char * )malloc( end - start + 1 );
            memcpy( target, start, end - start );
            target[end - start] = '\0';
        }
    }

    if ( target )
    {
        for(int i=0; i<4;i++)
            Serial.print( target[i]);
    }

    free( target );
    return 0;
}

我认为,

推荐答案

这是一种更简单的解决方法.这取决于是否有未明确说明的要求.

Here's a simpler way of going about it, I think. It depends on whether or not there are requirements that aren't explicitly stated.

几件事值得一提,

  1. 您想返回!"后的前4个字节,因此您 只需要缓冲4个字符
  2. 我目前还没有方便的所有电缆,所以我只是 碰撞在一起的东西要在PC上运行.在您的情况下, 返回字符串缓冲区的副本,您只需将其输出即可 Serial.print
  1. You'd like to return the first 4 bytes that follow a '!', so you only need to buffer 4 chars
  2. I haven't got all the cables handy at the moment, so I've just banged-together something to run on the PC. In your case, instead of returning a copy of the string buffer you'd just output it with Serial.print

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

class dummy
{
    public:
        dummy()
        {
            const char *testData = "234124!3455addg#5867";
            int dataLen = strlen(testData);
            mData = new char[dataLen+1];
            strcpy(mData, testData);
            mTotal = strlen(testData);
            mIndex = 0;
        }
        int available()
        {
            return mTotal - mIndex;
        }
        char read()
        {
            return mData[mIndex++];
        }
    private:
        char *mData;
        int mIndex;
        int mTotal;
};

char *testFunc()
{
    dummy *Serial = new dummy();
/// -------- 8< ------------ cut here until the next pair of scissors. put inside the loop function
/// your code does all of the functionality (reading and buffering) inside a single iteration of loop(). 
/// Normally, I'd expect a single character to be read each time. I'd expect loop() to be 
/// run 16 times before a result was output, since # is the 16th character of the string.
    char tmpBuffer[5] = {0};
    int bufferIndex = 0;
    bool marker1Seen = false;

    while (Serial->available() > 0)
    {
        char received = Serial->read();
        if (received == '!')
        {
            marker1Seen = true;
        }

        else if (received == '#')
        {
            return strdup(tmpBuffer);
        }

        else if (marker1Seen == true && bufferIndex < 4)
        {
            tmpBuffer[bufferIndex++] = received;
        }
    }
    // shouldn't get here if the input is well-formed
    return NULL;
/// -------- 8< ------------ cut here
}

int main()
{
    char *result = testFunc();
    cout << result;
    delete result;
}

这篇关于获取指定char数组arduino的前n个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆