|
|
|
|
#include <iostream>
|
|
|
|
|
#include "modbus_lib/modbus-tcp.h"
|
|
|
|
|
#include "common.h"
|
|
|
|
|
#include "time.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
#define REG_ADDR (header_length+1)
|
|
|
|
|
#define REG_NUM (header_length+3)
|
|
|
|
|
#define REG_VAL (header_length+3)
|
|
|
|
|
|
|
|
|
|
#define IP_SLAVE_ADDR "192.168.1.21"
|
|
|
|
|
#define TCP_PORT 1502
|
|
|
|
|
#define SERVER_ID 0x00000001
|
|
|
|
|
|
|
|
|
|
static char slave_ip_addr[20] = { 0 };
|
|
|
|
|
static unsigned short slave_tcp_port = TCP_PORT;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define SLAVE_REG_ADDR 0x008E
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
TCP,
|
|
|
|
|
TCP_PI,
|
|
|
|
|
RTU
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//modbus <20>ӻ<EFBFBD>tcp ipaddr
|
|
|
|
|
void xMaster_Set_slave_ip_addr(const char *ip_addr)
|
|
|
|
|
{
|
|
|
|
|
if (ip_addr) {
|
|
|
|
|
memcpy(slave_ip_addr, ip_addr, strlen(ip_addr));
|
|
|
|
|
printf("slave_ip_addr=%s\n", slave_ip_addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
memcpy(slave_ip_addr, IP_SLAVE_ADDR, strlen(IP_SLAVE_ADDR));
|
|
|
|
|
printf("ip_addr is null,use default\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//modbus<75>ӻ<EFBFBD>tcp port
|
|
|
|
|
void xMaster_Set_slave_port(const char* tcp_port)
|
|
|
|
|
{
|
|
|
|
|
if (tcp_port) {
|
|
|
|
|
slave_tcp_port = atoi(tcp_port);
|
|
|
|
|
printf("tcp_port=%s\n", tcp_port);
|
|
|
|
|
printf("slave_tcp_port=%d\n", slave_tcp_port);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
slave_tcp_port = TCP_PORT;
|
|
|
|
|
printf("SLAVE_PORT is null,use default\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DWORD WINAPI xModbus_master_Proc(LPVOID lp) {
|
|
|
|
|
char* com = (char*)lp;
|
|
|
|
|
CHAR rcv_buf[1024] = { 0 };
|
|
|
|
|
int sockt = -1;
|
|
|
|
|
int i = 0;
|
|
|
|
|
int rc = 0;
|
|
|
|
|
modbus_t* ctx = NULL;
|
|
|
|
|
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];//<2F><><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int header_length;
|
|
|
|
|
uint16_t* ptr,*ptr1;
|
|
|
|
|
int use_backend=TCP;
|
|
|
|
|
uint16_t reg_addr, reg_val, reg_num;
|
|
|
|
|
uint16_t tmp = 0;
|
|
|
|
|
modbus_mapping_t* mb_mapping;
|
|
|
|
|
int nb_points;
|
|
|
|
|
uint16_t* tab_rp_registers = NULL;
|
|
|
|
|
fd_set fdset_ro, fdset_wr, fdset_ex;
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
|
|
//Read the starting address of the holding register
|
|
|
|
|
unsigned short start_hold_register = 0x0000;//<2F><><EFBFBD><EFBFBD><EFBFBD>ּĴ<D6BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ
|
|
|
|
|
//Number of registers read from start address
|
|
|
|
|
int number_regs = 22;//<2F><><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ<EFBFBD><D6B7>ʼ<EFBFBD><CABC><EFBFBD>ļĴ<C4BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nb_points = 500;
|
|
|
|
|
tab_rp_registers = (uint16_t*)malloc(nb_points * sizeof(uint16_t));
|
|
|
|
|
if (NULL != tab_rp_registers) {
|
|
|
|
|
memset(tab_rp_registers, 0, nb_points * sizeof(uint16_t));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (use_backend == TCP) {
|
|
|
|
|
ctx = modbus_new_tcp(slave_ip_addr, slave_tcp_port);
|
|
|
|
|
//modbus_set_slave(ctx, SERVER_ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modbus_set_response_timeout(ctx, 20, 0);
|
|
|
|
|
while (1) {
|
|
|
|
|
if (sockt <= 0)
|
|
|
|
|
{
|
|
|
|
|
sockt = modbus_connect(ctx);
|
|
|
|
|
if (sockt == -1) {
|
|
|
|
|
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
|
|
|
|
|
//modbus_free(ctx);
|
|
|
|
|
Sleep(1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("Connection Success! socket=%d\n", sockt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned char* src = (unsigned char*)tab_rp_registers;
|
|
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
|
if (sockt <= 0)
|
|
|
|
|
continue;
|
|
|
|
|
FD_ZERO(&fdset_ro);
|
|
|
|
|
FD_ZERO(&fdset_wr);
|
|
|
|
|
FD_ZERO(&fdset_ex);
|
|
|
|
|
FD_SET(sockt, &fdset_ro);
|
|
|
|
|
FD_SET(sockt, &fdset_wr);
|
|
|
|
|
FD_SET(sockt, &fdset_ex);
|
|
|
|
|
memset((char*)&timeout, 0, sizeof(struct timeval));
|
|
|
|
|
if (select(sockt + 1, &fdset_ro, &fdset_wr, &fdset_ex, &timeout) < 1)
|
|
|
|
|
{
|
|
|
|
|
Sleep(1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (FD_ISSET(sockt, &fdset_ex))
|
|
|
|
|
{
|
|
|
|
|
//printf("<22>ر<EFBFBD>tcp<63>˿<EFBFBD>\n");
|
|
|
|
|
closesocket(sockt);
|
|
|
|
|
sockt = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (FD_ISSET(sockt, &fdset_wr)) {
|
|
|
|
|
#endif
|
|
|
|
|
rc = modbus_read_registers(ctx, start_hold_register, number_regs, tab_rp_registers);
|
|
|
|
|
#if 1
|
|
|
|
|
if (rc == -1)
|
|
|
|
|
{
|
|
|
|
|
closesocket(sockt);
|
|
|
|
|
sockt = -1;
|
|
|
|
|
printf("End Thread<61><64>\n");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Sleep(1000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sleep(3 * 1000);
|
|
|
|
|
//xUnsigned_char_hex_out("recv", src, 0, number_regs*2);
|
|
|
|
|
xRead_reg_hex_out(start_hold_register, "master recv", src, 0, number_regs * 2);
|
|
|
|
|
//int slave_addr = MODBUS_GET_INT32_FROM_INT16(tab_rp_registers, 0);
|
|
|
|
|
//printf("slave_addr=%d\n", slave_addr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float H2 = modbus_get_float(tab_rp_registers);
|
|
|
|
|
printf("H2=%f\n", H2);
|
|
|
|
|
float CO = modbus_get_float(tab_rp_registers + 2);
|
|
|
|
|
printf("CO=%f\n", CO);
|
|
|
|
|
float CO2 = modbus_get_float(tab_rp_registers + 4);
|
|
|
|
|
printf("CO2=%f\n", CO2);
|
|
|
|
|
float CH4 = modbus_get_float(tab_rp_registers + 6);
|
|
|
|
|
printf("CH4=%f\n", CH4);
|
|
|
|
|
float C2H2 = modbus_get_float(tab_rp_registers + 8);
|
|
|
|
|
printf("C2H2=%f\n", C2H2);
|
|
|
|
|
float C2H4 = modbus_get_float(tab_rp_registers + 10);
|
|
|
|
|
printf("C2H4=%f\n", C2H4);
|
|
|
|
|
float C2H6 = modbus_get_float(tab_rp_registers + 12);
|
|
|
|
|
printf("C2H6=%f\n", C2H6);
|
|
|
|
|
float total = modbus_get_float(tab_rp_registers + 14);
|
|
|
|
|
printf("total=%f\n", total);
|
|
|
|
|
float water = modbus_get_float(tab_rp_registers + 16);
|
|
|
|
|
printf("water=%f\n", water);
|
|
|
|
|
|
|
|
|
|
//xUnsigned_char_hex_out("recv", src, 36, 36+7);
|
|
|
|
|
int H32 = modbus_get_int(tab_rp_registers + 18);
|
|
|
|
|
printf("H32=%d\n", H32);
|
|
|
|
|
int L32 = modbus_get_int(tab_rp_registers + 20);
|
|
|
|
|
printf("L32=%d\n", L32);
|
|
|
|
|
|
|
|
|
|
time_t t = H32 << 32 | L32;
|
|
|
|
|
|
|
|
|
|
printf("t=%ld\n", t);
|
|
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
struct tm t0;
|
|
|
|
|
localtime_s(&t0, &now);
|
|
|
|
|
printf("<EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>:%d<><64>%d<><64>%d<><64> %dʱ%d<><64>%d<><64>\n", t0.tm_year + 1900, t0.tm_mon + 1, t0.tm_mday, t0.tm_hour, t0.tm_min, t0.tm_sec);
|
|
|
|
|
//printf("t=%d\n", (int)time(NULL));
|
|
|
|
|
|
|
|
|
|
//time_t t3 = 0x64F9 << 16 | 0x4204;
|
|
|
|
|
//printf("t3=%ld\n", t3);
|
|
|
|
|
//time_t t4 = 1694056964;
|
|
|
|
|
//printf("%02x\n",t4);
|
|
|
|
|
Sleep(15 * 1000);
|
|
|
|
|
}
|
|
|
|
|
//while (1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int xModbus_master_task_init()
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3><CCA3><EFBFBD><EFBFBD>ؾ<EFBFBD><D8BE><EFBFBD>
|
|
|
|
|
int a = 0;
|
|
|
|
|
|
|
|
|
|
HANDLE h = CreateThread(NULL, 0, xModbus_master_Proc, NULL, 0, 0);
|
|
|
|
|
|
|
|
|
|
//WaitForSingleObject(h, INFINITE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//CloseHandle(h);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|