View previous topic :: View next topic |
Author |
Message |
lisek_lichu
Joined: 27 Aug 2012 Posts: 23
|
Modbus CRC variable modbus_serial_crc.d |
Posted: Thu Aug 15, 2013 12:58 pm |
|
|
Hello,
can someone help me and explain for what is 16 bit variable modbus_serial_crc.d in modbus.c driver file?
modbus_serial_crc.b is used do calculate CRC code but where we use modbus_serial_crc.d?
It is always set to 0xFFFF and only in timer2 interrupt routine we check if it is 0x0000 but when it could change to 0x0000?
Simon |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Thu Aug 15, 2013 2:11 pm |
|
|
Read up on unions.
Unions in C allow two separate variables to be put in the same space.
modbus_serial_crc.b[0], and b[1], are the two _bytes_ of the variable.
modbus_serial_crc.d, is the single 16bit variable containing the two bytes.
It is an efficient way of allowing you to deal with the bytes separately, and then access the 16bit result.
Best Wishes |
|
|
lisek_lichu
Joined: 27 Aug 2012 Posts: 23
|
|
Posted: Fri Aug 16, 2013 6:11 am |
|
|
Hi Ttelmah,
I understand what is union and for what is it but my question was a little different.
I asked for what is 16 bit variable modbus_serial_crc.d and not what is union
in modbus.c code while we calculate CRC we never use variable modbus_serial_crc.d in union we only operate on modbus_serial_crc.b so my question is for what is D? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Fri Aug 16, 2013 7:36 am |
|
|
The union is the answer....
The point is that the two bytes, crc.b[0], and b[1] are generated and updated in the other code, then to check the whole 16bit value in one go, the two bytes are accessed together as a single 16bit value. crc.d
Best Wishes |
|
|
lisek_lichu
Joined: 27 Aug 2012 Posts: 23
|
|
Posted: Fri Aug 16, 2013 9:13 am |
|
|
Ok i get it, so normally I don't even need 16 bit d value because it can be used if I want to operate in one 16 bit variable instead of two 8bit variables.
Tkats ok,
Can You explain me another thing. In modbus.c code there is an variable "modbus_serial_new" which can be FALSE or TRUE, can You describe me in one sentence what is this variable responsible for? What is it controlling?
Some "if" statements are checking this variable and depending on result program does different code.
in RDA interrupt we can read incoming char only if modbus_serial_new==FALSE so it means that we can read byte only if serial is in middle of sending a whole frame?
But if we are sending address and function in "modbus_serial_send_start()" we change it to FALSE.
Also it is used in modbus_kbhit() but I can't explain myself how to describe this variable to understand code's behavior.
Can You explain it to me?
Simon |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Fri Aug 16, 2013 2:00 pm |
|
|
It flags when the driver is looking for a new packet. Turns off the timeout while this is happening (since there can be gaps between packets) etc..
Best Wishes |
|
|
lisek_lichu
Joined: 27 Aug 2012 Posts: 23
|
|
Posted: Wed Sep 04, 2013 1:44 pm |
|
|
Hi,
I have analyzed this code and right now I think that I understand it.
It goes like this:
MASTER
in send_start function we set modbus_serial_new=FALSE so it means that we are waiting for new response from slave (because we send a request). Then we are waiting for RDA interrupt from slave. In RDA interrupt we check if modbus_serial_new==FALSE so it means that we will save byte only if we are still waiting for response. We are reading and saving all bytes in RDA interrupt. then after we receive last byte from slave the RDA interrupt will not be run but every time in RDA interrupt we clear timer2 so if no longer BYTES come from slave the timer 2 will trigger TMR interrupt and it will change modbus_serial_new=TRUE an then it turns off tmr2 interrupt so it means that from this time no timer2 is active and we will not receive anything because we are not waiting for anything (modbus_serial_new=TRUE). Only if we send another request to SLAVE then again in send start() we will change modbus_serial_new=FALSE and so on...
Maybe it will be helpful for others |
|
|
|