GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Apr-2008, 10:22
boschow boschow is offline
Junior Member
 
Join Date: Feb 2007
Location: Slovenia
Posts: 47
boschow can only hope to improve

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:
CPP / C++ / C Code:
                        do
			{
				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
			
			do			
			{
				quantByte++; //how many bytes are we going to send
				bitCounter = bitCounter - 8; // bitCounter = the quqntity of bits
			}while (bitCounter > 0);
			
			do
			{ 
				pucFrame[MBAP + sendByte] = rwCoil->coils[byteNum];
				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,
BoSCHoW.
  #2  
Old 24-Apr-2008, 04:03
rajeev nair rajeev nair is offline
Member
 
Join Date: Aug 2006
Posts: 100
rajeev nair has a little shameless behaviour in the past

Re: problem with reading bits from a remote device


Hi,

Since the code happens to work correctly as expected with VC++ alone My guess is that it has got something got to do with how the variables are getting read or intialized ( array of char byts ).

Quote:
I wrote fixed values in the array char from byts[0] = 0xff; to byts[3] = 0xff;.

is it a single dimension array ?. char byte[0] can store only one character in the array element numbered '0' ( you are storing 0xff).Can you show a snippet of your code where you do this intialization of bytes.

Regards,
Rajeev
  #3  
Old 24-Apr-2008, 09:01
boschow boschow is offline
Junior Member
 
Join Date: Feb 2007
Location: Slovenia
Posts: 47
boschow can only hope to improve

Re: problem with reading bits from a remote device


The system (API) was already made, another developer made it, I just wrote the application on top of it. So the guy that made the system gave me a pointer that points to the non volatile data memory and I use this pointer to access to this registers...

And I think that is nothing wrong if I declare this nvvar (non volatile memory) area like this:
CPP / C++ / C Code:
typedef struct 
{ 
      unsigned char coilReg[200]; 
      unsigned char byteReg[270]; 
      unsigned short shortReg[273]; 
}TNVVar;
 
TNVVar vvar; 
TNVVar *nvvar; 
nvvar = &vvar;

main()
{
     nvvar->coilReg[0] = 0xff;
     /*
      all the other coils were comented
      */
}

I have tested the application, one byte at the time.

Best regards,
BoSCHoW.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
contents of .txt file into 2D array anirudhroxrulz C Programming Language 4 10-Apr-2008 22:45
BMP image on matrix load nemo C++ Forum 21 17-Dec-2007 23:04
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
D3D error when running fullscreen games/programs - help?! daa709 Computer Hardware Forum 4 01-Jul-2005 08:03
Problem with reading from file descriptor philea_mate C Programming Language 2 12-Apr-2005 02:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:35.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.