如何使用arduino拆分char数组 [英] How do I split a char array with arduino

查看:742
本文介绍了如何使用arduino拆分char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从我的Arduino读取我的GPS坐标作为逗号分隔的缓冲区数组。数据如下:



 $ GPGGA, 095350.000  5112.1239 ñ 04315.0058 ë中,1,7,1.13,0.3,男,47.2,





这是我的代码:



 #include< SoftwareSerial.h> ; 

SoftwareSerial SoftSerial( 4 5 );
unsigned char buffer [ 64 ];
int count = 0 ;

void sendGPSData(){
if (SoftSerial.available ())
{
while (SoftSerial.available())
{
char read = SoftSerial.read();

buffer [count ++] = read;

Serial.print( String (read));

if (count == 64 ){
断裂;
}
}

延迟( 2000 );
clearBufferArray();
count = 0 ;
}
}

void clearBufferArray()
{
for int i = 0 ; i < count; i ++)
{
buffer [i] = NULL;
}
}





我得到的问题是如何分割缓冲区阵列以便我可以在上面的输出中获得粗体部分?



我尝试过的事情:



我试图使用此代码 GPS数据·GitHub [ ^ ]但需要很多资源。

解决方案

< BLOCKQUOTE> GPGGA, 095350.000 5112.1239 ñ 04315.0058 ë中,1 ,7,1.13,0.3,男,47.2,





这是我的代码:



 #include< SoftwareSerial.h> 

SoftwareSerial SoftSerial( 4 5 );
unsigned char buffer [ 64 ];
int count = 0 ;

void sendGPSData(){
if (SoftSerial.available ())
{
while (SoftSerial.available())
{
char read = SoftSerial.read();

buffer [count ++] = read;

Serial.print( String (read));

if (count == 64 ){
断裂;
}
}

延迟( 2000 );
clearBufferArray();
count = 0 ;
}
}

void clearBufferArray()
{
for int i = 0 ; i < count; i ++)
{
buffer [i] = NULL;
}
}





我得到的问题是如何分割缓冲区阵列以便我可以在上面的输出中获得粗体部分?



我尝试过的事情:



我试图使用此代码 GPS数据·GitHub [ ^ ]但需要很多资源。


我用它来做我自己的GPS解析器。我把它从网上或书中拉出来,但不记得在哪里,所以我不能给予其作者适当的信誉。你需要声明一个char数组来保存你可能已经拥有的GPS数据,然后是一个字符串数组,在这种情况下msg_field []为char数组中的每个字段提供足够的元素 - 一个字段是逗号之间的数据。对于GPS数据来说,二十就足够了。



字节getCSVfields()
{
byte _sentencePos = 0 ;
byte _comma_count = 0 ;
msg_field [_comma_count] = ;
while 1
{
if (input_buffer [_sentencePos] == NULL) break ;
if (input_buffer [_sentencePos] == 44
{
_comma_count ++;
msg_field [_comma_count] = ;
_sentencePos ++;
}
else
{
msg_field [_comma_count] + = input_buffer [_sentencePos];
_sentencePos ++;
}
}
return _comma_count + 1 ;
}





你最终得到的是一系列字符串,在你的情况下是



GPGGA,095350.000,5112.1239,N,04315.0058,E,1,7,1.13,0.3,M,47.2


msg_field [0] =


I'll read my GPS coordinates form my Arduino as a comma seperated buffer array. the data comes in as follow:

$GPGGA,095350.000,5112.1239,N,04315.0058,E,1,7,1.13,0.3,M,47.2,



Here is my code:

#include <SoftwareSerial.h>
 
SoftwareSerial SoftSerial(4, 5);
unsigned char buffer[64];
int count = 0; 

void sendGPSData() {
    if (SoftSerial.available())
    {
        while (SoftSerial.available())
        {
            char read = SoftSerial.read();

            buffer[count++] = read;

            Serial.print(String(read));

            if (count == 64) {
                break;
            }
        }

        delay(2000);
        clearBufferArray();
        count = 0;
    }
}

void clearBufferArray()
{
    for (int i = 0; i < count; i++)
    {
        buffer[i] = NULL;
    }
}



The question I've got is how can I split the buffer array so I could get the bold parts in the output above?

What I have tried:

I've tried to use this code GPS data · GitHub[^] but there are to much resources needed.

解决方案

GPGGA,095350.000,5112.1239,N,04315.0058,E,1,7,1.13,0.3,M,47.2,



Here is my code:

#include <SoftwareSerial.h>
 
SoftwareSerial SoftSerial(4, 5);
unsigned char buffer[64];
int count = 0; 

void sendGPSData() {
    if (SoftSerial.available())
    {
        while (SoftSerial.available())
        {
            char read = SoftSerial.read();

            buffer[count++] = read;

            Serial.print(String(read));

            if (count == 64) {
                break;
            }
        }

        delay(2000);
        clearBufferArray();
        count = 0;
    }
}

void clearBufferArray()
{
    for (int i = 0; i < count; i++)
    {
        buffer[i] = NULL;
    }
}



The question I've got is how can I split the buffer array so I could get the bold parts in the output above?

What I have tried:

I've tried to use this code GPS data · GitHub[^] but there are to much resources needed.


I use this for my own GPS parser. I pulled it off the net or out of a book but can't remember where so I can't give proper credit to its author. You need to declare a char array to hold the GPS data which you probably already have, and then a string array, in this case msg_field[] with enough elements for each field in the char array - a field being the data between the commas. Twenty is sufficient for GPS data.

byte getCSVfields()
{
    byte _sentencePos = 0;
    byte _comma_count = 0;
    msg_field[_comma_count] = "";
    while (1)
    {
        if (input_buffer[_sentencePos] == NULL) break;
        if (input_buffer[_sentencePos] == 44)
        {
            _comma_count++;
            msg_field[_comma_count] = "";
            _sentencePos++;
        }
        else
        {
            msg_field[_comma_count] += input_buffer[_sentencePos];
            _sentencePos++;
        }
    }
    return _comma_count + 1;
}



What you end up with is an array of Strings, in your case with


GPGGA,095350.000,5112.1239,N,04315.0058,E,1,7,1.13,0.3,M,47.2

msg_field[0] =


这篇关于如何使用arduino拆分char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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