【问题标题】:How to save value of integer variable in TwinCat3?如何在 TwinCat3 中保存整数变量的值?
【发布时间】:2021-04-23 06:06:51
【问题描述】:

我在 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 网站也没有什么帮助。

【问题讨论】:

    标签: plc twincat structured-text


    【解决方案1】:

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

    • 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: Write to File

    【讨论】:

    • 我不确定如何将您提到的功能集成到我现有的代码中。我必须事先在我的 PLC 中创建一个空的 CSV 文件吗?每次运行用于测量泵计数的代码时,我是否能够创建新的 CSV 文件
    • @SaravananMohan InfoSys 链接有一个完整的工作示例。我建议你下载那个并尝试一下。
    • 我无法打开下载的文件。它将文件类型声明为 PRO 文件。无法使用 Visual Studio 甚至记事本打开它。有什么建议吗?
    • 这是一个 TwinCAT 2 文件。您必须下载 TwinCAT 2 并查看它。我通常只为需要测试的每个 TwinCAT 版本创建一个虚拟机。
    • 我已将您的文件编写器示例用作在需要时调用的单独功能块。我遇到的问题是文件打开函数在 fbFileOpen.bError 下不断抛出错误。我不确定如何解决此问题。
    【解决方案2】:

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

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

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

    首先,定义一个包含配置的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.Q 仅在一个周期内为true,否则您将生成大量消息。也可以看看logging on rising/falling edges

    由于记录器是作为单例实现的,你可以在程序的任何地方使用它,它会自动使用TcLogCore的配置。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 2022-10-14
      • 2016-12-15
      • 2020-04-26
      相关资源
      最近更新 更多