diff --git a/das-dn/CMakeLists.txt b/das-dn/CMakeLists.txt index b60f0e3f..45239f79 100644 --- a/das-dn/CMakeLists.txt +++ b/das-dn/CMakeLists.txt @@ -3,7 +3,7 @@ project (application C CXX) set (VERSION 1.0.1) 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) if (USE_SQLITE3) @@ -41,9 +41,6 @@ else () message ("else:${CMAKE_BUILD_TYPE}") message ("else:${CMAKE_C_FLAGS_RELEASE}") endif() -if (CMAKE_SYSTEM_PROCESSOR STREQUAL "ppc") - add_definitions (-DMCU_BIG_ENDIAN) -endif () option (HAVE_FTP_PROCESS "use ry ftp" ON) if (HAVE_FTP_PROCESS) @@ -79,7 +76,6 @@ set (APP_SRCS hostmodbustcp/host_modbus_tcp.cpp rtustatusproc/rtustatus.cpp subiec104/sub_iec104.cpp - submodbustcp/sub_modbus_rtu_tcp.cpp zjd3100proc/zjd3100pro.cpp ) @@ -207,7 +203,6 @@ endif (USE_MQTT) if (USE_SQLITE3) set (APP_SRCS ${APP_SRCS} - subgdw104/sub_gdw104.cpp third_party/sqlite/KompexSQLiteBlob.cpp third_party/sqlite/KompexSQLiteDatabase.cpp third_party/sqlite/KompexSQLiteStatement.cpp diff --git a/das-dn/cmg/changemaster.cpp b/das-dn/cmg/changemaster.cpp index b649b724..8d607cc3 100644 --- a/das-dn/cmg/changemaster.cpp +++ b/das-dn/cmg/changemaster.cpp @@ -4,7 +4,6 @@ #include "../subiec104/sub_iec104.h" #include "../hostmodbusrtu/host_modbus_rtu.h" #include "../hostmodbustcp/host_modbus_tcp.h" -#include "../submodbustcp/sub_modbus_rtu_tcp.h" #include "../rtustatusproc/rtustatus.h" #include "../zjd3100proc/zjd3100pro.h" //#include "../bfftpfile2issmqtt/bfftpfile2issmqtt.h" @@ -351,11 +350,6 @@ void CChangeMaster::StartUp(void) vLog(LOG_INFO, "协议<%d>创建为: modbus tcp主协议.\n", i); 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) { vLog(LOG_INFO, "协议<%d>创建为: iec104主协议.\n", i); @@ -376,13 +370,6 @@ void CChangeMaster::StartUp(void) vLog(LOG_INFO, "协议<%d>创建为: 网关状态协议.\n", i); 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 { vLog(LOG_ERROR, "(%s,%d)系统不支持该协议或该协议未知.\n", config.processes[i].name, config.processes[i].proto); diff --git a/das-dn/hostmodbustcp/host_modbus_tcp.cpp b/das-dn/hostmodbustcp/host_modbus_tcp.cpp index da8f08af..ad99e97f 100644 --- a/das-dn/hostmodbustcp/host_modbus_tcp.cpp +++ b/das-dn/hostmodbustcp/host_modbus_tcp.cpp @@ -5,14 +5,13 @@ //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.// /////////////////////////////////////////////////////////////////////////////////// - +#define 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_REGISTER_ADDRESS 0x01 //读取文件及文件夹ID寄存器地址。 -#define MODBUSP_READ_ID_REGISTER_LENGTH 0x6 //读取文件及文件夹ID寄存器长须。 +#define MODBUSP_READ_ID_REGISTER_ADDRESS 150 //读取文件及文件夹ID寄存器地址。 +#define MODBUSP_READ_ID_REGISTER_LENGTH 9 //读取文件及文件夹ID寄存器长度。 #include @@ -129,6 +128,14 @@ static void* ryftp_process(void* param) char password[128] = "123456"; 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 snprintf(user, sizeof(user), "%s", mbt->m_user); snprintf(password, sizeof(password), "%s", mbt->m_password); @@ -143,23 +150,26 @@ static void* ryftp_process(void* param) while (TRUE) { sleep(1); //每秒执行一次 //ftp获取文件 - snprintf(name, sizeof(name), "%d.rtd", mbt->m_currentFileNo + 1); - snprintf(remote, sizeof(remote), "ftp://%s/data/rtdatalog/%d/%d.rtd", str, mbt->m_currentDirNo + 1, mbt->m_currentFileNo + 1); + if (mbt->m_iv == 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)) { //成功,处理文件 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_currentFileNo++; - if (mbt->m_currentFileNo % 1000 == 0) - { //一个文件夹最多存放1000个文件 - mbt->m_currentFileNo = 0; + if ((mbt->m_currentFileNo - mbt->m_lastStartFileNo) % 1000 == 0) { + //一个文件夹最多存放1000个文件, mbt->m_currentFileNo = 0; mbt->m_currentDirNo++; - if (mbt->m_currentDirNo >= 56) - { //7天数据大约有56个文件夹 + mbt->m_lastStartFileNo = mbt->m_currentFileNo; + if (mbt->m_currentDirNo >= 56) { //7天数据大约有56个文件夹 mbt->m_currentDirNo = 0; } + //保存文件信息 } } } @@ -269,11 +279,17 @@ CHostModbusTcpProcess::CHostModbusTcpProcess(noPollConn *conn) m_nNeedSend = FALSE; //websocket接口 + m_conn = NULL; if (conn != NULL) { m_conn = conn; } + m_pid = 0; + + //目录无效 + m_iv = 1; + m_currentDirNo = -1; //当前目录编号 m_currentFileNo = -1; //当前文件编号 m_lastDirNo = -1; //上一目录编号 @@ -440,7 +456,17 @@ void CHostModbusTcpProcess::calc2(void) if (yccount) ycparam = new STRUCT_PARAM[yccount]; if (ymcount) ymparam = new STRUCT_PARAM[ymcount]; 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) { 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]); 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) { @@ -600,7 +626,25 @@ BOOLEAN CHostModbusTcpProcess::OnPreCreate(int id) m_nTimeout = 200; 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; } @@ -661,6 +705,8 @@ BOOLEAN CHostModbusTcpProcess::Run(void) pItem->m_nNum++; m_nSendPoint = system32.ticks;//0; 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(); } } @@ -701,10 +747,11 @@ BOOLEAN CHostModbusTcpProcess::OnTimer(void) pItem = (CHostModbusTcpProcessItem *)GetNextItem(); if (pItem == NULL) return TRUE; m_nNeedSend = TRUE; + +#if 1 + //启动时读取一次,后面自己维护序号 if ((m_currentDirNo == -1) && (m_currentFileNo == -1)) { //当前文件和目录都为-1,程序第一次启动。需要获取ftp目录及文件ID - /* code */ - vLog(LOG_DEBUG, "here..... add read id frame.\n"); m_nFrameType = MODBUSP_READ_ID; m_nCurFuncCode = MODBUSP_READ_ID_FUNCCODE; m_nCurBeginReg = MODBUSP_READ_ID_REGISTER_ADDRESS; @@ -712,6 +759,7 @@ BOOLEAN CHostModbusTcpProcess::OnTimer(void) pItem->m_nFramePoll |= MODBUSP_GET_DATA_FRAME; return TRUE; } +#endif struModbusExtFrame* frame = pItem->GetNextFrame(); if (frame != NULL) { @@ -1179,29 +1227,30 @@ BOOLEAN CHostModbusTcpProcess::OnReceiveIDData(CHostModbusTcpProcessItem *pItem, pBuf = pData; - //根据结构来处理得到id信息 +/* + 1)、路径信息是否有效 byte; + 2)、当前文件夹路径名的后缀 dint,按照顺序增加1(转字符串时在前补0); + 3)、当前文件夹下最后新文件的编号 dint; + 4)、当前文件夹下第一个文件的编号 dint; + 5)、当前文件夹下文件个数 dint; +*/ + //路径有效性判断 + int iv = ((pBuf[0] << 8) | pBuf[1]); pBuf += 2; - // - m_currentDirNo = 0; - m_currentFileNo = 0; - - 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; + m_iv = iv; + if (iv) { + vLog(LOG_DEBUG, "路径信息无效\n"); + return FALSE; } - char name[16]; - snprintf(name, 16, "%s_ftp", GetCurProcessName()); - pthread_setname_np(m_pid, name); - - pthread_attr_destroy(&attr); + //当前文件夹路径名 + m_currentDirNo = (DWORD)((pBuf[2] << 24) | (pBuf[3] << 16) | (pBuf[0] << 8) | pBuf[1]); pBuf += 4; + //当前文件夹下最后新文件 + 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); + return TRUE; } diff --git a/das-dn/hostmodbustcp/host_modbus_tcp.h b/das-dn/hostmodbustcp/host_modbus_tcp.h index 1afc3ba9..54a29506 100644 --- a/das-dn/hostmodbustcp/host_modbus_tcp.h +++ b/das-dn/hostmodbustcp/host_modbus_tcp.h @@ -4,6 +4,7 @@ #include "netproc.h" #include "modbus_def.h" +#include #include #include @@ -60,16 +61,28 @@ private: public: noPollConn *m_conn; - int m_currentDirNo; //当前目录编号 - int m_currentFileNo; //当前文件编号 - int m_lastDirNo; //上一目录编号 - int m_lastFileNo; //上一文件编号 + int m_iv; + LONG m_currentDirNo; //当前目录编号 + LONG m_currentFileNo; //当前文件编号 + LONG m_lastDirNo; //上一目录编号 + LONG m_lastFileNo; //上一文件编号 + LONG m_lastStartFileNo; //文件开始编号 //ftp参数信息 char m_user[128]; char m_password[128]; 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: CHostModbusTcpProcess(noPollConn *conn); virtual ~CHostModbusTcpProcess(); diff --git a/das-dn/inc/process.h b/das-dn/inc/process.h index a3cb03b1..84474ff7 100644 --- a/das-dn/inc/process.h +++ b/das-dn/inc/process.h @@ -47,11 +47,15 @@ public: 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; return config.processes[m_nProcess].name; } + inline int GetCurID(void) const + { + return m_nProcess; + } inline int GetCurOrder() const {