I just discovered Arduino and it really looks great !
I also have a ethernet shield to communicate with my PC.
In my little project, I am trying to send requests with a particular TCP frame.
I use a FSM and the read() function.
My problem is that when I receive data, I want to be able to count the number of bytes read.
Within a state, I tried :
while (i < length) {
data[i] = client.read();
i++;
}
// go back to initialization state
if (i == length) {
i = 0;
fsm_state = 0;
}
I used print function to debug it but I have the impression that this state is skipped. I guess it is because the read() function doesn't block the process. Thus, if there is nothing to read, the variable i is incremented and then my FSM goes back to the init state.
Hence my question : is there a trick to block the process until my FSM reads something ?
Hence my question : is there a trick to block the process until my FSM reads something ?
The code snippet you posted is not sufficient to answer your question. Typically, one does not try to read unless one knows that the read will be successful. Thus, read() should not block.
The read() function should return a -1 if there was nothing to read, so you can make your own blocking function. Keep reading in a while loop, until read() returns other than -1.
However, there should be an available() method that goes with the read() method that tells you whether there is data to be read (as in how much data, not true/false).
From the snippet, we can not tell what the client object is, though one might reasonably assume that the object is an instance of the Client class, in which case the above comments DO apply. If it is not, then the above comments might not apply.