How to assign Modbus setting into MC9RS08KB4 chip ?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to assign Modbus setting into MC9RS08KB4 chip ?

646 Views
joonsinchu
Contributor II

Dear All,

 

do anyone has any experience or sample coding for assigning Modbus setting into MC9RS08KB4 chip ? Currently, i am working on a project for this. My chip is able to transmit and receive signals via RS485 but the MOXa gateway is unable to recognize the device address to communication purposes.

 

Anyone can help one this ?

 

Your reply is highly appreciated.

 

Thank you.

Labels (1)
Tags (1)
0 Kudos
3 Replies

461 Views
scottm
Senior Contributor II

Have you looked at the RS485 bus with a logic analyzer to see what's going on?

The only time I've implemented Modbus-RTU on an 8-bit micro was for a hobby project a few years back.  I instrumented my travel trailer's holding tanks and power distribution system with sensors and used Modbus-RTU for the bus.  I remember it being fairly straightforward.

Without a little more information or a capture of what's going on on the bus, all I can do is speculate.  Make sure your CRC calculation is correct.  If you can post an example of a frame that your Modbus code is sending I could see if it at least looks valid.

Scott

0 Kudos

461 Views
joonsinchu
Contributor II

Hi Scott,

pastedImage_0.png

any idea why there is an error for this ? i checked my overall code all my bracket is correct, but this error is keep poping out.

and can i add in the header file of #include <iostream> and using namespace std; into the code ? or CW has it owns files for these ?

Thank you. Hope to hear from you soon.

0 Kudos

461 Views
scottm
Senior Contributor II

Hi,

It looks like your first problem is that you're making assignments outside of a function - the "ch[16] = 0x0a" won't work.  That would compile to actual code and would have to be inside a function.  You could initialize the entire array where it's declared:

unsigned char ch[4] = {1, 2, 3, 4};

But you can't make assignments there.  As for <iostream> and the namespace, those are C++ features.  You're using a very low-end MCU with 4 KB of flash and 126 bytes of RAM.  C++ really isn't an option at that level.  In pure C you can certainly implement a Modbus sender in a device like that - my own implementation was 1234 bytes of flash and 229 of RAM + 64 bytes stack, and most of that was for buffering ADC samples, but if you don't have much embedded C experience you're not going to have a lot of margin on a device like that.

In your other thread you mentioned that you were trying to implement Modbus ASCII.  The fact that you've got a CRC 16 function here makes me think you're implementing Modbus RTU, since Modbus ASCII uses just a simple arithmetic sum, but your attempt to initialize ch[16] with a linefeed character looks like a Modbus ASCII message.  Which exactly are you trying to implement?

Also, if it's Modbus RTU you're going for, are you sure that's the right CRC 16 calculation?  I can't see the whole thing but that doesn't look like the right polynomial.  Is that maybe CRC-16-CCITT?

Scott

0 Kudos