Hi to all !
I'm working with arduino (uno , mega , due) and I'm using this code to receive datas over the serial port
if(startSerAns==false)
{
int n_bytes=Serial.available();
if(n_bytes > 0)
{
digitalWrite(ledpin,LOW);
startSerAns=true;
for(int i=startPosition;i<startPosition+n_bytes;i++)
{
buf*=Serial.read();
}
}
}
[/sub]
This way is good to receive data and my buffer contains bytes .Then I would like to process this data by checking the first and second byte into the buffer received .
Then , depending by the protocol , I write over the serial
This is the code for answer ...
mytimers.timer[1].Start=startSerAns;
if(mytimers.timer[1].Done==true)
{
_ if(buf[0]=='')_
{
if(buf[1]=='1'){ Serial.println("Risposta 1");}
if(buf[1]=='2'){ Serial.println("Risposta 2");}
if(buf[1]=='3'){ Serial.println("Risposta 3");}
if(buf[1]=='4'){ Serial.println("Risposta 4");}
}
startSerAns=false;
digitalWrite(ledpin,HIGH);
}
Pratically I use my timer library which implements millis() to wait 100 mS before to respond over the serial.When some data arrived to arduino the led switch off , and after a time the led switch on and execute the answer code, the led is using just to show me the correct flow of the program .
If the timer is still counting the control of data incoming is off
But it doesn't work because I must to put atleast a delay(10) between receiving code and println
This is because I had try to use delay timer written by my self .
The software is working weel , the led shows me that , but it only works if I use delay(10) or delay(100) . I tried to use also Serial.Flush() but nothing
This application is real time and it must to be faster I cannot use delay because is locking
Somebody knows how to solve this problem?
Thanks
Walter
I now that arduino needs to wait 1 o10 mS necessary but what to do without delay?
Use millis().
Note the time the message came in, and note the time it is now.
Then
if ((timeNow - startTime)>duration){
// allow 2nd action to occur
}
I'm using millis() , in my library
My library makes the same as millis() , I get millis() and calculate each program scan a delta time and I increase my timers like in automation industries .
I can start TON and TOFF timers and the library works well
Only with serial communication it doesn't work ....using delay() or execute some code or use my library is the same but it doesn'work , only if I use delay() it works ... 
walterp wrote "This way is good to receive data" but I disagree.
The posted code verifies that there is at least one character available, and then possibly reads several characters. No wonder the delay(..) is needed.
If coded properly, delay(..) would NOT be needed. delay(..) is evil and is always suspect. It may be useful in a quickie demo or throwaway program, but it is dangerous in real life.
Code like this:
if(mytimers.timer[1].Done==true)
is suspect as well. I can only see a snippet of the code so I can't see how mytimers and its subordinates were defined, but this should be sufficient:
if(mytimers.timer[1].Done)
Hi .
After a day to debug this issue , I can say that there is something strange when I read bytes from serial
From serial monitor I write "1" and I debug this (in byte format)
42 ---> ''
0
49 ---> '1'
10
Is like there are two bytes I never send ....
ok I have solved this issue , now it's perfect
The problem was the reading of the byte by using while or Serial.Available with for loop
Now it works well and I can write my protocol modbus likely
#include "WTimer.h"
#define ledpin 13 // led on Arduino UNO
#define pb 5 // push button connected on Arduino UNO in pull down configuration
WTimer mytimers; // My library declaration
boolean startSerAns;
byte buf[10];
void setup()
{
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin, LOW);
pinMode(pb,INPUT);
Serial.begin(9600);
Serial.println("Attesa");
startSerAns=false;
mytimers.presetTimer(1,100,TON); // Preset the timer #1 for 100 mS type TON
mytimers.presetTimer(20,5000,TOF); // Preset the timer #20 for 5 seconds, type TOF
mytimers.initTimers(millis());
mytimers.enableClocks();
}
void loop()
{
mytimers.runTimers(millis()); // this method is called every cycle of program
mytimers.enableClocks();
if(Serial.available()>0)
{
Serial.readBytes(buf,2);
startSerAns=true;
digitalWrite(ledpin,LOW); // only for debug to check if serial data are received
}
mytimers.timer[1].Start=startSerAns; // wait 100 mS before respond , no delay locking
if(mytimers.timer[1].Done==true && startSerAns==true)
{
Serial.println(buf[0]); Serial.println(buf[1]); // only for debug to see the buf context
if(buf[0]=='*')
{
if(buf[1]=='1'){ Serial.println("---> 1");}
if(buf[1]=='2'){ Serial.println("---> 2");}
if(buf[1]=='3'){ Serial.println("---> 3");}
if(buf[1]=='4'){ Serial.println("---> 4");}
}
startSerAns=false;
digitalWrite(ledpin,HIGH); // only for debug to check if buf is processed
}
}
I'm using Serial.readBytes(buf,2) and this a best solution to read bytes over serial
ciao
Walter
buf=Serial.read();
what do you expect this line to do ?
I tried to use buf=Serial.read() in a for loop or while loop but it seems the bytes received are different
The Serial.readBytes(buf,2) is perfect
I don't know why , do you know why?