驱动开发
RapidSCADA 作为驱动程序创建平台的优势:
- Communicator 负责连接(串行、TCP、UDP)。开发者实现数据包的编码和解码。
- 驱动程序可以收集当前、历史数据和事件,并发送命令。
- 内置OPC UA服务器将从开发的驱动程序接收到的数据提供给第三方OPC客户端。
- 一个现成的日志系统。
- 统一的用户配置界面。
接下来我们看一个简单驱动程序的逻辑部分和用户界面的开发,该驱动程序将被命名为驱动程序。要开发实现工业协议的复杂驱动程序,请学习并使用 GitHub 上现有驱动程序的源代码作为示例(链接1,链接2)。
逻辑实现
根据以下内容创建一个新项目类库模板。输入项目名称DrvAbc.Logic,然后选择 .NET 8.0 框架。
添加依赖项ScadaCommon.dll,ScadaCommon.Log.dll和ScadaCommCommon.dll图书馆。库的二进制文件可以在RapidSCADA 安装目录中找到,或者从源代码编译。
双击项目节点解决方案浏览器打开项目文件DrvAbc.Logic.csproj并编辑其属性,如下所示。
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<RootNamespace>Scada.Comm.Drivers.DrvAbc.Logic</RootNamespace>
</PropertyGroup>
创建一个DevAbcLogic类并复制下面的代码。此类实现与设备交互的逻辑。请注意,命名空间和类名必须包含DrvAbc驱动程序代码。探索源代码设备逻辑基类来了解实现设备交互逻辑时可用的功能。
using Scada.Comm.Config;
using Scada.Comm.Devices;
using Scada.Data.Models;
namespace Scada.Comm.Drivers.DrvAbc.Logic
{
internal class DevAbcLogic : DeviceLogic
{
public DevAbcLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
: base(commContext, lineContext, deviceConfig)
{
CanSendCommands = true;
ConnectionRequired = false;
}
public override void Session()
{
base.Session();
Log.WriteLine("DrvAbc driver polling session");
FinishRequest();
FinishSession();
}
public override void SendCommand(TeleCommand cmd)
{
base.SendCommand(cmd);
Log.WriteLine("Command value = {0}", cmd.CmdVal);
FinishCommand();
}
}
}
创建一个DrvAbcLogic其代码如下所示的类。此类实现通用的、非设备特定的驱动程序逻辑。查看源代码驱动逻辑基类来了解可用的功能。
using Scada.Comm.Config;
using Scada.Comm.Devices;
namespace Scada.Comm.Drivers.DrvAbc.Logic
{
public class DrvAbcLogic : DriverLogic
{
public DrvAbcLogic(ICommContext commContext)
: base(commContext)
{
}
public override string Code => "DrvAbc";
public override DeviceLogic CreateDevice(ILineContext lineContext, DeviceConfig deviceConfig)
{
return new DevAbcLogic(CommContext, lineContext, deviceConfig);
}
}
}
驱动程序逻辑部分的示例已准备就绪。构建项目并复制DrvAbc.Logic.dll到 Communicator 驱动程序目录ScadaComm\Drv
接口实现
根据以下内容创建一个新项目Windows 窗体类库模板。输入项目名称DrvAbc.View,然后选择 .NET 8.0 框架。
添加依赖项ScadaCommon.dll,ScadaCommon.Forms.dll,ScadaCommon.Log.dll和ScadaCommCommon.dll图书馆。
双击项目节点解决方案浏览器打开项目文件DrvAbc.View.csproj并编辑其属性,如下所示。
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>disable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Scada.Comm.Drivers.DrvAbc.View</RootNamespace>
</PropertyGroup>
创建一个DevAbcView类并复制下面的代码。此类实现用于配置与设备交互的参数的用户界面。请注意,命名空间和类名必须包含DrvAbc驱动程序代码。在该示例中,驱动程序实际上没有用户界面,但是,需要接口的最小实现,以便可以在管理员应用程序中使用驱动程序。查看源代码设备视图基类来了解可用的功能。
using Scada.Comm.Config;
using Scada.Comm.Devices;
namespace Scada.Comm.Drivers.DrvAbc.View
{
internal class DevAbcView : DeviceView
{
public DevAbcView(DriverView parentView, LineConfig lineConfig, DeviceConfig deviceConfig)
: base(parentView, lineConfig, deviceConfig)
{
}
}
}
创建一个DrvAbcView其代码如下所示的类。此类实现通用的、非设备特定的驱动程序用户界面。查看源代码驾驶员视图基类来了解可用的功能。
using Scada.Comm.Config;
using Scada.Comm.Devices;
namespace Scada.Comm.Drivers.DrvAbc.View
{
public class DrvAbcView : DriverView
{
public DrvAbcView()
: base()
{
CanCreateDevice = true;
}
public override string Name => "ABC Driver";
public override string Descr => "Simple driver example";
public override DeviceView CreateDeviceView(LineConfig lineConfig, DeviceConfig deviceConfig)
{
return new DevAbcView(this, lineConfig, deviceConfig);
}
}
}
负责用户界面的驱动程序部分的示例已准备就绪。构建项目并复制DrvAbc.View.dll到管理员库目录ScadaAdmin\Lib
运行驱动程序
启动管理员应用程序或重新启动它(如果它已打开)。新建一个工程,在里面找到开发的驱动通讯器 > 驱动程序部分。选择驱动程序并确保其描述正确显示。如果显示驱动程序描述时发生错误,则驱动程序用户界面的命名空间或类名称很可能不准确。
创建通信线路并使用以下命令添加设备驱动程序司机到线。运行项目。在通信线路日志中您可以看到有关创建的驱动程序的操作信息:
2024-04-18 13:15:41 Session with the device [3] ABC
DrvAbc driver polling session