Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

About C programming questions

Status
Not open for further replies.

maniac84

Full Member level 6
Joined
Mar 4, 2012
Messages
337
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
3,661
Hi guys,
I've been studying other people's c coding lately and I encountered this declaration:
pRs485Struct->theDataStruct.strData = 1;
What I want to know is, why does it contain this "->" sign?
 

When you have a pointer to a struct, you use the arrow operator. When the variable is a struct (in stack), you use the dot:

struct SomeStruct * ss = ...;
struct SomeStruct ss2;

ss->someField = ...;
ss2.someField = ...;

It also seems your "theDataStruct" is either a struct or union, and not a pointer, as its member "strData" is referenced with a dot.
 

Code:
while (rs485.nRxByte != EOT && strlen(rs485.strRxBuffer) < sizeof(rs485.strRxBuffer)-1 &&
        ulTmpTimer>MS_TIMER)
{
          rs485.nRxByte = serDgetc();

        rs485.strRxBuffer[strlen(rs485.strRxBuffer)] = rs485.nRxByte;
}

I've encounter this dynamic c coding too. It's for receiving serial data.
For this line:
rs485.strRxBuffer[strlen(rs485.strRxBuffer)] = rs485.nRxByte;
if I received 10 bytes of data, can it be put into the buffer to become: rs485.strRxBuffer[0], rs485.strRxBuffer[1], rs485.strRxBuffer[2]..... rs485.strRxBuffer[10]
Because normally what I would write is:
rs485.strRxBuffer[i++] = rs485.nRxByte
 

Hai,
You can fill the buffer like that.

If the buffer is cleared in the beginning, strlen gives 0. In while loop, each time the buffer is filled by a byte, strlen(rs485.strRxBuffer) also increments. So,next time, the next byte will be stored in the next location.

Also, strlen for a character array gives the length of the string and sizeof operator gives the total size of the array.
 

Hai,
You can fill the buffer like that.

If the buffer is cleared in the beginning, strlen gives 0. In while loop, each time the buffer is filled by a byte, strlen(rs485.strRxBuffer) also increments. So,next time, the next byte will be stored in the next location.

Also, strlen for a character array gives the length of the string and sizeof operator gives the total size of the array.

Thanks.
By the way, for using serDgetc(), it will know that I have stored it to rs485.nRxByte and move to the next value right?
 

Hai,
serDgetc() reads a byte, when the while() condition is satisfied.
It stores the byte to rs485.nRxByte.
For the first time strlen gives 0, so rs485.nRxByte will be stored to rs485.strRxBuffer[0].

Second time,strlen gives 1 (buffer contains one character) . So, this time rs485.nRxByte will be stored to rs485.strRxBuffer[1] and so on....
 
Hai,
serDgetc() reads a byte, when the while() condition is satisfied.
It stores the byte to rs485.nRxByte.
For the first time strlen gives 0, so rs485.nRxByte will be stored to rs485.strRxBuffer[0].

Second time,strlen gives 1 (buffer contains one character) . So, this time rs485.nRxByte will be stored to rs485.strRxBuffer[1] and so on....
So, does this means the speed of this while loop have to be the same as the speed of receiving data of rs485. Am I right?
Or else if the speed of receiving data is faster than this while loop, it wil always get the last character only. Am I right?
 

If there is speed mismatch, either some bytes will be missed or some bytes will be buffered more than once.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top