如何通过 SQL Gateway 在控制器与任何数据库之间传输数据
需要达到以下系统要求:
oSQL 服务器(比如 MySQL,不由 Schneider Electric 提供)
oSQL 客户端(比如 MySQL Workbench,不由 Schneider Electric 提供)
o数据库,利用 SQL 客户端(比如 MySQL Workbench)创建
oSQL Gateway(其为 EcoStruxure Machine Expert DVD 上的独立设备)
oSqlRemoteAccess 库(其为 EcoStruxure Machine Expert 安装的一部分)
如何通过 SQL Gateway 在控制器与任何数据库之间传输数据
在传输数据之前,必须用 SQL Gateway 建立 SQL 连接。有关详情,请参阅 SQL 网关用户指南。
数据交换所用的功能块要求使用 SQL Gateway 的 IP 地址以及其正在侦听的端口。此外,必须向功能块提供网关内部配置 SQL 数据库连接时所用的连接脉冲。
注意: 本文的代码示例中的 SQL 语法取决于 MySQL 服务器,如果使用其他的 SQL 服务器,命令可能不同。
声明:
PROGRAM WriteData_Secured
VAR
fbSqlDbRequest : SE_SQL.FB_SqlDbRequest;
stExtConnectionSettings : SE_SQL.ST_ExtendedConnectionSettings;
refRequestWstring : SE_SQL.RequestWstring;
refUserData : SE_SQL.UserData;
etSqlRequest : SE_SQL.ET_SqlRequest;
END_VAR
实现:
stExtConnectionSettings.wsDbConnectionName := "ConnectionCountry";
stExtConnectionSettings.wsGwIpAddress := "10.128.154.47";
stExtConnectionSettings.wGwPort := 3458;
stExtConnectionSettings.timSqlTimeout := T#5S;
stExtConnectionSettings.timSqlConnectionTimeout := T#10S;
etSqlRequest := SE_SQL.ET_SqlRequest.Write;
// SQL Command to insert a new Country
refRequestWstring[1] := "INSERT INTO world.countries (Country, CapitalCity, Language)";
refRequestWstring[2] := "values ('Germany', 'Berlin', 'german')";
IF NOT fbSqlDbRequest.q_xReady THEN
fbSqlDbRequest(i_xEnable:= TRUE,
i_xExecute:= FALSE,
i_etSqlRequest:= etSqlRequest,
i_refRequestWstring:= refRequestWstring,
i_uiNumOfWstrings:= 2,
i_refUserData:= refUserData,
iq_stExtendedConnSettings:= stExtConnectionSettings);
ELSE
fbSqlDbRequest(i_xEnable:= TRUE,
i_xExecute:= TRUE,
i_etSqlRequest:= etSqlRequest,
i_refRequestWstring:= refRequestWstring,
i_uiNumOfWstrings:= 2,
i_refUserData:= refUserData,
iq_stExtendedConnSettings:= stExtConnectionSettings);
END_IF
所用参数的描述
步骤 |
操作 |
---|---|
1 |
在 ST_ExtendedConnectionSettings 中,设置 wsDbConnectionName 中配置的连接名称 (SQL Gateway),比如 ConnectionCountry。 |
2 |
在 ST_ExtendedConnectionSettings 中,设置正运行网关的 PC 的 IP 地址 (wsGwIpAddress),比如 10.128.154.47。 |
3 |
在 ST_ExtendedConnectionSettings 中,设置网关的端口 (wGwPort)(缺省:3458)。 |
4 |
在 ST_ExtendedConnectionSettings 中,设置超时 (timSqlTimeout),比如 T#5s。 |
5 |
在 ST_ExtendedConnectionSettings 中,设置超时 (timSqlConnectionTimeout ),比如 T#10s。 |
6 |
将输入 i_etSqlRequest 设置为 SE_SQL.ET_SqlRequest.Write。 |
7 |
在 refRequestWstring 中写入您的写入请求,比如 INSERT INTO world.countries (Country) values ('France')。 |
8 |
在 i_uiNumOfWstrings 中,设置您在 refRequestWstring 中使用的行数,比如 2。 |
9 |
用上述设置/参数/变量来调用 FB_SqlDbRequest。 |
注意: 每调用一次功能块,只能够发送一个请求。
声明:
PROGRAM ReadData_Secured
VAR
fbSqlDbRequest : SE_SQL.FB_SqlDbRequest;
stExtConnectionSettings : SE_SQL.ST_ExtendedConnectionSettings;
refRequestWstring : SE_SQL.RequestWstring;
refUserData : SE_SQL.UserData;
etSqlRequest : SE_SQL.ET_SqlRequest;
END_VAR
实现:
stExtConnectionSettings.wsDbConnectionName := "ConnectionCountry";
stExtConnectionSettings.wsGwIpAddress := "10.128.154.47";
stExtConnectionSettings.wGwPort := 3458;
stExtConnectionSettings.timSqlTimeout := T#5S;
stExtConnectionSettings.timSqlConnectionTimeout := T#10S;
etSqlRequest := SE_SQL.ET_SqlRequest.Read;
// SQL Command to read all Countries in a database
refRequestWstring[1] := "SELECT * FROM world.countries";
IF NOT fbSqlDbRequest.q_xReady THEN
fbSqlDbRequest(i_xEnable:= TRUE,
i_xExecute:= FALSE,
i_etSqlRequest:= etSqlRequest,
i_refRequestWstring:= refRequestWstring,
i_uiNumOfWstrings:= 1,
i_refUserData:= refUserData,
iq_stExtendedConnSettings:= stExtConnectionSettings);
ELSE
fbSqlDbRequest(i_xEnable:= TRUE,
i_xExecute:= TRUE,
i_etSqlRequest:= etSqlRequest,
i_refRequestWstring:= refRequestWstring,
i_uiNumOfWstrings:= 1,
i_refUserData:= refUserData,
iq_stExtendedConnSettings:= stExtConnectionSettings);
END_IF
所用参数的描述
步骤 |
操作 |
---|---|
1 |
在 ST_ExtendedConnectionSettings 中,设置 wsDbConnectionName 中配置的连接名称 (SQL Gateway),比如 ConnectionCountry。 |
2 |
在 ST_ExtendedConnectionSettings 中,设置正运行 SQL Gateway 的 PC 的 IP 地址 (wsGwIpAddress),比如 10.128.154.47 |
3 |
在 ST_ExtendedConnectionSettings 中,设置网关的端口 (wGwPort)(缺省:3458)。 |
4 |
在 ST_ExtendedConnectionSettings 中,设置超时 (timSqlTimeout),比如 T#5s。 |
5 |
在 ST_ExtendedConnectionSettings 中,设置超时 (timSqlConnectionTimeout ),比如 T#10s。 |
6 |
将输入 i_etSqlRequest 设置为 SE_SQL.ET_SqlRequest.Read。 |
7 |
在 refRequestWstring 中写入您的读取请求,比如 SELECT * FROM world.countries。 |
8 |
在 i_uiNumOfWstrings 中,设置您在 refRequestWstring 中使用的行数,比如 1。 |
9 |
在 i_refUserData 中,设置您的 UserData 数组。 |
10 |
用上述设置/参数/变量来调用 FB_SqlDbRequest。 |
注意: 可专门针对库管理器中的项目覆盖高级设置。
步骤 |
操作 |
---|---|
1 |
在 Gc_uiMaxRows 中,设置 ARRAY of UserData 的最大行数(仅 FB_SqlDbRead 需要),比如 20。 结果:可从已配置的数据库中读取 20 行。 |
2 |
在 Gc_uiMaxColumns 中,设置 ARRAY of UserData 的最大列数(仅 FB_SqlDbRead 需要),比如 10。 结果:可从已配置的数据库中读取 10 列。 |
3 |
在 Gc_uiTableWstringLength 中,设置二维 ARRAY of UserData 中的最大字符数(仅 FB_SqlDbRead 需要),比如 200。 结果:可从已配置的数据库中读取包含 200 个字符的字段。 |
4 |
在 Gc_uiMaxRequest 中,设置 ARRAY of RequestWstring 的最大行数,比如 20。 结果:可将请求拆分成 20 行 WSTRINGs。 |
5 |
在 Gc_uiRequestWstringLength 中,设置 ARRAY of RequestWstring 中的最大字符数,比如 200。 结果:一个请求行可包括 200 个字符。 |