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.
160 lines
3.1 KiB
160 lines
3.1 KiB
11 months ago
|
#include <stdio.h>
|
||
|
#include <unistd.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <errno.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <modbus.h>
|
||
|
|
||
|
int dataProessing( uint8_t *query , modbus_mapping_t* map ) ;
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
int ret = 0;
|
||
|
|
||
|
modbus_t* ctx = NULL;
|
||
|
modbus_mapping_t* map = NULL;
|
||
|
|
||
|
uint8_t query[MODBUS_RTU_MAX_ADU_LENGTH];
|
||
|
|
||
|
|
||
|
ctx = modbus_new_rtu("/dev/ttyAS3", 115200 , 'N', 8, 1);
|
||
|
if (NULL == ctx)
|
||
|
{
|
||
|
fprintf(stderr, "New Rtu Error: %s\n", modbus_strerror(errno));
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
|
||
|
ret = modbus_set_slave( ctx, 1 );
|
||
|
if (-1 == ret)
|
||
|
{
|
||
|
printf("设置从机地址失败.. %s\n", modbus_strerror(errno));
|
||
|
modbus_free(ctx);
|
||
|
return 1;
|
||
|
}
|
||
|
#if 0
|
||
|
ret = modbus_set_debug(ctx, TRUE);
|
||
|
if (-1 == ret)
|
||
|
{
|
||
|
printf("modbus_set_debug failed...\n");
|
||
|
modbus_free(ctx);
|
||
|
return 1;
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
|
||
|
ret = modbus_connect(ctx);
|
||
|
if (-1 == ret)
|
||
|
{
|
||
|
fprintf(stderr, "打开串口失败: %s\n", modbus_strerror(errno));
|
||
|
modbus_free(ctx);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
map = modbus_mapping_new_start_address( 0 , 0 , 0 , 0 , 30000 , 24 , 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 ;
|
||
|
}
|
||
|
|
||
|
for(int i = 0 ; i < 135 ; i++ )
|
||
|
{
|
||
|
map->tab_input_registers[i] = i * 2 ;
|
||
|
}
|
||
|
|
||
|
while (1)
|
||
|
{
|
||
|
map->tab_registers[0]++ ;
|
||
|
memset(query, 0, sizeof(query));
|
||
|
|
||
|
//获取查询请求报文
|
||
|
ret = modbus_receive(ctx, query);
|
||
|
if (ret >= 0)
|
||
|
{
|
||
|
//恢复响应报文
|
||
|
modbus_reply(ctx, query, ret, map);
|
||
|
dataProessing( query , map ) ;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
printf("Connection close\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
printf("Quit the loop: %s\n", modbus_strerror(errno));
|
||
|
|
||
|
|
||
|
modbus_mapping_free(map);
|
||
|
|
||
|
modbus_close(ctx);
|
||
|
modbus_free(ctx);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
int dataProessing( uint8_t *query , modbus_mapping_t* map )
|
||
|
{
|
||
|
uint16_t address ;
|
||
|
uint16_t address1 ;
|
||
|
uint16_t address2 ;
|
||
|
|
||
|
uint16_t count ;
|
||
|
uint16_t count1 ;
|
||
|
uint16_t count2 ;
|
||
|
#if 0
|
||
|
printf("\n=====================query start======================\n") ;
|
||
|
for(int i = 0 ; i < MODBUS_RTU_MAX_ADU_LENGTH ; i++)
|
||
|
{
|
||
|
printf( "%d\t" , query[i] ) ;
|
||
|
}
|
||
|
printf("\n====================query end==========================\n") ;
|
||
|
|
||
|
return 0 ;
|
||
|
#endif
|
||
|
printf("\n=============================================================\n") ;
|
||
|
printf("从机设备地址为:%d\n" , query[0] ) ;
|
||
|
if( query[1] == 3 )
|
||
|
{
|
||
|
printf("03 读取Read Holding Registers寄存器\n") ;
|
||
|
}
|
||
|
else if( query[1] == 4 )
|
||
|
{
|
||
|
printf("04 读取Read Input Registers寄存器\n") ;
|
||
|
}
|
||
|
address1 = query[2] << 8 ;
|
||
|
address2 = query[3] & 0x00ff ;
|
||
|
address = address1 | address2 ;
|
||
|
printf("读取的起始地址为:%d\n" , address) ;
|
||
|
|
||
|
count1 = query[4] << 8 ;
|
||
|
count2 = query[5] & 0x00ff ;
|
||
|
count = count1 | count2 ;
|
||
|
printf("读取的起始地址为:%d\n" , count) ;
|
||
|
|
||
|
printf("----------tab_registers----------\n") ;
|
||
|
for(int i = 0 ; i < 24 ; i++)
|
||
|
{
|
||
|
printf("%d\t" , map->tab_registers[i] ) ;
|
||
|
}
|
||
|
|
||
|
printf("\n----------tab_input_registers----------\n") ;
|
||
|
for(int i = 0 ; i < 135 ; i++ )
|
||
|
{
|
||
|
printf("%d\t" , map->tab_input_registers[i] ) ;
|
||
|
}
|
||
|
printf("\n=====================================================================\n" ) ;
|
||
|
|
||
|
return 0 ;
|
||
|
}
|