Hello.
When using if statement I can read the input written into the buffer and then trigger a function.
I'm trying to learn how I i can do the same when nothing has been sent to the serial port.
Example:
When letter A has been sent to the serial port and has been recognised by char, because it is greater than 0, I can get it to trigger a function.
Now I like to do the same, but this time nothing has been sent and therefore nothing will be greater than 0.
From what I have learnt this is not possible using an if statement as it requires that there has been sent something to the serial port. So I wonder how you can do it?
A short question without explanation would be: Which code is used to determine when something has been or has not been sent to the serial port?
I cannot post any code as I don't have one. And forgive me if I use wrong terms as I'm new learning to code. I hope you understand my anyway. Else please let me know and I will do my best to make it more clear what I try to learn.
You need to start working through the provided Arduino examples, in order to learn the language and special features of the Arduino. There are plenty of on line tutorials as well.
If you skip this step, you won't have the knowledge required to ask a good question, and get a good answer.
But, FYI, there is a special Arduino function that tells you if one or more characters arrived via the serial port. Covered in the examples, of course.
Also, please read the "How to use this forum" post.
Hello.
Thank you for your answer.
What you tell me I do already know.
I assume that the special Arduino function you refer to is the following:
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
Now this is exactly not what I'm looking for. But it was when reading a tutorial about how to use this function I started researching what to do when there is not received any incoming byte.
So I cannot start reading tutorials as I already do this. I also did made further research but can not find anything else than the same function which only work if there has been received at least one byte. Also not by checking tutorials for native C++ I find anything. So I have to ask.
If I knew what this function was named it would not be necessary for me to ask in here, as I for sure would be able to find something about it.
if (Serial.available() == 0)
{
//do something if nothing has been received
}
if (Serial.available() >= 1)
{
//do something if at least 1 character has been received
}
or this:
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
}
else
{ ... do the things you want to do when there is no character received... }
I cannot post any code as I don't have one.
Yet you somehow managed to post some code, which led to helpful responses.
That is why we recommend that new users read and follow the directions in the "How to use this forum" post.
jremington:
Yet you somehow managed to post some code, which led to helpful responses.
The code I posted was a referral to your response. I already knew that this code could not do what I requested. I only posted the code as you referred to a the code doing the complete opposite of what I requested. However the same code works exactly as I described, which is actually written in the how to use this forum post that you can do instead of posting some crap code not making any sense.
I cannot see why should I ever post a code that I already know is not working and do something totally different than what I'm asking for.
I can understand why I should post a code that I would believe could do what I'm trying to archive which also fits into the how to use this forum post.
@Axident, your question really makes no sense. Serial.available() does in fact, perform the function that you are requesting - "Which code is used to determine when something has been or has not been sent to the serial port?". Can you see how that makes it hard to help you?
If there was a "Serial.unavailable()" it would just check Serial.available() and return true if Serial.available() == 0.
But that monkey business belongs in your code, not in a library where you are the only user out of 100,000 that would ever use it.
Your question was answered perfectly. You apparently do not see the difference between testing for a condition and testing for a character.
Paul
Thank you UKHeliBob and aarg.
I can now see that the tutorials here on arduino.cc. is far from complete only describing parts of the code.
But this is the same on so many other sites.
But I figured out how it works and what to do now. Only thing I now miss. What is the part of the code containing == 0 is called. Hopefully the naming can help me finding further informations next time I have questions to a function as it might only be a variation which is not described in a tutorial.
If any should have links to sites describing variations of functions left out in tutorials I would really happy. There must be some sites describing this better than arduino.cc.
aarg:
@Axident, your question really makes no sense. Serial.available() does in fact, perform the function that you are requesting - "Which code is used to determine when something has been or has not been sent to the serial port?". Can you see how that makes it hard to help you?
If there was a "Serial.unavailable()" it would just check Serial.available() and return true if Serial.available() == 0.
But that monkey business belongs in your code, not in a library where you are the only user out of 100,000 that would ever use it.
I cannot agree with you.
Serial.available is not the part determine if nothing has been sent. In fact serial.available() does in fact not do anything by it.self
But serial.available() combined with >= 0) or >= 1)does in fact determine if something has been sent to the serial port. But if the part with >= 0) is the one and only description in the tutorials how will anyone be able to know that there is variations of this?
I have no idea how much I will ever use to determine if nothing has been sent to the serial port. But why do you bother. I cannot see why this cannot be described like the other variations which has al o not been described. I do assume that a tutorial contain more than only the very basic if it has not been stated that it does not describe anything else.
But I might just have to find tutorials anywhere else than Arduino.cc and ask my questions some where else. Clearly you don't like to help a novice making it clear that there might be terms which is wrong. And it seems like someone has also forgot that once they did not know anything themselves.
Paul_KD7HB:
Your question was answered perfectly. You apparently do not see the difference between testing for a condition and testing for a character.
Paul
You are very right about this. And I never claimed to know it either. But I clearly stated that I'm new learning to code for the Arduino. And honestly how could I know as the tutorials do not describe this either. This is why I ask. Where did you learn the variations of serial reading containing the difference in condition and character?
What is the part of the code containing == 0 is called.
It is simply a test as to whether the value before the == equals the value after it. If they are equal then the test returns true, otherwise it returns false. You then decide what to do based on what is returned
UKHeliBob:
It is simply a test as to whether the value before the == equals the value after it. If they are equal then the test returns true, otherwise it returns false. You then decide what to do based on what is returned
Thank you for the explanation. You put on word for the same I figured out by looking at your two examples.
There are no "variations" on Serial.available(). The documentation is very clear about the meaning of the return value. The importance of knowing how many characters are available is plainly obvious. If you are only interested in knowing whether there are any characters waiting, a simple test for non-equality to zero will yield that. If, on the other hand, you wish to know exactly how many so you can handle them in bulk, you can capture the value and use it to process the characters. The choice between these two methods is so program and programmer dependent that it is indeed fortunate that C does not exclude such "variations" in its use.
You said something about testing Serial.available for the condition '>= 0' in the tutorials. I have never seen that "variation", and if I did, I would recognize it as a bug because it would always be true.
I'm actually happy to be corrected about any terms that I am using incorrectly. But I need to see some evidence before I change my language.
Also, you will likely never see any code that "does" anything when there is no input. Rather, it will always do something if there is input, and simply pass control to the following code if there isn't. That is why I questioned why you need to do it, in reply #7.
aarg:
There are no "variations" on Serial.available(). The documentation is very clear about the meaning of the return value. The importance of knowing how many characters are available is plainly obvious.
I don't know which documentation you refer to. But I refer to the reference site for serial.available() on arduino.cc and the description is like this
“Description
Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer (which holds 64 bytes)."
I cannot see that it is obvious by reading the documentation like you claim it to be. With other words it only describes that there must be data available. No words about that serial.available() can be used otherwise. Also no examples showing that the code could be different like in answer #3. If I had seen an example code like in answer #3 it would not have been necessary for me to ask into this. Instead the site have links referring to many other sites. But it is obvious for me that if you can check if there are at least one byte present, then it should also be possible to determine when there is no byte present.
Are there other sites with better explaining references where this become obvious.? I ask as you must have been reading this somewhere else.
Also, you will likely never see any code that "does" anything when there is no input. Rather, it will always do something if there is input, and simply pass control to the following code if there isn't. That is why I questioned why you need to do it, in reply #7.
If an app or a device are sending to the serial port but only sent an input with number 1, which triggers the port to shift to high, but after this the app or device doesn't sent any further numbers, which can set the port to low. There you have an situation where I would use it. But how would you do the same?
You write you would pass control to the following code. Could you give an example how you would do the same under same circumstances as in my example with the app or device do not sent any further numbers.
I know it would probably be easier program the app or have the devise to sent another number. Or use analog button with a pull-up or a pull-down resistor to do it. But let's say this is not an option, so it has to be coded into the Arduino sketch.
Consider choosing a less stressful hobby. You are getting far too worked up over trivial details.
Axident:
If an app or a device are sending to the serial port but only sent an input with number 1, which triggers the port to shift to high, but after this the app or device doesn't sent any further numbers, which can set the port to low. There you have an situation where I would use it. But how would you do the same?
You write you would pass control to the following code. Could you give an example how you would do the same under same circumstances as in my example with the app or device do not sent any further numbers.
You want to trigger an event, if no serial data has been received for a certain interval? Really, I would just save a time stamp every time I do receive a character. Then, someplace in loop(), I would compare this time stamp with the current time. If the difference exceeds my interval, then I would run the code for the event.
If you know of a serial application that requires instantaneous response whenever the incoming stream of characters is broken, I would really like to know what the reason for it would be. Don't try to tell me that a broken or unreachable sensor would fit this description. I have never seen any sensor with a serial output that depends on continuous transmission and reception. It's completely at odds with the design and intent of asynchronous communications. A time out would make much more sense in that scenario.
jremington:
Consider choosing a less stressful hobby. You are getting far too worked up over trivial details.
I'm not getting stress at all. I'm pretty sure that those who get stress of coding would newer ask into coding anything as I do. I'm sure they find it too trivial. But I'm not one of them. I'm just one of those types who like to know things in details and how and when to use it.
aarg:
You want to trigger an event, if no serial data has been received for a certain interval? Really, I would just save a time stamp every time I do receive a character. Then, someplace in loop(), I would compare this time stamp with the current time. If the difference exceeds my interval, then I would run the code for the event.
If you know of a serial application that requires instantaneous response whenever the incoming stream of characters is broken, I would really like to know what the reason for it would be.
Thank you so much. I will start answering you question. I donk know any software application which would require instantaneous response. But I know machines that does, but they are controlled by logic gate IC's. So they are pure analog devices. I just found it interesting how to archive the same by coding.
It sounds interesting to use a timer instead of reading the input and using if statements. I have no issues to imagine that you can also do it this way, however I don't know how to write the code. So I will keep it in mind but leave this subject to another post, if I cannot find out how the function is working when first I start learning this part of coding.
I really appreciate that you took your time explaining it.