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

184 lines
3.8 KiB

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <modbus.h>
int dataProcessing( uint8_t* query , modbus_mapping_t* map) ;
//TCP模式的Slave端程序
int main(void)
{
int ret = 0 ;
int sockfd = -1 ;
modbus_t* ctx = NULL ;
modbus_mapping_t* map = NULL ;
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH] ;
//1. 设置串口信息
ctx = modbus_new_tcp("192.168.1.231", 1502) ;
if (NULL == ctx)
{
fprintf(stderr, "Error: %s\n", modbus_strerror(errno)) ;
return 1 ;
}
else
{
printf("设置TCP信息成功\n") ;
}
map = modbus_mapping_new_start_address(0 , 0 , 0 , 0 , 30000 , 25 , 40000 , 135) ;
if (NULL == map)
{
fprintf(stderr, "Error: mapping %s\n", modbus_strerror(errno)) ;
modbus_free(ctx) ;
return 1 ;
}
for(int i = 0 ; i < 24 ; i++)
{
map->tab_registers[i] = i ;
}
map->tab_registers[24] = 999 ;
//浮点测试
//map->tab_registers[2] = 0x0020 ;
//map->tab_registers[3] = 0xF147 ;
for(int i = 0 ; i < 135 ; i++)
{
map->tab_input_registers[i] = i * 2 ;
}
//4. 开始监听端口
sockfd = modbus_tcp_listen(ctx, 1) ;
if (-1 == sockfd)
{
printf("modbus_tcp_listen failed...\n") ;
modbus_free(ctx) ;
return 1 ;
}
//5. 接受客户端连接
ret = modbus_tcp_accept(ctx, &sockfd) ;
if (-1 == ret)
{
printf("modbus_tcp_accept failedl: %s\n", modbus_strerror(errno)) ;
modbus_free(ctx) ;
return 1 ;
}
//6. 循环接受客户端请求,并且响应客户端
while (1)
{
memset(query, 0, sizeof(query)) ;
//获取查询请求报文
ret = modbus_receive(ctx, query) ;
if (ret >= 0)
{
//恢复响应报文
modbus_reply(ctx, query, ret, map) ;
dataProcessing( query , map ) ;
}
else
{
printf("Connection close\n") ;
modbus_close(ctx) ;
//等待下一个客户端连接
modbus_tcp_accept(ctx, &sockfd) ;
}
}
printf("Quit the loop: %s\n", modbus_strerror(errno)) ;
//6. 释放内存
modbus_mapping_free(map) ;
//7. 关闭设备
modbus_close(ctx) ;
modbus_free(ctx) ;
return 0 ;
}
int dataProcessing( uint8_t* query , modbus_mapping_t* map )
{
uint16_t address ;
uint16_t address1 ;
uint16_t address2 ;
int i = 0 ;
//浮点处理
uint16_t floatArray[2] = {0} ;
float floatting ;
//unsigned 转 signed
uint16_t sint ;
//尝试处理浮点数
floatArray[0] = map->tab_registers[2] ;
floatArray[1] = map->tab_registers[3] ;
floatting = modbus_get_float_dcba(floatArray) ;
//将unsigned转为signed
if(map->tab_registers[4] & 0x8000)
{
sint = (~(map->tab_registers[4])) + 1 ;
}
else
{
sint = map->tab_registers[4] ;
}
printf("\n请求序列号:%d\n" , query[1] ) ;
if( query[7] == 4)
{
printf("读取Input Registers寄存器\n") ;
}
else if(query[7] == 3)
{
printf("读取Holding Registers寄存器\n") ;
}
address1 = query[8] << 8 ;
address2 = query[9] & 0x00ff ;
address = address1 | address2 ;
printf("本次读取的起始地址为:%d\n" , address) ;
printf("本次读取个数为:%d个\n" , query[11] ) ;
//打印信息
printf("\n======================================================================\n") ;
printf("\n----------map->tab_registers----------\n") ;
for(i = 0 ; i < 24 ; i++)
{
printf("%d\t" , map->tab_registers[i]) ;
}
printf("\n----------map->tab_input_registers----------\n") ;
for(i = 0 ; i < 135 ; i ++)
{
printf("%d\t" , map->tab_input_registers[i]) ;
}
printf("\n----------float DC BA ----------\n");
printf("%f\n" , floatting) ;
printf("----------signed int ----------\n");
if(map->tab_registers[4] & 0x8000)
{
sint = (~(map->tab_registers[4])) + 1 ;
printf("-%d\n" , sint) ;
}
else
{
sint = map->tab_registers[4] ;
printf("%d\n" , sint);
}
printf("======================================================================\n") ;
map->tab_registers[0]++ ;
return 0 ;
}