首页 Modbus 正文

回复

求助:COM口支持232422485修改成485模式不成功,什么原因

Modbus 浏览:1878 回复:2 收藏

wznjupt  2019-07-18 17:23

目前用的是研华工控机,和继电器485 A  B 连接

工控机是com 口3 支持232 485

连接两根线和继电器连接上

在工控机上写控制程序RTU模式 :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"modbus.h"
#include <unistd.h>
 
int main(void)
{
  modbus_t *mb;
  uint8_t tab_reg[16];
  memset(tab_reg, 0, 16);

  mb = modbus_new_rtu("/dev/ttyS4",115200,'N',8,1);//open port
  modbus_rtu_set_serial_mode(mb, MODBUS_RTU_RS485);
  if (mb == NULL) {
    fprintf(stderr, "Unable to create the libmodbus context\n");
    return -1;
  }
  modbus_set_slave(mb,1);//set slave address
  int n=modbus_rtu_set_serial_mode(mb, MODBUS_RTU_RS485);
  printf("modbus_rtu_set_serial_mode = %d\n",n);


  int k =  modbus_connect(mb);
  printf("modbus_connect = %d\n",k);
  if(mb && modbus_connect(mb) == -1) {
  printf("\nSorry,fail to connect ttyUSB0,please check your device's state.\n");
  }
 
  k= modbus_rtu_get_serial_mode(mb);
  printf("modbus_rtu_get_serial_mode = %d\n",k);



  modbus_set_slave(mb, 1);
    
  uint8_t dest[4]; //setup memory for data
  uint16_t dest1[4] ={0};
  memset(dest, 0, 4);
  int i=0;
  int ret =0;


 
  if(mb == NULL)
{
   printf("No connection,check your device!\n");
   return -1;
}

 if(dest[1]==1)
 {
   
   for(i = 0; i < 4 ; i++)
   {
     ret = modbus_write_bit(mb, i ,0);

     if(ret==1)
     {
        printf("成功写入数据!返回值:%d\n",ret);
     }
    else
    {
        printf("写入数据失败,返回值:%d\n",ret);
    }
    sleep(2);
   }


 }

else
{
  for( i = 0; i < 4 ; i++)
  {
     ret = modbus_write_bit(mb, i ,0xff);

     if(ret==1)
     {
    printf("成功写入数据!返回值:%d\n",ret);
     }
    else
    {
        printf("写入数据失败,返回值:%d\n",ret);
    }
    sleep(1);
 }

}

  int j=0;
  modbus_read_bits(mb, 0, 4, dest);
 
  int regs = modbus_read_registers(mb,0,4,dest1);
  for(j = 0; j < 4 ; j++)
  {
    printf("\nmodbus_read_registers dest1[%d]=%4d,%4d\n",j,dest1[j],regs);
  }
 
  for(j = 0; j < 4 ; j++)
  {
    printf("\ndest[%d]=%4d\n",j,dest[j]);
  }
  modbus_close(mb);  
  modbus_free(mb);
  return 0;
}


modbus_rtu_set_serial_mode 没有设置成功 返回-1   modbus_rtu_get_serial_mode 返回为0 工控机com为232模式

我想把com口设置成485 模式 因为 连接的继电器是485口  如何解决  modbus_rtu_get_serial_mode 函数内容也看了 :

int modbus_rtu_set_serial_mode(modbus_t *ctx, int mode)
{
    if (ctx->backend->backend_type == _MODBUS_BACKEND_TYPE_RTU) {
#if HAVE_DECL_TIOCSRS485
        modbus_rtu_t *ctx_rtu = ctx->backend_data;
        struct serial_rs485 rs485conf;
        memset(&rs485conf, 0x0, sizeof(struct serial_rs485));

        if (mode == MODBUS_RTU_RS485) {
            rs485conf.flags = SER_RS485_ENABLED;
            if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
                return -1;
            }

            ctx_rtu->serial_mode |= MODBUS_RTU_RS485;
            return 0;
        } else if (mode == MODBUS_RTU_RS232) {
            if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
                return -1;
            }

            ctx_rtu->serial_mode = MODBUS_RTU_RS232;
            return 0;
        }
#else
        if (ctx->debug) {
            fprintf(stderr, "This function isn't supported on your platform\n");
        }
        errno = ENOTSUP;
        return -1;
#endif
    }

    /* Wrong backend and invalid mode specified */
    errno = EINVAL;
    return -1;
} 应该在ioctl 挂的  求如何解决


我知道了