当前位置:网站首页>CoDeSys method of reading CSV file (non Excel)
CoDeSys method of reading CSV file (non Excel)
2022-04-22 00:03:00 【tnt1314】
1、 preface
CSV It's universal 、 Relatively simple file format , By user 、 Business and science are widely used . The most widely used is to transfer table data between programs , And these programs themselves operate in incompatible formats ( It's often private and / Or no standard format ). Because a lot of programs support some kind of CSV variant , At least as an alternative input / Output format . If your machine is equipped with Microsoft Excel Words ,.csv By default, the file is Excel The open .
codesys With file open , File read , Function blocks and libraries written to files , that csv Whether the format of the file can be in codesys Open in , The answer is obvious , Next, let's introduce the specific operation methods .
2、CSV data format
Comma separated values (Comma-Separated Values,CSV, Sometimes it's also called Character separated values , Because the separator character can also not be a comma ), Its files store tabular data in plain text ( Numbers and text ). Plain text means that the file is a character Sequence , It doesn't have to be like Binary numbers The data interpreted like that .CSV A file consists of any number of records , Records are separated by some kind of line break ; Each record is made by Field form , The separator between fields is another character or string , The most common are commas or tabs .
Let's take a file data as an example to illustrate : use EXCEL open DATA.CSV file , See the following data :

We can use Notepad mode to open , You can see the following :

By comparison, we can find that , In this case CSV The data from “,” separate , There is no data in the blank space , At the same time, it should be noted that there is a newline between the second line and the first line , The Notepad doesn't show , But in general, we can put CSV Data is treated as a sequence of characters .
3、 File reading function block
3.1、 File reading function block
codesys To read the file, you need to add 4 Library files , This example USES codesys The software version is V3.5.14.10:
a、SysFile, 3.5.15.0(System): The library provides access to files
b、SysTypes2 Interfaces, 3.5.4.0(System): The library provides data formats for accessing files
c、Util, 3.5.15.0(System): The library provides data type conversion
d、StringUtils, 3.5.15.0(System): The library contains functions that handle strings
The method of adding library files is not introduced here .
3.2、 Introduction to function block instructions
The file reading function is mainly used SysFileOpen,SysFileGetSizeByHandle,SysFileRead,SysFileGetPos,SysFileClose Five instructions , The functions are as follows :





3.3、CSV Read the basic process
CSV The steps of file reading are as follows :
a、 adopt SysFileOpen Command to open the file ;
b、 adopt SysFileGetSizeByHandle Get the file character sequence size ;
c、 adopt SysFileRead Read the character sequence once , And sequentially stored in the preset array ;
d、 adopt SysFileGetPos Get file pointer position , Determine whether all data has been read ;
e、 adopt SysFileClose close CSV file , If the file is not closed , Cannot read next time .
f、 Array data processing .
4、 Programming
This example passes ST Language writing
4.1、 Create variables
The variable table is created as follows : The file path is set according to the actual situation , The path cannot contain Chinese , Otherwise, I can't recognize !!.

4.2、 Programming
a、 Read file data 
b、 Data processing
adopt SysFileRead Instructions read out CSV The original data of the character sequence is ASCII Code value , It needs to be transformed , Then merge , Need to avoid " comma "," Carriage return and line change ", And need to pass " comma "," Carriage return and line change " To determine the specific location of the corresponding data , Space limit , Don't explain in detail , The data processing procedure is as follows :

4.3、 Create a visual view
Use multiple text boxes to create a table corresponding to the data , And connect with the array through text variables , Single button control , The view is as follows :

The variables are connected as follows :


4.4、 Running results
Through the simulation run, we first check the original data , You can use the corresponding ASCII Compare the code table with the original data , give the result as follows :


adopt ascii Through comparison and verification, it can be concluded that , There is no problem reading the data .
Then verify the correspondence between the converted data and the table data , The simulation results are as follows :
codesys Read csv Video demonstration of files
5、 review
Through the above analysis , Programming and verification ,codesys Through the file reading function block, it is completely possible to CSV Read file data , The next article will explore the write function .
It should be noted that :
1、 Read CSV when , We found that we need to read out all the data , adopt " comma "、" Line break "、" enter " Wait for symbols to determine the corresponding position of all data , So even if we only need one of these data , You also need to read out all the data .
2、 And when the amount of data is large , The read array we build also needs to have corresponding capacity to store , Otherwise, data will be lost . In this case, the amount of data is small , No more than 50 individual , So it has no effect .
In this paper codesys Version is V3.5.14.10, The programs involved can be downloaded from the following address :
https://download.csdn.net/download/qq_19979629/85196575
版权声明
本文为[tnt1314]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212359535125.html
边栏推荐
- Go language learning -- Process Control II of go language foundation
- [network protocol] DHCP and PXE
- Gartner announces emerging technology research: insight into the meta universe
- 已有千人收藏,一起“干完”这份300页1000道面试题
- JDBC concept steps of creating JDBC project in idea
- 【源码篇】LinkedBlockingQueue源码超详细解读
- 院士专家热议如何拥抱“东数西算”,第二届中国IDC行业Discovery大会顺利召开
- SolidWorks怎么设置运动行程和角度
- How to make knowledge map assist multi round dialogue
- 09. 树莓派ASP.NET环境配置
猜你喜欢

7.10 thread condition variables

【超详解加源代码和测试结果】顺序表的一系列操作合集

Fansea 4W single coil transmitting wireless charging 5W module

7.9 thread mutex
![[fundamentals of interface testing] Part III differences between traditional style interfaces and restful style interfaces](/img/9b/dc183dc9d02bda4b1ad6e6164ec84e.png)
[fundamentals of interface testing] Part III differences between traditional style interfaces and restful style interfaces

"National tide" building blocks, the next answer to tide play?

AI helps to monitor the wearing of labor protection equipment and escort the safe production of enterprises

AI's new story is hidden in Robin Li's "intelligent transport" audio book.

稳压二极管限流电阻怎么选择

DetNet: A Backbone network for Object Detection
随机推荐
可替换MPS MP2451的高压DCDC芯片FS2451助力智能电表设计40V0.5A降压IC
"Yushu technology" completed hundreds of millions of yuan of Series B financing and accelerated the large-scale commercial landing of quadruped robots
[MySQL optimization 3] observe the MySQL process status
Cvpr2022 oral | contribution imbalance in multimodal learning
Centos7 install MySQL using Yum - specify the version
Gartner发布新兴技术研究:深入洞悉元宇宙
开关电源中开关管与二极管EMI抑制方法分析
Leetcode -- character string
Dw07d two in one lithium battery protection IC
[fundamentals of interface testing] Part III differences between traditional style interfaces and restful style interfaces
Difference between breadth first notes and breadth first notes
Go language learning -- Process Control II of go language foundation
低成本成FS68001A、FS68003无线充SOC芯片
【无标题】2022煤炭生产经营单位(机电运输安全管理人员)上岗证题目模拟考试平台操作
How about huarongda futures? Is it safe to open futures accounts?
小程序 分包
[leetcode daily question] goat Latin
Kuangshi Research Institute | dark vision network: RGB IR Fusion low illumination imaging method using depth inconsistent a priori
Three special data types of redis -- bitmap calculation
Cuda02 - memory access optimization and unified memory