forked from xuos/xiuos
new paragraph named xdh_60t4_e.c xsdh_60a32_e.c omron_cp1l.c
This commit is contained in:
parent
d797a999ae
commit
0a58099c51
|
@ -11,10 +11,10 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file omron_cj2m.c
|
||||
* @brief PLC OMRON CJ2M app
|
||||
* @file omron_cp1h.c
|
||||
* @brief PLC OMRON CP1H app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022.9.27
|
||||
* @date 2023.4.1
|
||||
*/
|
||||
|
||||
|
|
|
@ -45,4 +45,4 @@ void ControlOmronCP1LTest(void)
|
|||
//ControlProtocolClose(fins_protocol);
|
||||
}
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(ControlOmronCP1LTest, Omron Plc FINS Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
PRIV_SHELL_CMD_FUNCTION(ControlOmronCP1LTest, Omron Plc Cp1l Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
|
@ -18,6 +18,34 @@
|
|||
* @date 2022.9.27
|
||||
*/
|
||||
|
||||
|
||||
#include <control.h>
|
||||
|
||||
void ControlOmronNX102Test(void)
|
||||
{
|
||||
int i = 0;
|
||||
uint16_t read_data_length = 0;
|
||||
uint8_t read_data[1024] = {0};
|
||||
ControlProtocolType fins_protocol = ControlProtocolFind();
|
||||
if (NULL == fins_protocol) {
|
||||
printf("%s get fins protocol %p failed\n", __func__, fins_protocol);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s get fins protocol %p successfull\n", __func__, fins_protocol);
|
||||
|
||||
if (CONTROL_REGISTERED == fins_protocol->protocol_status) {
|
||||
ControlProtocolOpen(fins_protocol);
|
||||
|
||||
for (;;) {
|
||||
read_data_length = ControlProtocolRead(fins_protocol, read_data, sizeof(read_data));
|
||||
printf("%s read [%d] fins data %d using receipe file\n", __func__, i, read_data_length);
|
||||
i++;
|
||||
PrivTaskDelay(10000);
|
||||
}
|
||||
|
||||
// ControlProtocolClose(fins_protocol);
|
||||
}
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(ControlOmronNX102Test, Omron Plc FINS Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
{
|
||||
"device_id": 1,
|
||||
"device_name": "CP1L",
|
||||
"communication_type": 0,
|
||||
"socket_config": {
|
||||
"plc_ip": "192.168.250.31",
|
||||
"local_ip": "192.168.250.233",
|
||||
"gateway": "192.168.250.1",
|
||||
"netmask": "255.255.255.0",
|
||||
"port": 9600
|
||||
},
|
||||
"protocol_type": 5,
|
||||
"read_period": 100,
|
||||
"read_item_list": [
|
||||
{
|
||||
"value_name": "启动",
|
||||
"value_type": 1,
|
||||
"area_char": "W",
|
||||
"data_type": 0,
|
||||
"start_address": 100,
|
||||
"bit_address": 0,
|
||||
"data_length": 1
|
||||
},
|
||||
{
|
||||
"value_name": "停止",
|
||||
"value_type": 1,
|
||||
"area_char": "W",
|
||||
"data_type": 0,
|
||||
"start_address": 100,
|
||||
"bit_address": 1,
|
||||
"data_length": 1
|
||||
},
|
||||
{
|
||||
"value_name": "整型1",
|
||||
"value_type": 3,
|
||||
"area_char": "D",
|
||||
"data_type": 1,
|
||||
"start_address": 100,
|
||||
"bit_address": 0,
|
||||
"data_length": 1
|
||||
},
|
||||
{
|
||||
"value_name": "整型2",
|
||||
"value_type": 3,
|
||||
"area_char": "D",
|
||||
"data_type": 1,
|
||||
"start_address": 101,
|
||||
"bit_address": 0,
|
||||
"data_length": 1
|
||||
}
|
||||
]
|
||||
}
|
|
@ -19,9 +19,6 @@
|
|||
*/
|
||||
|
||||
#include <control.h>
|
||||
|
||||
extern int Adapter4GActive(void);
|
||||
|
||||
void ControlM241Test(void)
|
||||
{
|
||||
int i, j = 0;
|
||||
|
|
|
@ -15,9 +15,6 @@
|
|||
* @brief PLC SIEMENS S7-1200 app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022.9.27
|
||||
* @date 2023.3.27
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -15,9 +15,6 @@
|
|||
* @brief PLC SIEMENS S7-1500 app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022.9.27
|
||||
* @date 2023.3.27
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -11,13 +11,10 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file siemens_s7_200_smart.c
|
||||
* @brief PLC SIEMENS S7-200 SMART app
|
||||
* @file siemens_s7_200 smart.c
|
||||
* @brief PLC SIEMENS S7-200 smart app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022.9.27
|
||||
* @date 2023.3.27
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -15,9 +15,6 @@
|
|||
* @brief PLC SIEMENS S7-300 app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022.9.27
|
||||
* @date 2023.3.27
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
{
|
||||
"device_id": 1,
|
||||
"device_name": "打包机",
|
||||
"communication_type": 0,
|
||||
"socket_config": {
|
||||
"plc_ip": "192.168.250.2",
|
||||
"local_ip": "192.168.250.233",
|
||||
"gateway": "192.168.250.252",
|
||||
"netmask": "255.255.255.0",
|
||||
"port": 102
|
||||
},
|
||||
"protocol_type": 1,
|
||||
"read_period": 100,
|
||||
"read_item_list": [
|
||||
{
|
||||
"value_name": "浮点数",
|
||||
"value_type": 9,
|
||||
"area": "DB",
|
||||
"wordlen": "Real",
|
||||
"db_number": 18,
|
||||
"start": 18,
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"value_name": "整型1",
|
||||
"value_type": 3,
|
||||
"area": "M",
|
||||
"wordlen": "WORD",
|
||||
"db_number": 1,
|
||||
"start": 100,
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"value_name": "整型2",
|
||||
"value_type": 3,
|
||||
"area": "M",
|
||||
"wordlen": "WORD",
|
||||
"db_number": 1,
|
||||
"start": 102,
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"value_name": "布尔1",
|
||||
"value_type": 1,
|
||||
"area": "Q",
|
||||
"wordlen": "Bit",
|
||||
"db_number": 1,
|
||||
"start": 1,
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"value_name": "布尔2",
|
||||
"value_type": 1,
|
||||
"area": "Q",
|
||||
"wordlen": "Bit",
|
||||
"db_number": 1,
|
||||
"start": 2,
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"value_name": "布尔3",
|
||||
"value_type": 1,
|
||||
"area": "Q",
|
||||
"wordlen": "Bit",
|
||||
"db_number": 1,
|
||||
"start": 3,
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"value_name": "布尔4",
|
||||
"value_type": 1,
|
||||
"area": "Q",
|
||||
"wordlen": "Bit",
|
||||
"db_number": 1,
|
||||
"start": 4,
|
||||
"amount": 1
|
||||
},
|
||||
{
|
||||
"value_name": "整型3",
|
||||
"value_type": 3,
|
||||
"area": "DB",
|
||||
"wordlen": "Word",
|
||||
"db_number": 18,
|
||||
"start": 2,
|
||||
"amount": 1
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,184 @@
|
|||
{
|
||||
"device_id": 1,
|
||||
"device_name": "OP01",
|
||||
"communication_type": 0,
|
||||
"socket_config": {
|
||||
"plc_ip": "192.168.250.32",
|
||||
"local_ip": "192.168.250.233",
|
||||
"gateway": "192.168.250.1",
|
||||
"netmask": "255.255.255.0",
|
||||
"port": 502
|
||||
},
|
||||
"protocol_type": 2,
|
||||
"read_period": 100,
|
||||
"read_item_list": [
|
||||
{
|
||||
"value_name": "M19",
|
||||
"value_type": 1,
|
||||
"function_code": 5,
|
||||
"start_address": 19,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M20",
|
||||
"value_type": 1,
|
||||
"function_code": 5,
|
||||
"start_address":20,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D500",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 500,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D501",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 501,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D502",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 502,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D503",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 503,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D504",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 504,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M22",
|
||||
"value_type": 1,
|
||||
"function_code": 5,
|
||||
"start_address": 22,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D505",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 505,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D506",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 506,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D507",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 507,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D508",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 508,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D509",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 509,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D510",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 510,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D511",
|
||||
"value_type": 4,
|
||||
"function_code": 6,
|
||||
"start_address": 511,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M23",
|
||||
"value_type": 1,
|
||||
"function_code": 5,
|
||||
"start_address": 23,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M24",
|
||||
"value_type": 1,
|
||||
"function_code": 5,
|
||||
"start_address":24,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M25",
|
||||
"value_type": 1,
|
||||
"function_code": 5,
|
||||
"start_address": 25,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M26",
|
||||
"value_type": 1,
|
||||
"function_code": 5,
|
||||
"start_address": 26,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M29",
|
||||
"value_type": 1,
|
||||
"function_code": 5,
|
||||
"start_address": 29,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D513",
|
||||
"value_type": 4,
|
||||
"function_code": 3,
|
||||
"start_address": 513,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "D515",
|
||||
"value_type": 4,
|
||||
"function_code": 3,
|
||||
"start_address": 515,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M27",
|
||||
"value_type": 1,
|
||||
"function_code": 1,
|
||||
"start_address":27,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "M28",
|
||||
"value_type": 1,
|
||||
"function_code": 1,
|
||||
"start_address":28,
|
||||
"quantity": 1
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
{
|
||||
"device_id": 255,
|
||||
"device_name": "GJ2",
|
||||
"communication_type": 0,
|
||||
"socket_config": {
|
||||
"plc_ip": "192.168.250.45",
|
||||
"local_ip": "192.168.250.233",
|
||||
"gateway": "192.168.250.1",
|
||||
"netmask": "255.255.255.0",
|
||||
"port": 502
|
||||
},
|
||||
"protocol_type": 2,
|
||||
"read_period": 100,
|
||||
"read_item_list": [
|
||||
{
|
||||
"value_name": "QX40.0",
|
||||
"value_type": 1,
|
||||
"function_code": 1,
|
||||
"start_address":0,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "QX40.1",
|
||||
"value_type": 1,
|
||||
"function_code": 1,
|
||||
"start_address": 1,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "QX40.2",
|
||||
"value_type": 1,
|
||||
"function_code": 1,
|
||||
"start_address": 2,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "QX40.3",
|
||||
"value_type": 1,
|
||||
"function_code": 1,
|
||||
"start_address":3,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "QX40.4",
|
||||
"value_type": 1,
|
||||
"function_code": 1,
|
||||
"start_address": 4,
|
||||
"quantity": 1
|
||||
},
|
||||
{
|
||||
"value_name": "QW0",
|
||||
"value_type": 3,
|
||||
"function_code": 3,
|
||||
"start_address": 0,
|
||||
"quantity": 2
|
||||
},
|
||||
{
|
||||
"value_name": "QW1",
|
||||
"value_type": 3,
|
||||
"function_code": 3,
|
||||
"start_address": 1,
|
||||
"quantity": 2
|
||||
},
|
||||
{
|
||||
"value_name": "QW2",
|
||||
"value_type": 3,
|
||||
"function_code": 3,
|
||||
"start_address": 2,
|
||||
"quantity": 2
|
||||
},
|
||||
{
|
||||
"value_name": "QW3",
|
||||
"value_type": 3,
|
||||
"function_code": 3,
|
||||
"start_address": 3,
|
||||
"quantity": 2
|
||||
},
|
||||
{
|
||||
"value_name": "QW10",
|
||||
"value_type": 3,
|
||||
"function_code": 4,
|
||||
"start_address": 0,
|
||||
"quantity": 2
|
||||
},
|
||||
{
|
||||
"value_name": "QW11",
|
||||
"value_type": 3,
|
||||
"function_code": 4,
|
||||
"start_address": 1,
|
||||
"quantity": 2
|
||||
}
|
||||
]
|
||||
}
|
|
@ -11,10 +11,41 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file mitsubishi_fx3u.c
|
||||
* @brief PLC MITSUBISHI FX3U app
|
||||
* @file xdh-60t4-e.c
|
||||
* @brief PLC XINJE XDH-60T4-E app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2023.2.1
|
||||
* @date 2023.4.5
|
||||
*/
|
||||
|
||||
#include <control.h>
|
||||
void ControlXDHTest(void)
|
||||
{
|
||||
int i, j = 0;
|
||||
int read_data_length = 0;
|
||||
uint8_t read_data[128] = {0};
|
||||
ControlProtocolType modbus_tcp_protocol = ControlProtocolFind();
|
||||
if (NULL == modbus_tcp_protocol) {
|
||||
printf("%s get modbus tcp protocol %p failed\n", __func__, modbus_tcp_protocol);
|
||||
return;
|
||||
}
|
||||
printf("%s get modbus tcp protocol %p successfull\n", __func__, modbus_tcp_protocol);
|
||||
|
||||
if (CONTROL_REGISTERED == modbus_tcp_protocol->protocol_status) {
|
||||
ControlProtocolOpen(modbus_tcp_protocol);
|
||||
for (;;) {
|
||||
read_data_length = ControlProtocolRead(modbus_tcp_protocol, read_data, sizeof(read_data));
|
||||
printf("%s read [%d] modbus tcp data %d using receipe file\n", __func__, i, read_data_length);
|
||||
if (read_data_length) {
|
||||
for (j = 0; j < read_data_length; j ++) {
|
||||
printf("j %d data 0x%x\n", j, read_data[j]);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
memset(read_data, 0, sizeof(read_data));
|
||||
PrivTaskDelay(10000);
|
||||
}
|
||||
//ControlProtocolClose(modbus_tcp_protocol);
|
||||
}
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(ControlXDHTest, Xinje Plc XDH_60T4_E Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
|
@ -11,9 +11,42 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file mitsubishi_fx3u.c
|
||||
* @brief PLC MITSUBISHI FX3U app
|
||||
* @file xsdh-60a32-e.c
|
||||
* @brief PLC XINJE XSDH-60A32-E app
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2023.3.1
|
||||
* @date 2023.3.28
|
||||
*/
|
||||
|
||||
|
||||
#include <control.h>
|
||||
void ControlXSDHTest(void)
|
||||
{
|
||||
int i, j = 0;
|
||||
int read_data_length = 0;
|
||||
uint8_t read_data[128] = {0};
|
||||
ControlProtocolType modbus_tcp_protocol = ControlProtocolFind();
|
||||
if (NULL == modbus_tcp_protocol) {
|
||||
printf("%s get modbus tcp protocol %p failed\n", __func__, modbus_tcp_protocol);
|
||||
return;
|
||||
}
|
||||
printf("%s get modbus tcp protocol %p successfull\n", __func__, modbus_tcp_protocol);
|
||||
|
||||
if (CONTROL_REGISTERED == modbus_tcp_protocol->protocol_status) {
|
||||
ControlProtocolOpen(modbus_tcp_protocol);
|
||||
for (;;) {
|
||||
read_data_length = ControlProtocolRead(modbus_tcp_protocol, read_data, sizeof(read_data));
|
||||
printf("%s read [%d] modbus tcp data %d using receipe file\n", __func__, i, read_data_length);
|
||||
if (read_data_length) {
|
||||
for (j = 0; j < read_data_length; j ++) {
|
||||
printf("j %d data 0x%x\n", j, read_data[j]);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
memset(read_data, 0, sizeof(read_data));
|
||||
PrivTaskDelay(10000);
|
||||
}
|
||||
//ControlProtocolClose(modbus_tcp_protocol);
|
||||
}
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(ControlXSDHTest, Xinje Plc XSDH_60A32_E Demo, PRIV_SHELL_CMD_MAIN_ATTR);
|
|
@ -27,7 +27,7 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MODBUS_TCP_UNIT_ID 0x01
|
||||
|
||||
#define MODBUS_TCP_READ_CMD_LENGTH 0x0C
|
||||
#define MODBUS_TCP_WRITE_SINGLE_CMD_LENGTH 0x0C
|
||||
|
||||
|
|
|
@ -225,7 +225,7 @@ static int ModbusTcpInitialDataInfo(ModbusTcpReadItem *p_read_item, uint16_t ind
|
|||
p_base_data_info->p_command[5] = 0x09;
|
||||
}
|
||||
|
||||
p_base_data_info->p_command[6] = MODBUS_TCP_UNIT_ID;
|
||||
p_base_data_info->p_command[6] = control_protocol->recipe->device_id;
|
||||
p_base_data_info->p_command[7] = function_code;
|
||||
p_base_data_info->p_command[8] = start_address >> 8;
|
||||
p_base_data_info->p_command[9] = start_address;
|
||||
|
|
|
@ -23,6 +23,7 @@ endif
|
|||
config CONTROL_PROTOCOL_S7
|
||||
bool "Using s7 control protocol"
|
||||
default n
|
||||
select CONTROL_USING_SOCKET
|
||||
if CONTROL_PROTOCOL_S7
|
||||
source "$APP_DIR/Framework/control/plc_protocol/s7/Kconfig"
|
||||
endif
|
||||
|
|
|
@ -329,7 +329,11 @@ void *ReceivePlcDataTask(void *parameter)
|
|||
/*read all variable item data, put them into circular_area*/
|
||||
if (i == control_protocol->recipe->read_item_count) {
|
||||
printf("%s get %d item %d length\n", __func__, i, data_length);
|
||||
// for(int i = 0; i < data_length; i++){
|
||||
// printf("%x\n",fins_data[i]);
|
||||
// }
|
||||
CircularAreaAppWrite(circular_area, fins_data, data_length, 0);
|
||||
printf("%s\n",fins_data);
|
||||
}
|
||||
|
||||
/*read data every single 'read_period' ms*/
|
||||
|
|
|
@ -0,0 +1,408 @@
|
|||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
#ifndef snap7_h
|
||||
#define snap7_h
|
||||
//---------------------------------------------------------------------------
|
||||
# include <stdint.h>
|
||||
# include <unistd.h>
|
||||
|
||||
//******************************************************************************
|
||||
// COMMON
|
||||
//******************************************************************************
|
||||
// Exact length types regardless of platform/processor
|
||||
typedef uint8_t byte;
|
||||
typedef uint16_t word;
|
||||
typedef uint32_t longword;
|
||||
typedef byte* pbyte;
|
||||
typedef word* pword;
|
||||
typedef uintptr_t S7Object; // multi platform/processor object reference
|
||||
// DON'T CONFUSE IT WITH AN OLE OBJECT, IT'S SIMPLY
|
||||
// AN INTEGER VALUE (32 OR 64 BIT) USED AS HANDLE.
|
||||
|
||||
#ifndef __cplusplus
|
||||
typedef struct
|
||||
{
|
||||
int tm_sec;
|
||||
int tm_min;
|
||||
int tm_hour;
|
||||
int tm_mday;
|
||||
int tm_mon;
|
||||
int tm_year;
|
||||
int tm_wday;
|
||||
int tm_yday;
|
||||
int tm_isdst;
|
||||
}tm;
|
||||
|
||||
typedef int bool;
|
||||
#define false 0;
|
||||
#define true 1;
|
||||
#endif
|
||||
|
||||
static const int errLibInvalidParam = -1;
|
||||
static const int errLibInvalidObject = -2;
|
||||
|
||||
// CPU status
|
||||
#define S7CpuStatusUnknown 0x00
|
||||
#define S7CpuStatusRun 0x08
|
||||
#define S7CpuStatusStop 0x04
|
||||
|
||||
// ISO Errors
|
||||
static const longword errIsoConnect = 0x00010000; // Connection error
|
||||
static const longword errIsoDisconnect = 0x00020000; // Disconnect error
|
||||
static const longword errIsoInvalidPDU = 0x00030000; // Bad format
|
||||
static const longword errIsoInvalidDataSize = 0x00040000; // Bad Datasize passed to send/recv buffer is invalid
|
||||
static const longword errIsoNullPointer = 0x00050000; // Null passed as pointer
|
||||
static const longword errIsoShortPacket = 0x00060000; // A short packet received
|
||||
static const longword errIsoTooManyFragments = 0x00070000; // Too many packets without EoT flag
|
||||
static const longword errIsoPduOverflow = 0x00080000; // The sum of fragments data exceded maximum packet size
|
||||
static const longword errIsoSendPacket = 0x00090000; // An error occurred during send
|
||||
static const longword errIsoRecvPacket = 0x000A0000; // An error occurred during recv
|
||||
static const longword errIsoInvalidParams = 0x000B0000; // Invalid TSAP params
|
||||
static const longword errIsoResvd_1 = 0x000C0000; // Unassigned
|
||||
static const longword errIsoResvd_2 = 0x000D0000; // Unassigned
|
||||
static const longword errIsoResvd_3 = 0x000E0000; // Unassigned
|
||||
static const longword errIsoResvd_4 = 0x000F0000; // Unassigned
|
||||
|
||||
// Tag Struct
|
||||
typedef struct {
|
||||
int Area;
|
||||
int DBNumber;
|
||||
int Start;
|
||||
int Size;
|
||||
int WordLen;
|
||||
}TS7Tag, * PS7Tag;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// PARAMS LIST
|
||||
//------------------------------------------------------------------------------
|
||||
static const int p_u16_LocalPort = 1;
|
||||
static const int p_u16_RemotePort = 2;
|
||||
static const int p_i32_PingTimeout = 3;
|
||||
static const int p_i32_SendTimeout = 4;
|
||||
static const int p_i32_RecvTimeout = 5;
|
||||
static const int p_i32_WorkInterval = 6;
|
||||
static const int p_u16_SrcRef = 7;
|
||||
static const int p_u16_DstRef = 8;
|
||||
static const int p_u16_SrcTSap = 9;
|
||||
static const int p_i32_PDURequest = 10;
|
||||
static const int p_i32_MaxClients = 11;
|
||||
static const int p_i32_BSendTimeout = 12;
|
||||
static const int p_i32_BRecvTimeout = 13;
|
||||
static const int p_u32_RecoveryTime = 14;
|
||||
static const int p_u32_KeepAliveTime = 15;
|
||||
|
||||
// Client/Partner Job status
|
||||
static const int JobComplete = 0;
|
||||
static const int JobPending = 1;
|
||||
|
||||
//******************************************************************************
|
||||
// CLIENT
|
||||
//******************************************************************************
|
||||
|
||||
// Error codes
|
||||
static const longword errNegotiatingPDU = 0x00100000;
|
||||
static const longword errCliInvalidParams = 0x00200000;
|
||||
static const longword errCliJobPending = 0x00300000;
|
||||
static const longword errCliTooManyItems = 0x00400000;
|
||||
static const longword errCliInvalidWordLen = 0x00500000;
|
||||
static const longword errCliPartialDataWritten = 0x00600000;
|
||||
static const longword errCliSizeOverPDU = 0x00700000;
|
||||
static const longword errCliInvalidPlcAnswer = 0x00800000;
|
||||
static const longword errCliAddressOutOfRange = 0x00900000;
|
||||
static const longword errCliInvalidTransportSize = 0x00A00000;
|
||||
static const longword errCliWriteDataSizeMismatch = 0x00B00000;
|
||||
static const longword errCliItemNotAvailable = 0x00C00000;
|
||||
static const longword errCliInvalidValue = 0x00D00000;
|
||||
static const longword errCliCannotStartPLC = 0x00E00000;
|
||||
static const longword errCliAlreadyRun = 0x00F00000;
|
||||
static const longword errCliCannotStopPLC = 0x01000000;
|
||||
static const longword errCliCannotCopyRamToRom = 0x01100000;
|
||||
static const longword errCliCannotCompress = 0x01200000;
|
||||
static const longword errCliAlreadyStop = 0x01300000;
|
||||
static const longword errCliFunNotAvailable = 0x01400000;
|
||||
static const longword errCliUploadSequenceFailed = 0x01500000;
|
||||
static const longword errCliInvalidDataSizeRecvd = 0x01600000;
|
||||
static const longword errCliInvalidBlockType = 0x01700000;
|
||||
static const longword errCliInvalidBlockNumber = 0x01800000;
|
||||
static const longword errCliInvalidBlockSize = 0x01900000;
|
||||
static const longword errCliDownloadSequenceFailed = 0x01A00000;
|
||||
static const longword errCliInsertRefused = 0x01B00000;
|
||||
static const longword errCliDeleteRefused = 0x01C00000;
|
||||
static const longword errCliNeedPassword = 0x01D00000;
|
||||
static const longword errCliInvalidPassword = 0x01E00000;
|
||||
static const longword errCliNoPasswordToSetOrClear = 0x01F00000;
|
||||
static const longword errCliJobTimeout = 0x02000000;
|
||||
static const longword errCliPartialDataRead = 0x02100000;
|
||||
static const longword errCliBufferTooSmall = 0x02200000;
|
||||
static const longword errCliFunctionRefused = 0x02300000;
|
||||
static const longword errCliDestroying = 0x02400000;
|
||||
static const longword errCliInvalidParamNumber = 0x02500000;
|
||||
static const longword errCliCannotChangeParam = 0x02600000;
|
||||
|
||||
static const int MaxVars = 20; // Max vars that can be transferred with MultiRead/MultiWrite
|
||||
|
||||
// Client Connection Type
|
||||
static const word CONNTYPE_PG = 0x0001; // Connect to the PLC as a PG
|
||||
static const word CONNTYPE_OP = 0x0002; // Connect to the PLC as an OP
|
||||
static const word CONNTYPE_BASIC = 0x0003; // Basic connection
|
||||
|
||||
// Area ID
|
||||
static const byte S7AreaPE = 0x81;
|
||||
static const byte S7AreaPA = 0x82;
|
||||
static const byte S7AreaMK = 0x83;
|
||||
static const byte S7AreaDB = 0x84;
|
||||
static const byte S7AreaCT = 0x1C;
|
||||
static const byte S7AreaTM = 0x1D;
|
||||
|
||||
// Word Length
|
||||
static const int S7WLBit = 0x01;
|
||||
static const int S7WLByte = 0x02;
|
||||
static const int S7WLWord = 0x04;
|
||||
static const int S7WLDWord = 0x06;
|
||||
static const int S7WLReal = 0x08;
|
||||
static const int S7WLCounter = 0x1C;
|
||||
static const int S7WLTimer = 0x1D;
|
||||
|
||||
// Block type
|
||||
static const byte Block_OB = 0x38;
|
||||
static const byte Block_DB = 0x41;
|
||||
static const byte Block_SDB = 0x42;
|
||||
static const byte Block_FC = 0x43;
|
||||
static const byte Block_SFC = 0x44;
|
||||
static const byte Block_FB = 0x45;
|
||||
static const byte Block_SFB = 0x46;
|
||||
|
||||
// Sub Block Type
|
||||
static const byte SubBlk_OB = 0x08;
|
||||
static const byte SubBlk_DB = 0x0A;
|
||||
static const byte SubBlk_SDB = 0x0B;
|
||||
static const byte SubBlk_FC = 0x0C;
|
||||
static const byte SubBlk_SFC = 0x0D;
|
||||
static const byte SubBlk_FB = 0x0E;
|
||||
static const byte SubBlk_SFB = 0x0F;
|
||||
|
||||
// Block languages
|
||||
static const byte BlockLangAWL = 0x01;
|
||||
static const byte BlockLangKOP = 0x02;
|
||||
static const byte BlockLangFUP = 0x03;
|
||||
static const byte BlockLangSCL = 0x04;
|
||||
static const byte BlockLangDB = 0x05;
|
||||
static const byte BlockLangGRAPH = 0x06;
|
||||
|
||||
// Read/Write Multivars
|
||||
typedef struct {
|
||||
int Area;
|
||||
int WordLen;
|
||||
int Result;
|
||||
int DBNumber;
|
||||
int Start;
|
||||
int Amount;
|
||||
void* pdata;
|
||||
} TS7DataItem, * PS7DataItem;
|
||||
|
||||
//typedef int TS7ResultItems[MaxVars];
|
||||
//typedef TS7ResultItems *PS7ResultItems;
|
||||
|
||||
// List Blocks
|
||||
typedef struct {
|
||||
int OBCount;
|
||||
int FBCount;
|
||||
int FCCount;
|
||||
int SFBCount;
|
||||
int SFCCount;
|
||||
int DBCount;
|
||||
int SDBCount;
|
||||
} TS7BlocksList, * PS7BlocksList;
|
||||
|
||||
// Blocks info
|
||||
typedef struct {
|
||||
int BlkType; // Block Type (OB, DB)
|
||||
int BlkNumber; // Block number
|
||||
int BlkLang; // Block Language
|
||||
int BlkFlags; // Block flags
|
||||
int MC7Size; // The real size in bytes
|
||||
int LoadSize; // Load memory size
|
||||
int LocalData; // Local data
|
||||
int SBBLength; // SBB Length
|
||||
int CheckSum; // Checksum
|
||||
int Version; // Block version
|
||||
// Chars info
|
||||
char CodeDate[11]; // Code date
|
||||
char IntfDate[11]; // Interface date
|
||||
char Author[9]; // Author
|
||||
char Family[9]; // Family
|
||||
char Header[9]; // Header
|
||||
} TS7BlockInfo, * PS7BlockInfo;
|
||||
|
||||
typedef word TS7BlocksOfType[0x2000];
|
||||
typedef TS7BlocksOfType* PS7BlocksOfType;
|
||||
|
||||
// Order code
|
||||
typedef struct {
|
||||
char Code[21];
|
||||
byte V1;
|
||||
byte V2;
|
||||
byte V3;
|
||||
} TS7OrderCode, * PS7OrderCode;
|
||||
|
||||
// CPU Info
|
||||
typedef struct {
|
||||
char ModuleTypeName[33];
|
||||
char SerialNumber[25];
|
||||
char ASName[25];
|
||||
char Copyright[27];
|
||||
char ModuleName[25];
|
||||
} TS7CpuInfo, * PS7CpuInfo;
|
||||
|
||||
// CP Info
|
||||
typedef struct {
|
||||
int MaxPduLengt;
|
||||
int MaxConnections;
|
||||
int MaxMpiRate;
|
||||
int MaxBusRate;
|
||||
} TS7CpInfo, * PS7CpInfo;
|
||||
|
||||
// See §33.1 of "System Software for S7-300/400 System and Standard Functions"
|
||||
// and see SFC51 description too
|
||||
typedef struct {
|
||||
word LENTHDR;
|
||||
word N_DR;
|
||||
} SZL_HEADER, * PSZL_HEADER;
|
||||
|
||||
typedef struct {
|
||||
SZL_HEADER Header;
|
||||
byte Data[0x4000 - 4];
|
||||
} TS7SZL, * PS7SZL;
|
||||
|
||||
// SZL List of available SZL IDs : same as SZL but List items are big-endian adjusted
|
||||
typedef struct {
|
||||
SZL_HEADER Header;
|
||||
word List[0x2000 - 2];
|
||||
} TS7SZLList, * PS7SZLList;
|
||||
|
||||
// See §33.19 of "System Software for S7-300/400 System and Standard Functions"
|
||||
typedef struct {
|
||||
word sch_schal;
|
||||
word sch_par;
|
||||
word sch_rel;
|
||||
word bart_sch;
|
||||
word anl_sch;
|
||||
} TS7Protection, * PS7Protection;
|
||||
|
||||
// Client completion callback
|
||||
typedef void (*pfn_CliCompletion) (void* usrPtr, int opCode, int opResult);
|
||||
//------------------------------------------------------------------------------
|
||||
// Import prototypes
|
||||
//------------------------------------------------------------------------------
|
||||
S7Object Cli_Create();
|
||||
void Cli_Destroy(S7Object* Client);
|
||||
int Cli_ConnectTo(S7Object Client, const char* Address, int Rack, int Slot);
|
||||
int Cli_SetConnectionParams(S7Object Client, const char* Address, word LocalTSAP, word RemoteTSAP);
|
||||
int Cli_SetConnectionType(S7Object Client, word ConnectionType);
|
||||
int Cli_Connect(S7Object Client);
|
||||
int Cli_Disconnect(S7Object Client);
|
||||
int Cli_GetParam(S7Object Client, int ParamNumber, void* pValue);
|
||||
int Cli_SetParam(S7Object Client, int ParamNumber, void* pValue);
|
||||
int Cli_SetAsCallback(S7Object Client, pfn_CliCompletion pCompletion, void* usrPtr);
|
||||
// Data I/O main functions
|
||||
int Cli_ReadArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void* pUsrData);
|
||||
int Cli_WriteArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void* pUsrData);
|
||||
int Cli_ReadMultiVars(S7Object Client, PS7DataItem Item, int ItemsCount);
|
||||
int Cli_WriteMultiVars(S7Object Client, PS7DataItem Item, int ItemsCount);
|
||||
// Data I/O Lean functions
|
||||
int Cli_DBRead(S7Object Client, int DBNumber, int Start, int Size, void* pUsrData);
|
||||
int Cli_DBWrite(S7Object Client, int DBNumber, int Start, int Size, void* pUsrData);
|
||||
int Cli_MBRead(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_MBWrite(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_EBRead(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_EBWrite(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_ABRead(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_ABWrite(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_TMRead(S7Object Client, int Start, int Amount, void* pUsrData);
|
||||
int Cli_TMWrite(S7Object Client, int Start, int Amount, void* pUsrData);
|
||||
int Cli_CTRead(S7Object Client, int Start, int Amount, void* pUsrData);
|
||||
int Cli_CTWrite(S7Object Client, int Start, int Amount, void* pUsrData);
|
||||
// Directory functions
|
||||
int Cli_ListBlocks(S7Object Client, TS7BlocksList* pUsrData);
|
||||
int Cli_GetAgBlockInfo(S7Object Client, int BlockType, int BlockNum, TS7BlockInfo* pUsrData);
|
||||
int Cli_GetPgBlockInfo(S7Object Client, void* pBlock, TS7BlockInfo* pUsrData, int Size);
|
||||
int Cli_ListBlocksOfType(S7Object Client, int BlockType, TS7BlocksOfType* pUsrData, int* ItemsCount);
|
||||
// Blocks functions
|
||||
int Cli_Upload(S7Object Client, int BlockType, int BlockNum, void* pUsrData, int* Size);
|
||||
int Cli_FullUpload(S7Object Client, int BlockType, int BlockNum, void* pUsrData, int* Size);
|
||||
int Cli_Download(S7Object Client, int BlockNum, void* pUsrData, int Size);
|
||||
int Cli_Delete(S7Object Client, int BlockType, int BlockNum);
|
||||
int Cli_DBGet(S7Object Client, int DBNumber, void* pUsrData, int* Size);
|
||||
int Cli_DBFill(S7Object Client, int DBNumber, int FillChar);
|
||||
// Date/Time functions
|
||||
int Cli_GetPlcDateTime(S7Object Client, tm* DateTime);
|
||||
int Cli_SetPlcDateTime(S7Object Client, tm* DateTime);
|
||||
int Cli_SetPlcSystemDateTime(S7Object Client);
|
||||
// System Info functions
|
||||
int Cli_GetOrderCode(S7Object Client, TS7OrderCode* pUsrData);
|
||||
int Cli_GetCpuInfo(S7Object Client, TS7CpuInfo* pUsrData);
|
||||
int Cli_GetCpInfo(S7Object Client, TS7CpInfo* pUsrData);
|
||||
int Cli_ReadSZL(S7Object Client, int ID, int Index, TS7SZL* pUsrData, int* Size);
|
||||
int Cli_ReadSZLList(S7Object Client, TS7SZLList* pUsrData, int* ItemsCount);
|
||||
// Control functions
|
||||
int Cli_PlcHotStart(S7Object Client);
|
||||
int Cli_PlcColdStart(S7Object Client);
|
||||
int Cli_PlcStop(S7Object Client);
|
||||
int Cli_CopyRamToRom(S7Object Client, int Timeout);
|
||||
int Cli_Compress(S7Object Client, int Timeout);
|
||||
int Cli_GetPlcStatus(S7Object Client, int* Status);
|
||||
// Security functions
|
||||
int Cli_GetProtection(S7Object Client, TS7Protection* pUsrData);
|
||||
int Cli_SetSessionPassword(S7Object Client, char* Password);
|
||||
int Cli_ClearSessionPassword(S7Object Client);
|
||||
// Low level
|
||||
int Cli_IsoExchangeBuffer(S7Object Client, void* pUsrData, int* Size);
|
||||
// Misc
|
||||
int Cli_GetExecTime(S7Object Client, int* Time);
|
||||
int Cli_GetLastError(S7Object Client, int* LastError);
|
||||
int Cli_GetPduLength(S7Object Client, int* Requested, int* Negotiated);
|
||||
int Cli_ErrorText(int Error, char* Text, int TextLen);
|
||||
// 1.1.0
|
||||
int Cli_GetConnected(S7Object Client, int* Connected);
|
||||
//------------------------------------------------------------------------------
|
||||
// Async functions
|
||||
//------------------------------------------------------------------------------
|
||||
int Cli_AsReadArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void* pUsrData);
|
||||
int Cli_AsWriteArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void* pUsrData);
|
||||
int Cli_AsDBRead(S7Object Client, int DBNumber, int Start, int Size, void* pUsrData);
|
||||
int Cli_AsDBWrite(S7Object Client, int DBNumber, int Start, int Size, void* pUsrData);
|
||||
int Cli_AsMBRead(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_AsMBWrite(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_AsEBRead(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_AsEBWrite(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_AsABRead(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_AsABWrite(S7Object Client, int Start, int Size, void* pUsrData);
|
||||
int Cli_AsTMRead(S7Object Client, int Start, int Amount, void* pUsrData);
|
||||
int Cli_AsTMWrite(S7Object Client, int Start, int Amount, void* pUsrData);
|
||||
int Cli_AsCTRead(S7Object Client, int Start, int Amount, void* pUsrData);
|
||||
int Cli_AsCTWrite(S7Object Client, int Start, int Amount, void* pUsrData);
|
||||
int Cli_AsListBlocksOfType(S7Object Client, int BlockType, TS7BlocksOfType* pUsrData, int* ItemsCount);
|
||||
int Cli_AsReadSZL(S7Object Client, int ID, int Index, TS7SZL* pUsrData, int* Size);
|
||||
int Cli_AsReadSZLList(S7Object Client, TS7SZLList* pUsrData, int* ItemsCount);
|
||||
int Cli_AsUpload(S7Object Client, int BlockType, int BlockNum, void* pUsrData, int* Size);
|
||||
int Cli_AsFullUpload(S7Object Client, int BlockType, int BlockNum, void* pUsrData, int* Size);
|
||||
int Cli_AsDownload(S7Object Client, int BlockNum, void* pUsrData, int Size);
|
||||
int Cli_AsCopyRamToRom(S7Object Client, int Timeout);
|
||||
int Cli_AsCompress(S7Object Client, int Timeout);
|
||||
int Cli_AsDBGet(S7Object Client, int DBNumber, void* pUsrData, int* Size);
|
||||
int Cli_AsDBFill(S7Object Client, int DBNumber, int FillChar);
|
||||
int Cli_CheckAsCompletion(S7Object Client, int* opResult);
|
||||
int Cli_WaitAsCompletion(S7Object Client, int Timeout);
|
||||
|
||||
|
||||
#endif // snap7_h
|
|
@ -17,3 +17,26 @@
|
|||
* @author AIIT XUOS Lab
|
||||
* @date 2022-10-08
|
||||
*/
|
||||
|
||||
#ifndef S7_H
|
||||
#define S7_H
|
||||
|
||||
#include <control_def.h>
|
||||
#include "libs7.h"
|
||||
|
||||
#define BASE_PLC_RECV_BUFF_SIZE 1024
|
||||
|
||||
ControlProtocolType control_protocol;
|
||||
|
||||
int8_t ReadPlcDataByRecipe(struct ControlRecipe *p_recipe);
|
||||
void voidpush(uint8_t *datastack,uint8_t* args,uint16_t length);
|
||||
static uint8_t GetUniformValueTypeMemorySize(UniformValueType uniform_value_type);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TS7DataItem data_info;
|
||||
UniformValueType value_type;
|
||||
char value_name[20];
|
||||
}S7ReadItem;
|
||||
|
||||
#endif
|
|
@ -1,2 +0,0 @@
|
|||
|
||||
|
|
@ -1,4 +1,2 @@
|
|||
SRC_FILES :=
|
||||
|
||||
SRC_FILES := libs7.a s7.c
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,223 @@
|
|||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file s7.c
|
||||
* @brief plc protocol s7
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2023-4-14
|
||||
*/
|
||||
|
||||
#include <s7.h>
|
||||
|
||||
sem_t union_plc_data_updata_sem;
|
||||
pthread_mutex_t union_plc_data_read_mutex;
|
||||
static BasicSocketPlc plc_socket = {0};
|
||||
void KPrintf(const char *fmt, ...);
|
||||
#define BASIC_PLC_PRINTF KPrintf
|
||||
|
||||
/**
|
||||
* @description: Printf Hex Number List
|
||||
* @param name - number list name
|
||||
* @param number_list - number list pointer
|
||||
* @param length - numner list length
|
||||
* @return
|
||||
*/
|
||||
void PrintHexNumberList(char name[4], uint8_t* number_list, uint16_t length)
|
||||
{
|
||||
BASIC_PLC_PRINTF("\n******************%4s****************\n", name);
|
||||
for (int32_t i = 0;i < length;i++) {
|
||||
BASIC_PLC_PRINTF("%03x ", number_list[i]);
|
||||
}
|
||||
BASIC_PLC_PRINTF("\n**************************************\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: S7 Receive Plc Data Task
|
||||
* @param parameter - parameter pointer
|
||||
* @return
|
||||
*/
|
||||
void *ReceivePlcDataTask(void *parameter)
|
||||
{
|
||||
printf("%s %d\n", __func__, __LINE__);
|
||||
ReadPlcDataByRecipe(control_protocol->recipe);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: S7 Protocol Open
|
||||
* @param control_protocol - control protocol pointer
|
||||
* @return success : 0 error
|
||||
*/
|
||||
int S7Open(struct ControlProtocol *control_protocol)
|
||||
{
|
||||
ControlProtocolOpenDef(control_protocol);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: S7 Protocol Close
|
||||
* @param control_protocol - control protocol pointer
|
||||
* @return success : 0 error
|
||||
*/
|
||||
int S7Close(struct ControlProtocol *control_protocol)
|
||||
{
|
||||
#ifdef CONTROL_USING_SOCKET
|
||||
ControlDisconnectSocket(&plc_socket);
|
||||
#endif
|
||||
|
||||
ControlProtocolCloseDef();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: S7 Protocol Read Data
|
||||
* @param control_protocol - control protocol pointer
|
||||
* @param buf - read data buffer pointer
|
||||
* @param len - read data length
|
||||
* @return success : data length error : 0
|
||||
*/
|
||||
int S7Read(struct ControlProtocol *control_protocol, void *buf, size_t len)
|
||||
{
|
||||
struct CircularAreaApp *circular_area = (struct CircularAreaApp *)control_protocol->args;
|
||||
return CircularAreaAppRead(circular_area, buf, len);
|
||||
}
|
||||
|
||||
static struct ControlDone s7_protocol_done =
|
||||
{
|
||||
._open = S7Open,
|
||||
._close = S7Close,
|
||||
._read = S7Read,
|
||||
._write = NULL,
|
||||
._ioctl = NULL,
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: S7 Protocol Value Type
|
||||
* @param uniform_value_type - uniform value type
|
||||
* @return success : 0-8 error : -1
|
||||
*/
|
||||
static uint8_t GetUniformValueTypeMemorySize(UniformValueType uniform_value_type)
|
||||
{
|
||||
if (uniform_value_type == UNIFORM_BOOL || uniform_value_type == UNIFORM_INT8 || uniform_value_type == UNIFORM_UINT8)
|
||||
return 1;
|
||||
if (uniform_value_type == UNIFORM_INT16 || uniform_value_type == UNIFORM_UINT16)
|
||||
return 2;
|
||||
if (uniform_value_type == UNIFORM_INT32 || uniform_value_type == UNIFORM_UINT32 || uniform_value_type == UNIFORM_FLOAT)
|
||||
return 4;
|
||||
if (uniform_value_type == UNIFORM_DOUBLE)
|
||||
return 8;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Push Data Onto a Stack One By One
|
||||
* @param datastack - data stack pointer
|
||||
* @param args - data pointer
|
||||
* @param length - data length
|
||||
* @return
|
||||
*/
|
||||
void PushDataIntoStack(uint8_t *datastack,uint8_t* args,uint16_t length)
|
||||
{
|
||||
static int index = 8;
|
||||
for(int i =0; i<length;i++)
|
||||
{
|
||||
datastack[index] = args[i];
|
||||
//printf("DEBUG:%d%4X\n",__LINE__,datapush[8+index]);
|
||||
index++;
|
||||
//printf("DEBUG:%d %d %d\n",__LINE__,index, length);
|
||||
if(index >= control_protocol->recipe->protocol_data.data_length){
|
||||
index = 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Read PLC Data By Recipe
|
||||
* @param p_recipe - recipe pointer
|
||||
* @return success : 0 error : -1
|
||||
*/
|
||||
int8_t ReadPlcDataByRecipe(struct ControlRecipe *p_recipe)
|
||||
{
|
||||
uint16_t data_length = 0;
|
||||
uint8_t *s7_data;
|
||||
data_length = control_protocol->recipe->protocol_data.data_length;
|
||||
s7_data = control_protocol->recipe->protocol_data.data;
|
||||
struct CircularAreaApp *circular_area = (struct CircularAreaApp *)control_protocol->args;
|
||||
BasicSocketPlc base_socket_plc = {0};
|
||||
S7Object s7_plc = {0};
|
||||
char plc_ip_string[15] = {0};
|
||||
s7_plc = Cli_Create();
|
||||
sprintf(plc_ip_string, "%u.%u.%u.%u",
|
||||
p_recipe->socket_config.plc_ip[0],
|
||||
p_recipe->socket_config.plc_ip[1],
|
||||
p_recipe->socket_config.plc_ip[2],
|
||||
p_recipe->socket_config.plc_ip[3]);
|
||||
int16_t read_item_count = p_recipe->read_item_count;
|
||||
uint8_t* p_read_item = (uint8_t*)(p_recipe->read_item);
|
||||
while (1) {
|
||||
int8_t error = 0;
|
||||
while (!error) {
|
||||
uint16_t i = 0;
|
||||
for (i; i < read_item_count; i++) {
|
||||
int is_connected = 0;
|
||||
Cli_GetConnected(s7_plc, &is_connected);
|
||||
while (!is_connected) {
|
||||
if (Cli_ConnectTo(s7_plc, plc_ip_string, 0, 1) != 0) {
|
||||
MdelayKTask(1000);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
TS7DataItem data_info = ((S7ReadItem*)p_read_item + i)->data_info;
|
||||
Cli_ReadMultiVars(s7_plc, &data_info, 1);
|
||||
uint16_t Size = GetUniformValueTypeMemorySize(((S7ReadItem*)p_read_item + i)->value_type);
|
||||
PrintHexNumberList("s7 recv", data_info.pdata,Size);
|
||||
PushDataIntoStack(s7_data,data_info.pdata,Size);
|
||||
MdelayKTask(100);
|
||||
}
|
||||
/*read all variable item data, put them into circular_area*/
|
||||
if (i == read_item_count) {
|
||||
printf("%s get %d item %d length\n", __func__, i, data_length);
|
||||
// for(int j = 0; j < data_length; j++)
|
||||
// {
|
||||
// printf("DEBUG:%X %d\n",s7_data[j],__LINE__);
|
||||
// }
|
||||
CircularAreaAppWrite(circular_area, s7_data, data_length, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: S7 Protocol Init
|
||||
* @param p_recipe - recipe pointer
|
||||
* @return success : 0 error : -1
|
||||
*/
|
||||
int S7ProtocolInit(struct ControlRecipe *p_recipe)
|
||||
{
|
||||
p_recipe->read_item = PrivMalloc(sizeof(S7ReadItem) * p_recipe->read_item_count);
|
||||
if (NULL == p_recipe->read_item) {
|
||||
PrivFree(p_recipe->read_item);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(p_recipe->read_item, 0, sizeof(S7ReadItem));
|
||||
|
||||
// p_recipe->ControlProtocolFormatCmd = S7ProtocolFormatCmd;
|
||||
|
||||
p_recipe->done = &s7_protocol_done;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -20,8 +20,7 @@
|
|||
|
||||
#include <control.h>
|
||||
#include <control_def.h>
|
||||
|
||||
ControlProtocolType control_protocol;
|
||||
//#include "s7.h"
|
||||
|
||||
/**
|
||||
* @description: Control Framework Find certain Protocol
|
||||
|
@ -282,8 +281,21 @@ int ControlFrameworkInit(void)
|
|||
goto _out;
|
||||
}
|
||||
|
||||
printf("%u %u\n",control_protocol->recipe->total_data_length,control_protocol->recipe->device_id);
|
||||
|
||||
printf("%s ControlPeripheralInit done\n", __func__);
|
||||
|
||||
//s7 read
|
||||
|
||||
|
||||
|
||||
// ret = ReadPlcDataByRecipe(control_protocol->recipe);
|
||||
// if (ret < 0) {
|
||||
// printf("%s failed!\n", __func__);
|
||||
// PrivFree(control_protocol);
|
||||
// goto _out;
|
||||
// }
|
||||
|
||||
_out:
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,8 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
struct ControlProtocol;
|
||||
typedef struct ControlProtocol *ControlProtocolType;
|
||||
|
||||
|
@ -98,6 +100,9 @@ int ControlProtocolWrite(struct ControlProtocol *control_protocol, const void *b
|
|||
/*Control Framework Protocol Ioctl*/
|
||||
int ControlProtocolIoctl(struct ControlProtocol *control_protocol, int cmd, void *args);
|
||||
|
||||
/*Control Framework new certain Protocol*/
|
||||
ControlProtocolType control_protocol;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include <control_def.h>
|
||||
#include <control_io.h>
|
||||
#include "s7.h"
|
||||
|
||||
/*using cirtular area to receive data*/
|
||||
#define PLC_DATA_LENGTH 1024
|
||||
|
@ -45,6 +46,10 @@ extern int ModbusTcpProtocolInit(struct ControlRecipe *p_recipe);
|
|||
extern int ModbusUartProtocolInit(struct ControlRecipe *p_recipe);
|
||||
#endif
|
||||
|
||||
#ifdef CONTROL_PROTOCOL_S7
|
||||
extern int S7ProtocolInit(struct ControlRecipe *p_recipe);
|
||||
#endif
|
||||
|
||||
/*
|
||||
CONTROL FRAMEWORK READ DATA FORMAT:
|
||||
| HEAD |device_id|read data length|read item count| data |
|
||||
|
@ -80,6 +85,9 @@ static struct ControlProtocolInitParam protocol_init[] =
|
|||
#ifdef CONTROL_PROTOCOL_MODBUS_UART
|
||||
{ PROTOCOL_MODBUS_UART, ModbusUartProtocolInit },
|
||||
#endif
|
||||
#ifdef CONTROL_PROTOCOL_S7
|
||||
{ PROTOCOL_S7, S7ProtocolInit },
|
||||
#endif
|
||||
|
||||
{ PROTOCOL_END, NULL },
|
||||
};
|
||||
|
@ -422,6 +430,73 @@ int RecipeBasicInformation(struct ControlRecipe *p_recipe, cJSON *p_recipe_file_
|
|||
printf("\n************************************************************\n");
|
||||
}
|
||||
|
||||
static uint8_t GetS7ValueTypeMemorySize(UniformValueType uniform_value_type)
|
||||
{
|
||||
if (uniform_value_type == UNIFORM_BOOL || uniform_value_type == UNIFORM_INT8 || uniform_value_type == UNIFORM_UINT8)
|
||||
return 1;
|
||||
if (uniform_value_type == UNIFORM_INT16 || uniform_value_type == UNIFORM_UINT16)
|
||||
return 2;
|
||||
if (uniform_value_type == UNIFORM_INT32 || uniform_value_type == UNIFORM_UINT32 || uniform_value_type == UNIFORM_FLOAT)
|
||||
return 4;
|
||||
if (uniform_value_type == UNIFORM_DOUBLE)
|
||||
return 8;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: S7 Protocol read item Init
|
||||
* @param p_read_item - read item pointer
|
||||
* @param read_item_json - read item json pointer
|
||||
* @param p_data - unused
|
||||
* @return success : 0 error : -1
|
||||
*/
|
||||
static uint8_t InitialS7ReadItem(S7ReadItem* p_read_item, cJSON* read_item_json, uint8_t* p_data)
|
||||
{
|
||||
p_read_item->value_type = cJSON_GetObjectItem(read_item_json, "value_type")->valueint;
|
||||
strncpy(p_read_item->value_name, cJSON_GetObjectItem(read_item_json, "value_name")->valuestring, 20);
|
||||
TS7DataItem* p_data_info = &(p_read_item->data_info);
|
||||
p_data_info->Amount = cJSON_GetObjectItem(read_item_json, "amount")->valueint;
|
||||
p_data_info->Start = cJSON_GetObjectItem(read_item_json, "start")->valueint;
|
||||
p_data_info->DBNumber = cJSON_GetObjectItem(read_item_json, "db_number")->valueint;
|
||||
char* area_valuestring = cJSON_GetObjectItem(read_item_json, "area")->valuestring;
|
||||
if (strcmp(area_valuestring, "I") == 0)
|
||||
p_data_info->Area = S7AreaPE;
|
||||
else if (strcmp(area_valuestring, "Q") == 0)
|
||||
p_data_info->Area = S7AreaPA;
|
||||
else if (strcmp(area_valuestring, "M") == 0)
|
||||
p_data_info->Area = S7AreaMK;
|
||||
else if (strcmp(area_valuestring, "DB") == 0)
|
||||
p_data_info->Area = S7AreaDB;
|
||||
else if (strcmp(area_valuestring, "C") == 0)
|
||||
p_data_info->Area = S7AreaCT;
|
||||
else if (strcmp(area_valuestring, "T") == 0)
|
||||
p_data_info->Area = S7AreaTM;
|
||||
char* wordlen_valuestring = cJSON_GetObjectItem(read_item_json, "wordlen")->valuestring;
|
||||
if (strcmp(wordlen_valuestring, "Bit") == 0)
|
||||
p_data_info->WordLen = S7WLBit;
|
||||
else if (strcmp(wordlen_valuestring, "Byte") == 0)
|
||||
p_data_info->WordLen = S7WLByte;
|
||||
else if (strcmp(wordlen_valuestring, "Word") == 0)
|
||||
p_data_info->WordLen = S7WLWord;
|
||||
else if (strcmp(wordlen_valuestring, "DWord") == 0)
|
||||
p_data_info->WordLen = S7WLDWord;
|
||||
else if (strcmp(wordlen_valuestring, "Real") == 0)
|
||||
p_data_info->WordLen = S7WLReal;
|
||||
else if (strcmp(wordlen_valuestring, "Counter") == 0)
|
||||
p_data_info->WordLen = S7WLCounter;
|
||||
else if (strcmp(wordlen_valuestring, "Timer") == 0)
|
||||
p_data_info->WordLen = S7WLTimer;
|
||||
p_data_info->pdata = p_data;
|
||||
printf("value_type is %d, amount is %d, start is %04d, db_number is %d, area is 0x%03x, wordlen is %d.\n",
|
||||
p_read_item->value_type, p_data_info->Amount, p_data_info->Start, p_data_info->DBNumber,
|
||||
p_data_info->Area, p_data_info->WordLen);
|
||||
return GetS7ValueTypeMemorySize(p_read_item->value_type);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @description: Control Framework Read Variable Item Function
|
||||
* @param p_recipe - Control recipe pointer
|
||||
|
@ -445,23 +520,37 @@ void RecipeReadVariableItem(struct ControlRecipe *p_recipe, cJSON *p_recipe_file
|
|||
p_recipe->protocol_data.data = PrivMalloc(CONTROL_DATA_HEAD_LENGTH + p_recipe->total_data_length);
|
||||
p_recipe->protocol_data.data_length = CONTROL_DATA_HEAD_LENGTH + p_recipe->total_data_length;
|
||||
memset(p_recipe->protocol_data.data, 0, p_recipe->protocol_data.data_length);
|
||||
|
||||
protocol_format_info.p_read_item_data = p_recipe->protocol_data.data + CONTROL_DATA_HEAD_LENGTH;
|
||||
|
||||
/*Init The Control Protocol*/
|
||||
ControlProtocolInitDesc(p_recipe, protocol_init);
|
||||
|
||||
/*Format Data Header, Reference "CONTROL FRAMEWORK READ DATA FORMAT"*/
|
||||
FormatDataHeader(p_recipe);
|
||||
|
||||
uint16_t read_item_count = p_recipe->read_item_count;
|
||||
|
||||
|
||||
uint8_t* S7_plc_read_data;
|
||||
S7_plc_read_data = malloc(p_recipe->protocol_data.data_length);
|
||||
uint16_t S7_plc_read_data_index = 8;
|
||||
|
||||
for (i = 0; i < read_item_count; i ++) {
|
||||
cJSON *read_single_item_json = cJSON_GetArrayItem(read_item_list_json, i);
|
||||
|
||||
protocol_format_info.read_single_item_json = read_single_item_json;
|
||||
protocol_format_info.read_item_index = i;
|
||||
switch (p_recipe->protocol_type) {
|
||||
|
||||
case PROTOCOL_S7:
|
||||
if (i == 0)
|
||||
p_recipe->read_item = malloc(sizeof(S7ReadItem) * p_recipe->read_item_count);
|
||||
S7_plc_read_data_index += InitialS7ReadItem((S7ReadItem*)(p_recipe->read_item) + i,
|
||||
read_single_item_json,S7_plc_read_data + S7_plc_read_data_index);
|
||||
if (S7_plc_read_data_index == 8) {
|
||||
printf("%s read %d item failed!\n", __func__, i);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
/*Format Protocol Cmd By Analyze Variable Item One By One*/
|
||||
ret = p_recipe->ControlProtocolFormatCmd(p_recipe, &protocol_format_info);
|
||||
if (ret < 0) {
|
||||
|
@ -472,3 +561,5 @@ void RecipeReadVariableItem(struct ControlRecipe *p_recipe, cJSON *p_recipe_file
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -44,6 +44,7 @@ extern "C" {
|
|||
} \
|
||||
}while (0)
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
UNIFORM_BOOL = 1,
|
||||
|
|
Loading…
Reference in New Issue