LabVIEW工控二进制数据存储

      在文件存储的逻辑上,二进制文件基于值编码,而不是字符编码,其占用空间小,读取/写入速度快,但是译码比较复杂,不利用数据共享。根据具体编码方式的不同,二进制的使用方式也有所不同,如对bmp格式,规定了文件各个字节段/块的含义,只需要按照相应的编码方式进行解码就可以得到bmp文件的内容。因此,使用记事本是无法查看bmp的内容的(无法解码bmp文件),只能使用专门的图像查看软件。事实上,任何程序员都可以按照自己的方式自定义二进制文件的编码方式,并提供相应的解码模块将信息从二进制文件中提取出来即可。 与文本文件的读写方式类似,LabVIEW中的二进制文件的读写采用图 10所示的两个函数完成:“Write To Binary File”和“Read From Binary File”。这两个函数是多态函数,可以接受File Refnum和File Path两种输入。其使用方式也与文本文件的两个VI类似,本文不再赘述,仅仅说明其特殊的参数和使用方法。

                                                                                                                                                 图 10 File I/O选板

图 11所示为利用Write To Binary File.vi函数将一个数组以二进制文件存储。首先,程序使用10次的FOR循环以产生0~9的数列并将其转换为U8类型的数组;然后将这个数组存入二进制文件中。图 11所示为利用Write To Binary File.vi函数将一个数组以二进制文件存储。首先,程序使用10次的FOR循环以产生0~9的数列并将其转换为U8类型的数组;然后将这个数组存入二进制文件中。

 

                                                                                                                                     图 11 使用Write To Binary File.vi函数

 

如果计算该文件的大小,可以很容易的得到:10*1 Byte(U8)=10 Bytes,即10字节。打开该文件的“属性”对话框,如图 12所示,显示文件大小为14字节。那么多余的4字节是怎么产生的呢?这与Write To Binary File.vi函数的“prepend array or string size (T)”参数有关,它表示当输入端的数据类型是数组或字符串时是否在文件头包含该数组的大小或字符串的长度,默认是True。该长度通过一个32 bits(4 Bytes)的整型表示,因此当往二进制文件中写入数组和字符串时,可以决定是否需要将大小和长度信息写入文件头。

 

                                                                                                                             图 12 test.dat文件属性

 

从上面可知,通过读取test.dat文件的前4个字节获取字符串的长度或数组的大小,如图 13所示

 

图 13 获取test.dat文件的大小

 

在Write To Binary File.vi和Read From Binary File.vi中,均包含一个byte order参数。这实际上就是常说的“大小头”问题,比如U16占有2个字节,是前面的字节占高的内存地址,还是后面的字节占高的内存地址。由于不同的操作平台对该问题有不同的处理方式,因此使用时需要显式地指定,只需要写入和读取时一一对应即可,一般保持默认值。

为了说明数据结构对二进制文件的影响,本文将使用一个比较复杂的数据类型写入二进制文件,如图 14所示。实例中使用簇的数组类型,在簇中依次包含了double、string、I32和2个I32型数据组成的数组元素。

 

                                                                                                                                   图 14 写入二进制文件

 

根据LabVIEW数据类型的定义方式,可以计算单个簇元素的大小是:8 Bytes(double)+5 Byte(string,其中4个字节表示字符串长度)+4 Bytes(I32)+12 Bytes(Array of I32,其中4个字节表示数组的长度)=29 Bytes,整个文件的大小是29 Bytes*10+4 Bytes(表示数组的长度)=294 Bytes。因此,test.dat文件的格式如图 15所示

 

图 15 test.dat文件的格式

 

根据图 15的文件组成,可以获取数组的第6个簇元素的值,如图 16所示。首先读取test.dat的前4个字节获取整个数组的长度;然后,将文件指针移动到第6个元素的开头;最后,读取第6个数据元素即可得到数组中的第6个元素。显然,如果程序员不知道test.dat中的字节存储情况,是无法获取第6个元素值的

 

图 16 获取test.dat文件中数组的第6个元素

 

