Use Pointers to Make Your CRBasic Programs More Efficient

byGary Roberts| Updated: 03/23/2017 | Comments: 2

搜索博客


订阅博客

Set up your preferences for receiving email notifications when new blog articles are posted that match your areas of interest.


区域 /申请

产品分类

Activity

Corporate / News

输入你的电子邮箱地址:



建议一篇文章

您想了解更多有关有关的话题吗?让我们知道。请尽可能具体。

将这个领域空白

指针是CRBASIC程序的绝佳工具。如果您仔细地使用它们,指针可以减少所需编写的程序代码量,从而提高程序的效率并使您能够使用更少的内存。(您的程序可以更快地运行,因为它不必在内存中复制数据)。

要学习如何使用指针,首先谈论变量及其存储方式将很有帮助。变量存储在数据记录器内存中的内存单元中。数据记录器的内存由连续的内存单元组成,一个字节长,每个字节都有一个唯一的地址。为了更好地了解数据记录器如何使用其内存,我将使用下面的类比,当我们开始谈论指针时,这应该有所帮助。

Understanding Data Logger Memory

想象一下,数据记录器的内存是一个大型仓库,可以在其中存储很多东西。在我们的示例中,我们将用不同尺寸和类型的桶填充仓库。(根据我们的需求,我们可能会填充整个仓库或仅部分的一部分。)我们使用的所有存储桶持有一种或另一种类型的东西。较大的水桶用于容纳较大的东西,较小的水桶用于较小的东西。所使用的存储桶的数量和大小以及仓库中使用的空间数量取决于您作为程序员和您正在从事的项目的需求。

当我们开始编写CRBASIC程序时,我们会声明变量。在我们的仓库场景中,当我们声明一个变量时,我们将在数据记录器仓库中创建一个存储桶(使用内存单元格),以保存我们想要使用的信息(变量)。该存储桶只能容纳某些类型的变量。例如,一个小桶容纳一个浮点变量,并占据了数据记录器的免费仓库(内存)空间的大约4个字节。另一个小桶有一个布尔变量,它还占用了仓库中4个字节的地板空间。然后,我们添加一个容纳一个字符串变量的大型存储桶,该变量占据了24个字节的仓库空间。

有了一个装满水桶的仓库,我们怎么知道哪个水桶拥有我们想要的信息?幸运的是,我们有能力将有意义的名字命名为有助于描述其持有的信息。例如,我们可以给这个名字temperatureto a bucket that holds a float variable by declaring our float variable astemperature。随着我们继续添加和声明变量,数据记录器的仓库(内存)开始填充。

如果我们的仓库越来越饱,我们如何轻松找到所需的水桶?幸运的是,数据记录器的处理器是一位出色的仓库经理。它知道每个存储桶存储在哪里。每个存储桶都有一个特定的独特地址,并在数据记录仪的仓库中带有过道,部分和架子。(没有两个水桶共享相同的地址。)仓库经理可以使用唯一地址快速在仓库中找到水桶。当我们来到仓库时,想要拿着浮点变量的铲斗我们命名temperature, the data logger efficiently decodes the address and gets the right bucket (data logger memory cells) we are looking for and the information (variable) it contains. We can then read or change what is inside thetemperature桶(例如,temperature= 32.14)。

指针的基础

Now here is where pointers come into play. Pointers are a way for us to help our warehouse manager (the data logger’s processor) be even more effective at its job and be a better steward of the limited warehouse space (memory).

A pointer is a variable whose value is the address of another variable. Just like any variable or constant, you must declare a pointer before you can work with it. When you declare a pointer in CRBasic, it needs to be declared as a typeLong:

Public my_pointer As Long

这条代码线称为指针声明或指针变量。指针变量存储我们要指出的另一个变量的地址。

To use or initialize the new pointer, we then need to tell CRBasic to point it at a specific piece of memory that is tied to thetemperature多变的。为此,我们可以使用以下语法:

my_pointer= @temperature

这memory (warehouse) address oftemperature现在存储在指针变量中my_pointer。这@符号被称为操作员。它是一个单一操作员,返回其操作数的内存地址。使用上面的示例,my_pointer现在等于地址temperature

现在,这使我们能够在CRBASIC计划中做几件事。例如:

public my_pointer长达公共麦克(tom),汤姆(Tom)长时间,梅利莎(Melissa)long my_pointer = @mike mike = 42 tom = Mikemelissa = my_pointer

在这个简短的CRBASIC程序中,以下内容是正确的:

  • 麦克风now equals42
  • Because汤姆设置等于麦克风,汤姆also equals42
  • melissa现在等于Mike的内存地址。
  • melissa不等于42但是分配给变量的一些内存地址号码麦克风通过数据记录器。
  • melissais now also a pointer like the variablemy_pointerbecausemelissa具有相同的信息my_pointer有。

如果我们想要变量melissa包含与麦克风(而不是Mike的内存地址),我们需要使用指针退出或间接操作员!如下所示:

