Serial Reading within a Certain Times Arduino

Status
Not open for further replies.

DianWardiana

Junior Member level 3
Joined
Sep 7, 2017
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
189
i want to read the existing serial data, in less than 2 seconds there is already serial data then it will go on next process. More than or equal 2 seconds there is no data in the serial, it will continue in the next process. How to do the code in arduino?
 

The Arduino is a poor platform for reading data through the UART serial interface depending on the intended data rate because this IDE does not support interrupt reception, so the process would have to be done by polling inside the main loop. The interval of 2 seconds is pretty enough to read the serial, but it is not clear what do you mean by "read serial data", it is just 1 byte ? ...or a frame of several bytes ?

An exception to the rule: If the hardware is based on some uC of the AVR family, there is an alternative library in Guithub (not native, but later installed) that allows this feature to be used.

Concerning the the answer to the question: Did you even have a look at any example on the official Arduino website ?
 

Hi,

what do you mean by "read serial data", it is just 1 byte ? ...or a frame of several bytes ?
I wanna see if there is data available in serial. The data is several bytes.

I mean is about read serial with timeout operation. I've look example of the official Arduino website, but i can't found how to do the code.
 

I wanna see if there is data available in serial. The data is several bytes.

It were expected you inform how many bytes are present in the frame. Indeed, this does not add much information, since "several bytes" can lie in the half-dozen range as well as can mean a few dozen bytes, and so the answer given earlier to the initial question is: Maybe it works, maybe not. As said before, being this process of reading done inside the loop(), would be competing with other functions that being executed faster or not, could yield loss of data.

I've look example of the official Arduino website, but i can't found how to do the code.

Again, question vague; without clear specification of what will be read, what other functions will be executed, what protocol, what platform h/w will be used, etc...
 

This is an example of Arduino which uses Serial Interrupt to receive data. Tell me your exact requirement and I will make a code for you and post it here and explain its working.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
  Serial Event example
 
  When new serial data arrives, this sketch adds it to a String.
  When a newline is received, the loop prints the string and clears it.
 
  A good test for this is to try it with a GPS receiver that sends out
  NMEA 0183 sentences.
 
  NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
  other ATmega32U4 based boards.
 
  created 9 May 2011
  by Tom Igoe
 
  This example code is in the public domain.
 
  [url]https://www.arduino.cc/en/Tutorial/SerialEvent[/url]
*/
 
String inputString = "";         // a String to hold incoming data
boolean stringComplete = false;  // whether the string is complete
 
void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}
 
void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}
 
/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

 

This is an example of Arduino which uses Serial Interrupt to receive data

You need to be familiar with fundamental concepts of Arduino, if you really expect to help someone.

The code you've posted above does NOT make use of interrupt feature, at least to get bytes on the fly, as would be expected when dealing with high rate receiving (not stated on this thread, presumed on example); particularly this would be useless on the case of more functions running inside the loop() as usual. The example works only when nothing more is needed to run, a very impractical case.

from Arduino website said:
This function is called inside the loop()

Anyway, you were already warned more than once to avoid making assumptions so often. There is no mention of OP of which device is intended to use at the serial. As pointed above, it is missing more details of the project specifications.
 

You could use the millis() function to measure your 2 second window. When 2 seconds is up, it will stop reading serial data. Below is an un-tested code example.


Code:
char dat;
long time_is_up;
char flag = 0;


void setup()
{
  Serial.begin(9600);
  time_is_up = millis(); 
  time_is_up += 2000; // 2000 millis = 2 seconds
}

void loop()
{
   if(flag == 0)                         // run the  checks below each time through the loop if flag = 0
      {
      if( time_is_up > millis() )    // check if 2 seconds has elepsed
         if(Serial.available() > 0)   // if under 2 seconds and if serial data is available  
            dat = Serial.read();      // read the serial data
     
      if( time_is_up < millis() )    // if 2 seconds has passed
         {
         flag = 1;                         // turn the flag off so the checks won't be run any more. 
         Serial.end();                   // turn off Serial here so it is off if millis rolls over after 4,294,967,295 milliseconds.
         }
      }

   // do something with the dat read, or whatever else here.

}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…