0

我在 TwinCat 中有一个程序,其中每 10 秒更新 5 个整数变量以表示 5 个泵的状态。我想将这些正在生成的值保存到文本文件或 CSV 文件中,以便稍后从 PLC 中提取。下面的代码:

IF toninPumpPulse.Q THEN
    rinPump1RPM := (inPump1Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump1Count := 0; //Reset counter for next 10 second sample
    rinPump2RPM := (inPump2Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump2Count := 0; //Reset counter for next 10 second sample
    rinPump3RPM := (inPump3Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump3Count := 0; //Reset counter for next 10 second sample
    rinPump4RPM := (inPump4Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump4Count := 0; //Reset counter for next 10 second sample
    rinPump5RPM := (inPump5Count/6)*6; //Pulse sample for 10 seconds. 6 pulses = 1 round.
    inPump5Count := 0; //Reset counter for next 10 second sample

我希望创建一个新的 CSV 文件,然后用变量值填充。我对 TwinCat 非常缺乏经验,阅读 Beckhoff 网站也没有什么帮助。

4

2 回答 2

2

您想使用多个功能块的组合:

  • FB_FileOpen
  • FB_FilePuts
  • FB_FileClose

为此目的,您需要的所有文档 + 示例代码已在 Beckhoff infosys 上提供:

https://infosys.beckhoff.com/english.php?content=../content/1033/tcplclibutilities/html/tcplclibutilities_csv_sample.htm&id=

另请参阅有关如何写入文件的一般信息: TwinCAT 3:写入文件

于 2021-04-23T06:26:23.640 回答
0

开源日志库TcLog可能会对您有所帮助。它为文件系统提供扩展的日志记录功能。

我写了一篇介绍它的博文,你也可以在那里下载一个预编译的库。

这就是你在你的情况下使用它的方式:

TcLogCore首先,定义一个包含配置的静态记录器实例:

VAR
  CoreLogger : TcLogCore;
END_VAR

CoreLogger
 .MinimumLevel(E_LogLevel.Warning)
 .WriteToFile('c:\logs\', 'pumpValues.csv')
 .RunLogger();

接下来,定义一个TcLog实际记录消息的记录器:

VAR
  Logger : TcLog;
END_VAR

您可以使用该记录器生成如下日志消息:

Logger
.OnCondition(toninPumpPulse.Q)
.AppendAny(rinPump1RPM)
.AppendString(',')
.AppendAny(rinPump2RPM)
.AppendString(',')
.AppendAny(rinPump3RPM)
.AppendString(',')
.AppendAny(rinPump4RPM)
.AppendString(',')
.AppendAny(rinPump5RPM)
.ToCustomFormat('');

确保toninPumpPulse.Qtrue用于一个周期,否则您将生成大量消息。看看记录上升/下降沿

由于记录器是作为单例实现的,因此您可以在程序中的任何位置使用它,它会自动使用TcLogCore.

库中有更多选项,例如为日志文件设置滚动间隔或自动删除可能对您有用的旧文件。

于 2021-11-06T14:50:53.717 回答