epsilonrt / libmodbuspp

Much more than a C++ wrapper for libmodbus
https://epsilonrt.fr/modbuspp
GNU Lesser General Public License v3.0
73 stars 44 forks source link

Invalid CRC when reading from Holding Registers #4

Closed chetan-chadha closed 4 years ago

chetan-chadha commented 4 years ago

I was trying to fetch data from the modbus simulator we have, but the response which i am getting with the example cpp program for reading from holding registers says

Unable to read input registers ! Invalid CRC

The same simulator i tested with other wrappers around libmodbus they were able to interpret the values and display the result. I am also attaching the diagnostics msgs from the simulator

Tx-> 01 03 04 00 00 40 40 CA 03 Rx-> 01 03 00 F8 00 02 45 FA

epsilonrt commented 4 years ago

I did not notice this problem. You have to describe the problem to me precisely ...

Have you activated debug mode of libmodbuspp to see the frames?

Please send me the information and a copy of the debug information.

chetan-chadha commented 4 years ago

Currently I had build libmodbuspp with libmodbus on ubuntu-tegra (linux-arm) running on Jetson nano. I am using the example provided under libmodbuspp to read-holding-registers MODE is RTU I activated the debug mode manually and the output is as follows:

[01][03][00][F8][00][02][45][FA]
Waiting for a confirmation...
<01><20><10><00><02>
ERROR CRC received 0x2 != CRC calculated 0x380C
Unable to read input registers ! Invalid CRC_test

While simulator is showing the same debug msgs as mentioned earlier:

Tx-> 01 03 04 00 00 40 40 CA 03
Rx-> 01 03 00 F8 00 02 45 FA 

I tried using pymodbus an alternative for the same even minimalmodbus they were able to read and provide me with appropriate value as given by the simulator.

The simultor from which i am trying to read is "Micro Motion Sense series 3000 MVD".

source code attached:

main.txt

epsilonrt commented 4 years ago

Ok, this problem is clearly not related to libmodbuspp. For me the problem is linked either to a problem of DE signal management, or to a problem of line polarization resistors. An RTU frame is delimited by a silence greater than 3.5 characters, but obviously, the slave or the transmission line causes this scenario before the actual end of the message.

Could you explain to me how master and slave are connected? RS485 or RS232? The MMS3000 computer does not have RS232, so it is probably RS485, in this case: You have not activated the management of RTS while you are using /dev/ttyAMA0 which is a hardware port, can you explain to me why?

Your frame captures do not match: the master sends to [01] [03] [00] [F8] [00] [02] [45] [FA], the slave receivesRx-> 01 03 00 F8 00 02 45 FA but I don't see the slave's answer? It should start with <01><20><10><00><02> ! But this answer does not match the demand! the answer should start with <01><03><04>

Once you have explained to me the physical link, which returns the slave, we can do a test with mbpoll

chetan-chadha commented 4 years ago

Yes, you were right with respect to that!! . The setup is like i have connected CH340G and arduino they provide me 2 usb ports as ftdi cable for serial communication is not available. Earlier i was connecting Arduino com port (ttyACM0) with Jetson Nano where i was running my libmodbuspp script and my system is connected to CH340G (ttyUSB0) when i reversed the connection it worked now the debug msgs are:

with if (slv.readRegisters (249, registers, 1) >= 0)

[01][03][00][F8][00][02][45][FA]
Waiting for a confirmation...
<01><03><04><00><22><00><22><DA><20>

when i used: if (slv.readRegisters (249, registers, 2) >= 0)

[01][03][00][F8][00][04][C5][F8]
Waiting for a confirmation...
Length to read 2<01><03><08><00><22><00><22><00><00><00><00><6F><D2>

When i was trying to read from libmodbus**** test code for libmodbus: libmod_test.txt with: modbus_read_registers(ctx, 248, 2, reg); output:

[01][03][00][F8][00][02][45][FA]
Waiting for a confirmation...
<01><03><04><00><22><00><22><DA><20>

with modbus_read_registers(ctx, 248, 4, reg); output:

[01][03][00][F8][00][04][C5][F8]
Waiting for a confirmation...
<01><03><08><00><22><00><22><00><00><00><00><6F><D2>
  1. I think its trying to read 2 registers from the slave when i am requesting for 1 something similar i observed in case of writing_registers also.
  2. Is the support available for "little endian byte swap" or i need to manipulate the bits manually as i am new to these formats any response will be very helpful.
  3. Can we expect libmodbuspp to support ttyACM0 as a serial port in the coming future.
epsilonrt commented 4 years ago

libmoduspp uses libmodbus to access the network and works very well. obviously, you do not have the necessary knowledge to criticize this library which works very well including on ttyAMA0 link. You initially hid from me the use of Arduino, but on this platform, the implementation of MODBUS is very often dubious with the exception of that of freemodbus I invite you to question yourself before criticizing the work of experts, and therefore to read in its entirety the reference documents of modbus.org because this site is not intended to provide university courses but to signal bugs.

chetan-chadha commented 4 years ago

Sorry, for wasting your time. I tried to read and write with libmodbus, pymodbus, minimalmodbus it was working fine with all of them with same configuration reading with Arduino only. I thought that might be an issue thats why raised the same.

I was not criticizing anyways i am thankful that community is putting a lot of effort and making it available to all of us and supporting us even with dumbest questions like i might had asked......

Thanks i will read more and try to get more knowledge wrt signals

I got that part it was my mistake of using Arduino...

But other issues which i faced like when i am requesting for 1 register its reading 2 registers. same was the case with writing can you help me with that if its some sort of error from my end or something. Request format as per my understanding is:

11 03 006B 0003 7687

11: The Slave Address (11 hex = address17 )
03: The Function Code 3 (read Analog Output Holding Registers)
006B: The Data Address of the first register requested.
             ( 006B hex = 107 , + 40001 offset = input #40108 )
0003: The total number of registers requested. (read 3 registers 40108 to 40110) 
7687: The CRC (cyclic redundancy check) for error checking.

But the string which i am requesting from the modbus slave when i am explicitly mentioning either is: [01][03][00][F8][00][02][45][FA]

slv.readRegister (249, registers) or slv.readRegisters (249, registers,1)

The only problem is reading odd number of register if in case required i know its rare but reading one register is required.