Go Down

Topic: Serial time read error[SOLVED]! (Read 548 times) previous topic - next topic

sirus

May 11, 2012, 12:24 pm Last Edit: May 31, 2012, 11:16 am by sirus Reason: 1
Hello guys!I've got a very very big problem!So i would like to give a hand to help me one more time...

I'm newbie so forgive for my mistakes!

Arduino source code:

Code: [Select]

#include <Time.h>
#include <stdio.h>

#define TIME_MSG_LEN 11 // time sync consists of a HEADER followed by ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message

void setup() {
 Serial.begin(9600);
 //Serial.println("Waiting for time sync message");
}
void loop(){
 if(Serial.available() )
 {
   processSyncMessage();
 }
 if(timeStatus()!= timeNotSet)
 {
   // here if the time has been set
   digitalClockDisplay();
 }
 delay(1000);
}
void digitalClockDisplay(){
 // digital clock display of the time
 /*Serial.print(hour());  
 printDigits(minute());
 printDigits(second());*/
 Serial.println(String(hour())+String(printDigits(minute()))+String(printDigits(second())));
 /*Serial.print(" ");
 Serial.print(day());
 Serial.print(" ");
 Serial.print(month());
 Serial.print(" ");
 Serial.print(year());*/
 Serial.println();
}
String printDigits(int digits){
 String str = "";
 String padding = "";
 // utility function for digital clock display: prints preceding colon and leading 0  
 //Serial.print(':');
 padding = ":";
 if(digits < 10){
   //Serial.print('0');
   //Serial.print(digits);
   str = ('0'+String(digits));
 }
 return (padding + str);
}
void processSyncMessage() {
   // if time sync available from serial port, update time and return true
   // time message consists of a header and ten ascii digits
   while(Serial.available() >= TIME_MSG_LEN ){
     char c = Serial.read() ;
     Serial.print(c);
     if( c == TIME_HEADER ) {
       time_t pctime = 0;
       for(int i=0; i < TIME_MSG_LEN -1; i++){
         c = Serial.read();
         if( c >= '0' && c <= '9'){
           pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
         }
      }
      setTime(pctime); // Sync clock to the time received on serial port
    }
  }
}


I want to take a string from time like that!  00:00:00, because should connect this values with a C# application.

Something else:
When i'm sending pulse ( unix time) to synchronize from pc e.g T18384590 etc i'm taking response fine.
but i don't take correct strings to modify them in my application.

The 1st string is e.g 12:
and the other one is 00:00\n\r00:00:00\n\r00:00:00\n\r

Please help me if you can!!!

Thanks

PaulS

It is not clear what your problem is. What, exactly, does the data sent to the Arduino look like?

What issues are you having with the Arduino code?

sirus

I've done an application in C# which sychronize Arduino with computer's time!
e.g I'm sending T123454565 and the arduino returns 12:25:30 something like that...

In this piece of code aduino preview the time into the serialport.
Code: [Select]

void digitalClockDisplay(){
  // digital clock display of the time
  formatHour(hour());
  printDigits(minute());
  printDigits(second());
 
/* Serial.print("- ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();*/
}


My question is how can i get strings like 00:00:00 with correct format, because serialport is sending my formats like :

e.g  1st 12:

2nd 20:31\n\r12:20:32\n\r12:20:33\n\r12:20:34\n\r12:20:35 ... etc!!!Let me explain more if you didn't understand it!!!

Thanks

PaulS

Quote
My question is how can i get strings like 00:00:00 with correct format, because serialport is sending my formats like :

e.g  1st 12:

2nd 20:31\n\r12:20:32\n\r12:20:33\n\r12:20:34\n\r12:20:35 ... etc!!!Let me explain more if you didn't understand it!!!


You are using way too many String instances. The String class is not very efficient, unfortunately, and your extensive use of it is possibly leading to errors when there is no memory left to allocate.

There is nothing that the sketch does that NEEDS to the String class. Get rid of it.

Go Up