综上所述,二进制文件由于其高速、高效的特点在高速流盘、安全存取方面应用广泛。同时,由于其无法被常用的字符处理程序直接读取,因此无法便捷地查看数据,而只能通过专用的应用程序读取。此外,对特有的数据结构而言,二进制文件需要非常了解该数据结构的内存占用情况才能够准确检索和定位。

通常,为了识别二进制的基本信息,程序员往往会在文件的头部增加一些基本的头文件信息,用来描述文件的组成。如bmp格式文件在头部使用14个字节表示文件信息以及40个字节表示位图信息,这些信息使得程序员可以快速地获取bmp文件的字节大小、位图像素、分辨率、颜色等相关信息。

转载于:https://www.cnblogs.com/siqing99/p/11490394.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
与西门子PLC通讯的Labview库,还不错。 以下是英文声明: Warning, the attached files are experimental VIs provided for educational purposes only. No warrantee is expressed or implied. You should test your code and completely understand the implications of writing to or reading from an operating PLC. PLCs are often used to control hazardous processes and/or equipment. Writing to or reading from a PLC in active control of equipment or process can result in the disruption of the PLC program or data areas, potentially causing economic loss, property damage, generation or release of hazardous substances and/or personal injury up to and including death. Test your software in a controlled environment and qualify it before using it on active equipment or processes. To my knowledge, Siemens has never released the details of the S7 protocol. Much of what is publicly available on S7 is based on observations of the protocol by others. There is an open source S7 data exchange package available at http://libnodave.sourceforge.net/ that documents many S7 features. These VIs were based upon the example posted at http://decibel.ni.com/content/docs/DOC-5467. They were modified by observing and mimicking a S7 data exchange between a protocol converter and a S7-300 series PLC. The observations were accomplished with the wireshark network protocol analyzer, available at http://www.wireshark.org/ and the Wireshark Plugin f黵 S7-Protokoll, available at http://sps-forum.de/showthread.php?p=202763. The Address Area parameter determines which PLC memory area is the target for the reads and writes. I have only tested reads and writes to the Data Block (DB) Area. Your application should read and write to separate read and write DBs dedicated for transfer only. By confining reads and writes to dedicated Data Blocks, the risk of unintended overwrites may be reduced. The data type: S7Com_Transport_Size.ctl is a ring variable that contains the parameter for the size or type of the transfer. I have only had success with the BYTE, INT and DINT transport sizes on an S7-300 series PLC. The other sizes remain in the ring variable for testing on other PLCs. I do not know if the bytes of a multiple-byte variable are read or written atomically. The example contains two top-level VIs: S7Com_Once.vi and S7Com_W+R_Loop.vi. S7Com_Once.vi performs one read or write per execution. It writes to the target PLC from an array of I32, it reads from the target PLC into an array of I32. The number of bytes written is proportional to the number of elements in the array to be written and the transport size parameter. S7Com_W+R_Loop.vi regularly writes to and reads from a pair of DBs in the target PLC. In order to use it, your Step7 PLC project should provide two Data Blocks, DB11 and DB12. See the screen capture image db11&12.PNG for their layout. After downloading the DBs to your PLC, monitor and change DB VAlues with a VAT. As can be seen in the VI, the transfer size is DWORD. Included are example VIs (S7Com_to_PLC(SubVI).vi and S7Com_from_PLC(SubVI).vi) to map variables between Labview and the S7 PLC DBs. In the example, they use the same cluster type definition (S7Com_PLC_Data.ctl) but that is not a requirement. When you change the mappings, you must calculate the size of the variables to be read from the PLC in order to use the correct Read Length parameter. The read length parameter is in transport size units. The write length parameter is taken from the size of the write data array, so if the write data array is larger than you anticipate, data may be overwritten. The hex byte and hex byte array type definitions were created to format the internal data structures to show hex values so that they could be easily compared to the wireshark packet dumps. These VIs have been minimally tested on a NI 9072 cRIO. This is not finished. Needed are better error checking, cleaner S7 Response decoding, and stress testing with malformed data.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值