Pages: 1 [2]   Go Down
Author Topic: How do I detect if I'm receiving serial data.  (Read 1027 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your solution checks if there is serial data. If not, it returns immediately. You have no idea how long the serial buffer has been empty. Maybe that's OK. Maybe not.

If there is as-yet-unread serial data, it records how much, and waits the entire amount of time defined.

If any data arrives during that time, it again waits the entire amount of time before checking whether new data has arrived.

Between when that function ends and the next function gets called, to actually send the data, more data could have arrived.

Nothing else can happen during those delays (and, yes I recognize that they are short).

Mine, on the other hand, checks on every pass through loop, and records when there is a change in the amount of data.

The delays only occur when there is data to send. My function waits only if there has been activity on the bus, and as soon as the bus has been idle long enough starts to send data.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8589
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
tell me HOW I could detect an edge on the RX pin (how would I wire it up) and how to attach an interrupt handler method.
You can wire one of the INT pins to RXD and use attachInterrupt(). 

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

DK
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Paul.

Thanks for your thorough answer!

Your solution checks if there is serial data. If not, it returns immediately. You have no idea how long the serial buffer has been empty. Maybe that's OK. Maybe not.
If there is as-yet-unread serial data, it records how much, and waits the entire amount of time defined.

That's okay. As I empty the Serial as the first thing in my loop() (see reply #4 in this thread).

If any data arrives during that time, it again waits the entire amount of time before checking whether new data has arrived.

Between when that function ends and the next function gets called, to actually send the data, more data could have arrived.

That's true - but your method  wouldn't catch that either. That's why I choose to wait and then check again, before start sending if new data was received since the buffer was emptied.

Nothing else can happen during those delays (and, yes I recognize that they are short).

Mine, on the other hand, checks on every pass through loop, and records when there is a change in the amount of data.

The delays only occur when there is data to send. My function waits only if there has been activity on the bus, and as soon as the bus has been idle long enough starts to send data.

You cannot know if data has been received between the first check in the loop() function and the call to the sendmessage() function (the lastSerial variable would still contain the time stamp we polled for serial data, not the time the last data was received). And that's the issue here! Both yours and my solution are workarounds. The *right* way would be to subscribe to a interrupt that fires when a byte is received - and then update a time stamp. That way a method as shown in my opening post could be used, which is a mix between yours and my attempt to make the workaround smiley

Thanks for your contribution so far! When I find the right solution (for me that is) I'll post an update to this thread!
Logged

DK
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can wire one of the INT pins to RXD and use attachInterrupt(). 

Thanks - I will try to work it out..
Logged

Pages: 1 [2]   Go Up
Jump to: