stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.44k stars 1.75k forks source link

Not getting connection error when supposed to #678

Open eduardo4jesus opened 1 year ago

eduardo4jesus commented 1 year ago

libmodbus version

$ pkg-config --modversion libmodbus
3.1.6

OS and/or distribution

$uname -a
Linux vostro3910 5.15.0-58-generic #64-Ubuntu SMP Thu Jan 5 11:43:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Environment

I am using a SV2M-220N servo driver, which I connect via RTU

$ sudo lshw -c cpu
  *-cpu                     
       description: CPU
       product: 12th Gen Intel(R) Core(TM) i5-12400
       vendor: Intel Corp.
       physical id: 400
       bus info: cpu@0
       version: 6.151.5
       slot: U3E1
       size: 3265MHz
       capacity: 5600MHz
       width: 64 bits
       clock: 100MHz
       capabilities: lm fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp x86-64 constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l2 invpcid_single cdp_l2 ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr flush_l1d arch_capabilities cpufreq
       configuration: cores=6 enabledcores=6 microcode=34 threads=12
$ sudo lshw -c network
  *-network                 
       description: Wireless interface
       product: Intel Corporation
       vendor: Intel Corporation
       physical id: 14.3
       bus info: pci@0000:00:14.3
       logical name: wlp0s20f3
       version: 11
       serial: 00:93:37:fd:0e:7b
       width: 64 bits
       clock: 33MHz
       capabilities: pm msi pciexpress msix bus_master cap_list ethernet physical wireless
       configuration: broadcast=yes driver=iwlwifi driverversion=5.15.0-58-generic firmware=64.97bbee0a.0 so-a0-hr-b0-64.uc ip=10.246.66.77 latency=0 link=yes multicast=yes wireless=IEEE 802.11
       resources: iomemory:600-5ff irq:18 memory:6003234000-6003237fff
  *-network
       description: Ethernet interface
       product: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
       vendor: Realtek Semiconductor Co., Ltd.
       physical id: 0
       bus info: pci@0000:03:00.0
       logical name: enp3s0
       version: 15
       serial: c0:25:a5:c3:0c:6a
       capacity: 1Gbit/s
       width: 64 bits
       clock: 33MHz
       capabilities: pm msi pciexpress msix bus_master cap_list ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
       configuration: autonegotiation=on broadcast=yes driver=r8169 driverversion=5.15.0-58-generic firmware=rtl8168h-2_0.0.2 02/26/15 latency=0 link=no multicast=yes port=twisted pair
       resources: irq:18 ioport:3000(size=256) memory:74004000-74004fff memory:74000000-74003fff

Description

I have this code

  /** Instantiate a Context */
  rtu_context_ = modbus_new_rtu(device_description_.c_str(), baud_, parity_, data_bit_, stop_bit_);
  if (rtu_context_ == nullptr) {
    return static_cast<uint32_t>(errno);
  }

  /** Adding the Slave Address to the Context (Device Connection)  */
  auto ret = modbus_set_slave(rtu_context_, static_cast<int>(device_address_));
  if (ret == -1) {
    return static_cast<uint32_t>(errno);
  }

  SPDLOG_DEBUG("modbus_set_slave ret={}", ret);

  /** Connecting to Device */
  ret = modbus_connect(rtu_context_);
  if (ret == -1) {
    return static_cast<uint32_t>(errno);
  }

  SPDLOG_DEBUG("modbus_connect ret={}", ret);

  SPDLOG_DEBUG("Connected");

Actual behavior if applicable

This code above works really good when the servo is ON with the cable connected. However, when the cable is unplug or the device is off, I was hoping to have an error being returned and definitely NOT see the message "Connected" printed. This is what I get.

modbus_set_slave ret=0
modbus_connect ret=0
Connected

I only get something different than this, with I provide a device_description_ that does not exists.

Expected behavior or suggestion

To receive errors in the return of the functions modbus_new_rtu, modbus_set_slave and modbus_connect according to the online documentation.

Steps to reproduce the behavior (commands or source code)

Provided above.

libmodbus output with debug mode enabled

Code with debug set:

  /** Instantiate a Context */
  rtu_context_ = modbus_new_rtu(device_description_.c_str(), baud_, parity_, data_bit_, stop_bit_);
  if (rtu_context_ == nullptr) {
    return static_cast<uint32_t>(errno);
  }

  auto ret = modbus_set_debug(rtu_context_, true);
  if (ret == -1) {
    SPDLOG_DEBUG("Cant set debug flag");
  }

  /** Adding the Slave Address to the Context (Device Connection)  */
  auto ret = modbus_set_slave(rtu_context_, static_cast<int>(device_address_));
  if (ret == -1) {
    return static_cast<uint32_t>(errno);
  }

  SPDLOG_DEBUG("modbus_set_slave ret={}", ret);

  /** Connecting to Device */
  ret = modbus_connect(rtu_context_);
  if (ret == -1) {
    return static_cast<uint32_t>(errno);
  }

  SPDLOG_DEBUG("modbus_connect ret={}", ret);

  SPDLOG_DEBUG("Connected");

Output

modbus_set_slave ret=0
Opening /dev/ttyB08P0 at 115200 bauds (N, 8, 2)
modbus_connect ret=0
Connected

PS: The only error I am getting when not having a connection is connection time outs from read and write operation.