Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   problem with reading bits from a remote device (http://www.go4expert.com/forums/reading-bits-remote-device-t10202/)

boschow 23Apr2008 22:06

problem with reading bits from a remote device
Hello all,

i am programming a micro controller with C programming language. I have successfully implemented the MODBUS communication protocol but now I have encountered a bug.

Let me explain how does the communication works:
from the PC i send a request to read multiple coils (bits), this request
tells the remote device the starting address (the address from which we want to start the reading of coils) and the quantity of bits.

In the program I calculate from the starting address the starting element of the char array, and then I calculate the number of bytes from the quantity of bits. Then I make a loop and cycle it until the number of sending bits is equal to the number of quantity of bytes.

The code looks something like this:

                                index = index + 8; // bit index
                                byteNum++; // the starting char array element
                        }while (address > index); // address = starting address

                        byteNum -= 1; //the first element of the char array
                                quantByte++; //how many bytes are we going to send
                                bitCounter = bitCounter - 8; // bitCounter = the quqntity of bits
                        }while (bitCounter > 0);
                                pucFrame[MBAP + sendByte] = rwCoil->coils[byteNum];
                                sendByte++; // how many bytes are we sending at the beginning  is 0
                        }while(sendByte < quantByte); // loop until all  the requested bytes are send

The code works like this:
I wrote fixed values in the array char from byts[0] = 0xff; to byts[3] = 0xff;.
When I wrote the 4th byte of array the rezult was strange, because both the byte[4] was also affected with the number but instead of setting on 0xff , it set to 0xf7. I also tried to set one bit at the time, and I found out that when I set the first bit of the byte[3] array char both the first and the last
were changed from logic 0 to logic 1. When I changed the second bit of byte[3] it change its second bit to logic 1 and the first bit on byte[4]. One last thing when I observe the reading of bits one byte at the time it works fine, maybe because I can't see the effect of the other array of char elements.

I have tested this code with Visual C++ 2008 and it works, everything is at the right place and this is why I can understand why on the micro controller doesn't work. Anyone has any idea about this?

Best Regards,

All times are GMT +5.5. The time now is 19:34.