public my_pointer长达公共麦克(tom),汤姆(Tom)长时间,梅利莎(Melissa)long my_pointer = @mike mike = 42 tom = Mikemelissa = !my_pointer

Nowmelissa等于42- 与麦克风汤姆

好吧,这是整洁的,但这不是一个容易的设置melissaequal to麦克风? Most of the time, yes. There are times when a pointer is handier, but that’s a more advanced topic.

使用指针

Now to put pointers into action.

在里面“6步骤轻松地解析数据从一个可信的源头e” blog article,我与您分享了如何使用CRBASIC数据记录器从其他来源分析XML。现在,我将向您展示如何使用指针来改进和缩短程序,并保存宝贵的数据记录记忆。

在早期的博客文章中,我与您共享的程序查找并从XML文件中读取一个变量。要从返回的XML获取更多信息,我们将不得不添加更多信息如果statements to get the information we need (such as humidity):

如果xml_response_code = xml_end_of_element和XML_ELEMENT_NAME =“ ferver_humity”,那么noaa_releative_humity = xml_value endif

要从XML文件中获取所有数据,我们将添加34个附加如果statements to get it all parsed. Now, using pointers, we can get the work done in a single如果陈述!

如果xml_response_code = xml_end_of_element,则指针= 0 pointer = @(xml_element_name)If(pointer > 0) Then !pointer = xml_value万一

我们所做的是声明我们想要使用与XML文件中元素相同的名称中存储信息的变量。通过这样做,我们利用数据记录仪中的指针,使我们的编程更加紧凑和精确。

使用指针,我们的程序像以前一样下载XML,并开始使用XMLParse()instruction. When it gets to the end of an element name, we start into our如果statement. The first line in that如果statement sets the pointer to memory address0。这只是一个很好的编程实践,因为数据记录器将永远不会返回对地址的变量的引用0。这有助于我们(以及数据记录仪)知道下一行代码中的解除是否有效或失败。

这next line sets the pointer to the address-of operator (@) for the variable with the same name as is stored in the variablexml_element_name。这parentheses around the variable namexml_element_name告诉数据记录仪不要查看地址xml_element_name, but to look at what is stored inxml_element_name。例如,当xml_element_name= “relative_humidity”the variable pointer points to the memory address of the variable named相对湿度和not toxml_element_name。That is why our variable names must be named the same as the elements in the XML file we are parsing.

这last line in the statement says that if the pointer is not pointing to memory address zero, pour the information inxml_value在指向指向的地址的变量中。使用我们以前的示例,存储在xml_value将被倾倒到变量相对湿度

Aren’t pointers handy?

More Information

我包括一个使用程序的工作示例XMLParse()和指针。只是download the CRBasic program进入数据记录器(确保数据记录器具有活动的Internet连接),运行并使用指针观看程序更新。

I hope this information was helpful to you. If you have any questions, please post them below.


分享这篇文章


关于作者

加里·罗伯茨加里·罗伯茨(Gary Roberts)是坎贝尔科学公司(Campbell Scientific,Inc.万博matex网页登录新万博2019最新活动啊加里的教育和背景是信息技术和计算机科学。当他不上班时,他将与童子军一起享受大户外活动,与火/EMS,工作业余广播或编程计算机进行战斗。

查看该作者的所有文章。


注释

凯利|02/16/2018 at 04:56 AM

这是帮助文件中的几行。

"

可以在表达式中访问指针:

!(指针 + x)

将访问Pointer + X的值。

"

so, using this call:

UnpackArray(PkAryVal, @UnPack(),12)

为什么这项工作:

Sub UnpackArray(Val As Long, AryPtr As Long, Elements As Long)
DIM参考长期
Dim x As Long
For x = 0 To Elements - 1
ref = aryptr + x
!ref =(val >> x和1)
Next x
EndSub

but not this:

Sub UnpackArray(Val As Long, AryPtr As Long, Elements As Long)
Dim x As Long
For x = 0 To Elements - 1
!(aryptr + x)=(val >> x和1)
Next x
EndSub

在CR6 OS 6.08上进行测试

感谢您在这个问题上可以忽略的任何灯光

CRS|04/07/2018在03:51 PM

我希望看到更多有关在CRBASIC中使用指针的博客文章 /教程。

I am trying to write a function that can accept any type of variable (or array) and perform some operation on it (e.g., using TypeOf() and SprintF()), but not having any luck recognizing the original variable within my function. How does the following, from the CRBasic help file, apply?

By default, pointer variables are of type Long, but they can be typed using Float!, Long!, Boolean!, or String! (e.g., Dim P as Float!). If pointers are not typed, the summation of the values pointed to truncates the floats before the addition.

Also, how would I handle an array within the function? Does the pointer offset have to be scaled for each successive array element by the size of that element?

Pleaselog in or register评论。

我们活跃于社交媒体上!
Stay informed with our latest updates by following us on these platforms:

Baidu