Do until Serial1.BytesAvailable > 100

It’s not you Xojo, it’s me :slight_smile:

Firstly the value of 100 is arbitrary. Secondly, the Serial Port has been opened and all was working well until I started to put together previously worked parts. Thirdly, data is always available at the port (at a rate of 100 samples per second), so I can’t see how the condition is never met.

This is the offending code:

Do until Serial1.BytesAvailable > 100 tempBuffer = Serial1.ReadAll(encodings.ASCII) loop

What I was thinking should happen is that the loop continues until at least 100 bytes are read from the serial port, then that value is assigned to tempBuffer.

Unfortunately it causes the program to a hang (Not Responding). I don’t get any errors and have to abort using the debug stop icon. I’ve tried debugging including a hard coded break, but even that does not allow me to “see” what is happening either.

I’ve spent at least 2hrs reading stuff from the web, and none the wiser. I’m left with wondering if this is a debugging issue or poor programming on my behalf.

Hopefully it’s just me.

Steve.

[code]If Serial.Lookahead(encodings.ASCII).LenB > 100 Then

Your code here…

End if
[/code]

In the event.

*DataAvailable :smiley:

Thanks Derk, that looks very promising. I’ll check it out tomorrow - getting late here.

Sascha, thanks, but I’m purposefully not using the DataAvailable event. I want to read the data when “I” say so.

Cheers.

What happens if bytesAvailable is > 100 initially? From the code you posted, bytes will never be read.

Else you start to read all available bytes into TempBuffer. So bytesAvailable will be reset. If the serial doesn’t have at least 100 Bytes during the next round, you will again read into TempBuffer and, depending on serial1’s speed, the loop end condition may never be read.

It would look different if your code were

Do until Serial1.BytesAvailable > 100 loop tempBuffer = Serial1.ReadAll(encodings.ASCII)

Or are there other parts of your code that catch these circumstances?

And unless you poll the port, your loop will prevent it from receiving any data.

[quote=287103:@Steve K]Thanks Derk, that looks very promising. I’ll check it out tomorrow - getting late here.

Sascha, thanks, but I’m purposefully not using the DataAvailable event. I want to read the data when “I” say so.

Cheers.[/quote]
Serial.Lookahead
Wont remove the data. Use Serial.Read( ) when you want to read the data.

that’s event based programming.

Thanks Ulrich and all.

It’s embarrassing that I didn’t see the obvious - things became a bit clearer this morning after a good sleep. :slight_smile:

After some experimenting, none of the above suggetions worked. In the end I used a timer and then everything fell into place.

I guess my mind is still looking at programming in a “procedural” sense, and difficult to see the “event driven” side. I’d prefer that I make that “event” happen.

Nevertheless, I got this working perfectly. :slight_smile:

Cheers.