新增获取协议ID

This commit is contained in:
zhouhuang 2024-10-15 14:10:59 +08:00
parent b497f4cbdb
commit 9973a1acc1
5 changed files with 111 additions and 63 deletions

View File

@ -3,7 +3,7 @@ project (application C CXX)
set (VERSION 1.0.1) set (VERSION 1.0.1)
option (USE_MQTT "use mqtt protocol" ON) option (USE_MQTT "use mqtt protocol" ON)
option (USE_WEBSOCKET "use websocket" OFF) option (USE_WEBSOCKET "use websocket" ON)
option (USE_SQLITE3 "use sqlite3" ON) option (USE_SQLITE3 "use sqlite3" ON)
if (USE_SQLITE3) if (USE_SQLITE3)
@ -41,9 +41,6 @@ else ()
message ("else:${CMAKE_BUILD_TYPE}") message ("else:${CMAKE_BUILD_TYPE}")
message ("else:${CMAKE_C_FLAGS_RELEASE}") message ("else:${CMAKE_C_FLAGS_RELEASE}")
endif() endif()
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "ppc")
add_definitions (-DMCU_BIG_ENDIAN)
endif ()
option (HAVE_FTP_PROCESS "use ry ftp" ON) option (HAVE_FTP_PROCESS "use ry ftp" ON)
if (HAVE_FTP_PROCESS) if (HAVE_FTP_PROCESS)
@ -79,7 +76,6 @@ set (APP_SRCS
hostmodbustcp/host_modbus_tcp.cpp hostmodbustcp/host_modbus_tcp.cpp
rtustatusproc/rtustatus.cpp rtustatusproc/rtustatus.cpp
subiec104/sub_iec104.cpp subiec104/sub_iec104.cpp
submodbustcp/sub_modbus_rtu_tcp.cpp
zjd3100proc/zjd3100pro.cpp zjd3100proc/zjd3100pro.cpp
) )
@ -207,7 +203,6 @@ endif (USE_MQTT)
if (USE_SQLITE3) if (USE_SQLITE3)
set (APP_SRCS ${APP_SRCS} set (APP_SRCS ${APP_SRCS}
subgdw104/sub_gdw104.cpp
third_party/sqlite/KompexSQLiteBlob.cpp third_party/sqlite/KompexSQLiteBlob.cpp
third_party/sqlite/KompexSQLiteDatabase.cpp third_party/sqlite/KompexSQLiteDatabase.cpp
third_party/sqlite/KompexSQLiteStatement.cpp third_party/sqlite/KompexSQLiteStatement.cpp

View File

@ -4,7 +4,6 @@
#include "../subiec104/sub_iec104.h" #include "../subiec104/sub_iec104.h"
#include "../hostmodbusrtu/host_modbus_rtu.h" #include "../hostmodbusrtu/host_modbus_rtu.h"
#include "../hostmodbustcp/host_modbus_tcp.h" #include "../hostmodbustcp/host_modbus_tcp.h"
#include "../submodbustcp/sub_modbus_rtu_tcp.h"
#include "../rtustatusproc/rtustatus.h" #include "../rtustatusproc/rtustatus.h"
#include "../zjd3100proc/zjd3100pro.h" #include "../zjd3100proc/zjd3100pro.h"
//#include "../bfftpfile2issmqtt/bfftpfile2issmqtt.h" //#include "../bfftpfile2issmqtt/bfftpfile2issmqtt.h"
@ -351,11 +350,6 @@ void CChangeMaster::StartUp(void)
vLog(LOG_INFO, "协议<%d>创建为: modbus tcp主协议.\n", i); vLog(LOG_INFO, "协议<%d>创建为: modbus tcp主协议.\n", i);
procs[i] = new CHostModbusTcpProcess(m_conn); procs[i] = new CHostModbusTcpProcess(m_conn);
} }
else if (PROTOCOL_SUB_MODBUS_TCP == config.processes[i].proto)
{
vLog(LOG_INFO, "协议<%d>创建为: modbus tcp从协议.\n", i);
procs[i] = new CSubModbusTcpProcess();
}
else if (PROTOCOL_HOST_IEC104 == config.processes[i].proto) else if (PROTOCOL_HOST_IEC104 == config.processes[i].proto)
{ {
vLog(LOG_INFO, "协议<%d>创建为: iec104主协议.\n", i); vLog(LOG_INFO, "协议<%d>创建为: iec104主协议.\n", i);
@ -376,13 +370,6 @@ void CChangeMaster::StartUp(void)
vLog(LOG_INFO, "协议<%d>创建为: 网关状态协议.\n", i); vLog(LOG_INFO, "协议<%d>创建为: 网关状态协议.\n", i);
procs[i] = new CRTUStatusProcess(); procs[i] = new CRTUStatusProcess();
} }
#if 0
else if (PROTOCOL_BF_FTP == config.processes[i].proto)
{//倍福FTP文件协议
vLog(LOG_INFO, "协议<%d>创建为: 倍福FTP文件协议.\n", i);
procs[i] = new CBFFTPFile2ISSMQTTProcess();
}
#endif
else else
{ {
vLog(LOG_ERROR, "(%s,%d)系统不支持该协议或该协议未知.\n", config.processes[i].name, config.processes[i].proto); vLog(LOG_ERROR, "(%s,%d)系统不支持该协议或该协议未知.\n", config.processes[i].name, config.processes[i].proto);

View File

@ -5,14 +5,13 @@
//The individual slave devices are assigned addresses in the range of 1 锟?C 247. // //The individual slave devices are assigned addresses in the range of 1 锟?C 247. //
//Address 0 is used for the broadcast address, which all slave devices recognize.// //Address 0 is used for the broadcast address, which all slave devices recognize.//
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
#define HAVE_FTP_PROCESS
#ifdef HAVE_FTP_PROCESS #ifdef HAVE_FTP_PROCESS
#define MODBUSP_READ_ID 100 //读取文件及文件夹ID #define MODBUSP_READ_ID 100 //读取文件及文件夹ID
#define MODBUSP_READ_ID_FUNCCODE 0x03 //读取文件及文件夹ID功能码。 #define MODBUSP_READ_ID_FUNCCODE 0x03 //读取文件及文件夹ID功能码。
#define MODBUSP_READ_ID_REGISTER_ADDRESS 0x01 //读取文件及文件夹ID寄存器地址。 #define MODBUSP_READ_ID_REGISTER_ADDRESS 150 //读取文件及文件夹ID寄存器地址。
#define MODBUSP_READ_ID_REGISTER_LENGTH 0x6 //读取文件及文件夹ID寄存器长须 #define MODBUSP_READ_ID_REGISTER_LENGTH 9 //读取文件及文件夹ID寄存器长度
#include <curl/curl.h> #include <curl/curl.h>
@ -129,6 +128,14 @@ static void* ryftp_process(void* param)
char password[128] = "123456"; char password[128] = "123456";
char ipaddress[128] = "127.0.0.1"; char ipaddress[128] = "127.0.0.1";
//根据协议创建一个本地协议目录
int pid = mbt->GetCurID();
if (pid < 0 || pid >= PROCESSES_NUM) return ((void*)0);
char pathName[128];
snprintf(pathName, sizeof(pathName), "%d", pid);
if (mbt->_mkdir(pathName) < 0) return ((void*)0);
//配置的用户名和密码
#if 0 #if 0
snprintf(user, sizeof(user), "%s", mbt->m_user); snprintf(user, sizeof(user), "%s", mbt->m_user);
snprintf(password, sizeof(password), "%s", mbt->m_password); snprintf(password, sizeof(password), "%s", mbt->m_password);
@ -143,23 +150,26 @@ static void* ryftp_process(void* param)
while (TRUE) { while (TRUE) {
sleep(1); //每秒执行一次 sleep(1); //每秒执行一次
//ftp获取文件 //ftp获取文件
snprintf(name, sizeof(name), "%d.rtd", mbt->m_currentFileNo + 1); if (mbt->m_iv == 1) {
snprintf(remote, sizeof(remote), "ftp://%s/data/rtdatalog/%d/%d.rtd", str, mbt->m_currentDirNo + 1, mbt->m_currentFileNo + 1); //文件目录无效
continue;
}
snprintf(name, sizeof(name), "%s/%d", pathName, mbt->m_currentFileNo);
snprintf(remote, sizeof(remote), "ftp://%s/data/rtdatalog/%d/%d", str, mbt->m_currentDirNo, mbt->m_currentFileNo);
if (ftpget(remote, name, user, password)) { if (ftpget(remote, name, user, password)) {
//成功,处理文件 //成功,处理文件
vLog(LOG_DEBUG, "get a file, then send to ws.\n"); vLog(LOG_DEBUG, "get a file, then send to ws.\n");
mbt->m_lastDirNo = mbt->m_currentDirNo;
mbt->m_lastFileNo = mbt->m_currentFileNo; mbt->m_lastFileNo = mbt->m_currentFileNo;
mbt->m_currentFileNo++; mbt->m_currentFileNo++;
if (mbt->m_currentFileNo % 1000 == 0) if ((mbt->m_currentFileNo - mbt->m_lastStartFileNo) % 1000 == 0) {
{ //一个文件夹最多存放1000个文件 //一个文件夹最多存放1000个文件, mbt->m_currentFileNo = 0;
mbt->m_currentFileNo = 0;
mbt->m_currentDirNo++; mbt->m_currentDirNo++;
if (mbt->m_currentDirNo >= 56) mbt->m_lastStartFileNo = mbt->m_currentFileNo;
{ //7天数据大约有56个文件夹 if (mbt->m_currentDirNo >= 56) { //7天数据大约有56个文件夹
mbt->m_currentDirNo = 0; mbt->m_currentDirNo = 0;
} }
//保存文件信息
} }
} }
} }
@ -269,11 +279,17 @@ CHostModbusTcpProcess::CHostModbusTcpProcess(noPollConn *conn)
m_nNeedSend = FALSE; m_nNeedSend = FALSE;
//websocket接口 //websocket接口
m_conn = NULL;
if (conn != NULL) if (conn != NULL)
{ {
m_conn = conn; m_conn = conn;
} }
m_pid = 0;
//目录无效
m_iv = 1;
m_currentDirNo = -1; //当前目录编号 m_currentDirNo = -1; //当前目录编号
m_currentFileNo = -1; //当前文件编号 m_currentFileNo = -1; //当前文件编号
m_lastDirNo = -1; //上一目录编号 m_lastDirNo = -1; //上一目录编号
@ -440,7 +456,17 @@ void CHostModbusTcpProcess::calc2(void)
if (yccount) ycparam = new STRUCT_PARAM[yccount]; if (yccount) ycparam = new STRUCT_PARAM[yccount];
if (ymcount) ymparam = new STRUCT_PARAM[ymcount]; if (ymcount) ymparam = new STRUCT_PARAM[ymcount];
if (yxcount) yxparam = new STRUCT_PARAM[yxcount]; if (yxcount) yxparam = new STRUCT_PARAM[yxcount];
#if 0
//插入一帧读取信息的报文
ycframes[0].FrameType = MODBUSP_READ_ID;
ycframes[0].FuncCode = MODBUSP_READ_ID_FUNCCODE;
ycframes[0].RegBegin = MODBUSP_READ_ID_REGISTER_ADDRESS;
ycframes[0].RegCount = MODBUSP_READ_ID_REGISTER_LENGTH;
j = 1;
#else
j = 0;
#endif
if (ycparam) if (ycparam)
{ {
memset(ycparam, 0, sizeof(STRUCT_PARAM) * yccount); memset(ycparam, 0, sizeof(STRUCT_PARAM) * yccount);
@ -457,7 +483,7 @@ void CHostModbusTcpProcess::calc2(void)
{ {
WORD addr = MAKEWORD(ycparam[n].param[1], ycparam[n].param[2]); WORD addr = MAKEWORD(ycparam[n].param[1], ycparam[n].param[2]);
if (addr >= 65535) continue; if (addr >= 65535) continue;
for (j = 0; j < MODBUS_RTU_AUTOMATIC_FRAME; j++) for (; j < MODBUS_RTU_AUTOMATIC_FRAME; j++)
{ {
if (ycframes[j].FuncCode == 0) if (ycframes[j].FuncCode == 0)
{ {
@ -600,7 +626,25 @@ BOOLEAN CHostModbusTcpProcess::OnPreCreate(int id)
m_nTimeout = 200; m_nTimeout = 200;
calc2(); calc2();
//启动后,读取本地文件更新最后一次的文件夹及文件序号 //启动后创建ftp线程
if (m_pid <= 0) {
vLog(LOG_DEBUG, "create a ftp thread.\n");
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, MEMERY_1M);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
if (pthread_create(&m_pid, &attr, ryftp_process, this) < 0) {
vLog(LOG_ERROR, "create ryftp_process error(%d,%s).\n", errno, strerror(errno));
return TRUE;
}
char name[17];
snprintf(name, 16, "%s_ftp", GetCurProcessName());
pthread_setname_np(m_pid, name);
pthread_attr_destroy(&attr);
}
return TRUE; return TRUE;
} }
@ -661,6 +705,8 @@ BOOLEAN CHostModbusTcpProcess::Run(void)
pItem->m_nNum++; pItem->m_nNum++;
m_nSendPoint = system32.ticks;//0; m_nSendPoint = system32.ticks;//0;
m_nNeedSend = FALSE; m_nNeedSend = FALSE;
//fprintf(stderr, "tx<%d>: ", count+6); for (int a = 0; a < count+6; a++) fprintf(stderr, "%02X ", buffer[a]); fprintf(stderr, "\n");
FeedDog(); FeedDog();
} }
} }
@ -701,10 +747,11 @@ BOOLEAN CHostModbusTcpProcess::OnTimer(void)
pItem = (CHostModbusTcpProcessItem *)GetNextItem(); pItem = (CHostModbusTcpProcessItem *)GetNextItem();
if (pItem == NULL) return TRUE; if (pItem == NULL) return TRUE;
m_nNeedSend = TRUE; m_nNeedSend = TRUE;
#if 1
//启动时读取一次,后面自己维护序号
if ((m_currentDirNo == -1) && (m_currentFileNo == -1)) if ((m_currentDirNo == -1) && (m_currentFileNo == -1))
{ //当前文件和目录都为-1程序第一次启动。需要获取ftp目录及文件ID { //当前文件和目录都为-1程序第一次启动。需要获取ftp目录及文件ID
/* code */
vLog(LOG_DEBUG, "here..... add read id frame.\n");
m_nFrameType = MODBUSP_READ_ID; m_nFrameType = MODBUSP_READ_ID;
m_nCurFuncCode = MODBUSP_READ_ID_FUNCCODE; m_nCurFuncCode = MODBUSP_READ_ID_FUNCCODE;
m_nCurBeginReg = MODBUSP_READ_ID_REGISTER_ADDRESS; m_nCurBeginReg = MODBUSP_READ_ID_REGISTER_ADDRESS;
@ -712,6 +759,7 @@ BOOLEAN CHostModbusTcpProcess::OnTimer(void)
pItem->m_nFramePoll |= MODBUSP_GET_DATA_FRAME; pItem->m_nFramePoll |= MODBUSP_GET_DATA_FRAME;
return TRUE; return TRUE;
} }
#endif
struModbusExtFrame* frame = pItem->GetNextFrame(); struModbusExtFrame* frame = pItem->GetNextFrame();
if (frame != NULL) if (frame != NULL)
{ {
@ -1179,29 +1227,30 @@ BOOLEAN CHostModbusTcpProcess::OnReceiveIDData(CHostModbusTcpProcessItem *pItem,
pBuf = pData; pBuf = pData;
//根据结构来处理得到id信息 /*
1 byte
2 dint10
3 dint
4 dint;
5 dint
*/
//路径有效性判断
int iv = ((pBuf[0] << 8) | pBuf[1]); pBuf += 2;
// m_iv = iv;
m_currentDirNo = 0; if (iv) {
m_currentFileNo = 0; vLog(LOG_DEBUG, "路径信息无效\n");
return FALSE;
vLog(LOG_DEBUG, "create a ftp thread.\n");
//创建ftp线程
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, MEMERY_1M);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
if (pthread_create(&m_pid, &attr, ryftp_process, this) < 0) {
vLog(LOG_ERROR, "create ryftp_process error(%d,%s).\n", errno, strerror(errno));
return TRUE;
} }
char name[16]; //当前文件夹路径名
snprintf(name, 16, "%s_ftp", GetCurProcessName()); m_currentDirNo = (DWORD)((pBuf[2] << 24) | (pBuf[3] << 16) | (pBuf[0] << 8) | pBuf[1]); pBuf += 4;
pthread_setname_np(m_pid, name); //当前文件夹下最后新文件
m_currentFileNo = (DWORD)((pBuf[2] << 24) | (pBuf[3] << 16) | (pBuf[0] << 8) | pBuf[1]); pBuf += 4;
//当前文件夹下第一个文件
m_lastStartFileNo = (DWORD)((pBuf[2] << 24) | (pBuf[3] << 16) | (pBuf[0] << 8) | pBuf[1]); pBuf += 4;
vLog(LOG_DEBUG, "dir: %d, file: %d: start: %d\n", m_currentDirNo, m_currentFileNo, m_lastStartFileNo);
pthread_attr_destroy(&attr);
return TRUE; return TRUE;
} }

View File

@ -4,6 +4,7 @@
#include "netproc.h" #include "netproc.h"
#include "modbus_def.h" #include "modbus_def.h"
#include <dirent.h>
#include <nopoll.h> #include <nopoll.h>
#include <nopoll_decl.h> #include <nopoll_decl.h>
@ -60,16 +61,28 @@ private:
public: public:
noPollConn *m_conn; noPollConn *m_conn;
int m_currentDirNo; //当前目录编号 int m_iv;
int m_currentFileNo; //当前文件编号 LONG m_currentDirNo; //当前目录编号
int m_lastDirNo; //上一目录编号 LONG m_currentFileNo; //当前文件编号
int m_lastFileNo; //上一文件编号 LONG m_lastDirNo; //上一目录编号
LONG m_lastFileNo; //上一文件编号
LONG m_lastStartFileNo; //文件开始编号
//ftp参数信息 //ftp参数信息
char m_user[128]; char m_user[128];
char m_password[128]; char m_password[128];
char m_remoteIp[64]; char m_remoteIp[64];
int _mkdir(const char* pathName) {
if (access(pathName, F_OK) == 0) return 0;
// 创建目录
if (mkdir(pathName, 0777) == -1) {
vLog(LOG_ERROR, "创建文件夹 %s失败<%d,%s>\r\n", pathName, errno, strerror(errno));
return -1;
}
return 1;
}
public: public:
CHostModbusTcpProcess(noPollConn *conn); CHostModbusTcpProcess(noPollConn *conn);
virtual ~CHostModbusTcpProcess(); virtual ~CHostModbusTcpProcess();

View File

@ -47,11 +47,15 @@ public:
config.processes[m_nProcess].softdog = 0; config.processes[m_nProcess].softdog = 0;
} }
inline char* GetCurProcessName() const inline char* GetCurProcessName(void) const
{ {
if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return NULL; if (m_nProcess < 0 || m_nProcess >= PROCESSES_NUM) return NULL;
return config.processes[m_nProcess].name; return config.processes[m_nProcess].name;
} }
inline int GetCurID(void) const
{
return m_nProcess;
}
inline int GetCurOrder() const inline int GetCurOrder() const
{ {