kiltum / modbus

Very simple modbus for STM32 HAL FreeRTOS
Other
203 stars 76 forks source link

Reference different from 1 does not work #1

Closed erikhaverkamp closed 4 years ago

erikhaverkamp commented 6 years ago

I am using your code for a USART based project. It was very simple to implement, because of the well-documented code. However, I am facing this problem:

This works fine: modpoll.exe -a 1 -r 1 -c 6 COM5

But any -r different from 1 fails:

modpoll.exe -a 1 -r 2 -c 6 COM5
modpoll.exe -a 1 -r 3 -c 6 COM5

Data seems to arrive well in STM32F4, also mb_buf_out seems ok, I don't really know where to go from here?

Thanks

erikhaverkamp commented 6 years ago

I found the solution, there is an error in function ModbusParse:

Please change for Functioncode 3:

 mb_buf_out[mb_buf_out_count++]=(st+nu)*2; // how many bytes we will send?
              for(i=st;i<nu;i++)

To

 mb_buf_out[mb_buf_out_count++]=(nu)*2; // how many bytes we will send?
              for(i=st;i<(st+nu);i++)
erikhaverkamp commented 6 years ago

For functioncode 16:

Change:

for(i=st;i<nu;i++)
                {
                  mb_reg[i]=mb_buf_in[7+i*2]*256+mb_buf_in[8+i*2];
                }

Into:

for(i=0;i<nu;i++)
                {
                  mb_reg[st+i]=mb_buf_in[7+i*2]*256+mb_buf_in[8+i*2];
                }
kiltum commented 4 years ago

Oh , thanks for patch! Push fixes from you ;)

PS Sorry, i was little far away this repo ;)