You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.0 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "common.h"
#include "stdio.h"
#include "stdlib.h"
#include "ini.h"
#include "ysp_modbus_master.h"
#include "windows.h"
#define CONFIG_FILE_NAME "modbusConnect.ini"
void xUnsigned_char_hex_out(const char *msg, unsigned char* src, int start, int number)
{
int new_line = 0;
int end = start + number;
if (msg != NULL) {
printf("%s:\n", msg);
}
for (int i = start; i < end; i++) {
printf("%02X ", src[i]);
new_line++;
if ( !(new_line % 8)) {
printf("\n");
}
}
printf("\n");
}
void xRead_reg_hex_out(unsigned short reg_start_addr,const char* msg, unsigned char* src, int start, int number)
{
int new_line = 0;
int end = start + number;
int reg_number = 0;
if (msg != NULL) {
printf("\n%s:\n", msg);
}
printf("-----------------\n");
printf("reg addr | data\n");
//printf("(%d)%.4x ", reg_number, reg_start_addr);
for (int i = start; i < end; i++) {
if ((new_line % 2) == 0) {
printf("\n");
printf("(%d)%.4X |", ++reg_number, reg_start_addr++);
}
new_line++;
printf("%02X ", src[i]);
}
printf("\n-----------------\n\n");
}
int xGet_config_from_file()
{
#if 1
char curr_path[MAX_PATH] = { 0 };
char configFile_path[MAX_PATH] = { 0 };
const char* fmt = "%s/%s";
GetCurrentDirectory(MAX_PATH, curr_path);
for (int i = 0; i < strlen(curr_path); i++) {
if (curr_path[i] == 0x5C) { //0x5C \ ·´Ð±¸Ü
curr_path[i] = 0x2F; //0x2F / б¸Ü
}
}
printf("curr path:%s\n", curr_path);
snprintf(configFile_path, sizeof(configFile_path), fmt, curr_path, CONFIG_FILE_NAME);
printf("configFile_path:%s\n", configFile_path);
#endif
struct ini_t* ini_ctl = ini_load(configFile_path);
if (ini_ctl == NULL) {
printf("open file fail,all para use default\n");
return 1;
}
//modbus ´Ó»útcp ipaddr
const char* ip_addr = ini_get(ini_ctl, "MODBUS_SLAVE", "SLAVE_IP_ADDR");
xMaster_Set_slave_ip_addr(ip_addr);
//modbus´Ó»útcp port
const char* tcp_port = ini_get(ini_ctl, "MODBUS_SLAVE", "SLAVE_IP_PORT");
xMaster_Set_slave_port(tcp_port);
ini_free(ini_ctl);
return 0;
}