Pages: [1] 2   Go Down
Author Topic: Serial problem  (Read 737 times)
0 Members and 1 Guest are viewing this topic.
indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hii... I have a problem ,.. smiley-confuse
When the micro-controller performed delay, I sent data through the serial port. But at the sub-program called store run, the data could not be processed.
Does anyone know why??


Here is the listing

* store.txt (9.45 KB - downloaded 14 times.)
* store.txt (9.45 KB - downloaded 5 times.)
« Last Edit: November 28, 2012, 01:26:22 am by alva » Logged

indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have tried another program that apply the principles but with a shorter void read. It works.

Does anybody know why?

#
void setup() {                
Serial.begin(9600);
  pinMode(13, OUTPUT);    
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(5000);              // wait for a second
  read();
  digitalWrite(13, LOW);    // set the LED off
delay(1000);
}

void read()
{
if (Serial.available())
{
 char val = Serial.read();
 Serial.print(val);
}
}



« Last Edit: November 27, 2012, 08:10:01 am by alva » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry, I don't know what you're asking, but your first example screams "use arrays". (And whispers "don't use Strings"))
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  delay(5000);              // wait for a second

Comments should match the code, or just omit them.

Quote
when i send the data while microcontroller running the delay,
micro still processing data after the delay finished

That's how interrupts work.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
String c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).
Logged

indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In both the first and the second program, I sent the data while the micro-processor was delay,
My question is:
Why the data could not be processed in the first program, yet it could be processed in the second program?

« Last Edit: November 28, 2012, 01:27:57 am by alva » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46215
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What i asked about why at the first program the interrupt (Nick said) doesnt work, and at the second program it works.
There are no interrupts in either program, so your question doesn't make sense.

Serial data is received using interrupts, but the interrupt service routine is not code you need to provide. All that the serial interrupt routine does is get a byte and put it in a buffer. It is your responsibility to get that data from the buffer before the buffer gets full, without abusing the poor Arduino with Strings.
Logged

indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To make it better, this is the case:

In the first program, when I sent the data, the arduino was in 'delay' condition. But when the 'delay' condition was over, the data couldn't be processed.

In the second program, when I sent the data, the arduino was in 'delay' condition. But when the 'delay' condition was over, the data could be processed.
Why?
« Last Edit: November 28, 2012, 01:27:29 am by alva » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In the second program, In the first program,
Could you maybe take another look at that last paragraph, and see how it reads?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for grammatical error... try to fix it
« Last Edit: November 28, 2012, 01:27:09 am by alva » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the first program, when I sent the data, the arduino was in 'delay' condition. But when the 'delay' condition was over, the data couldn't be processed.

What do you mean "the data couldn't be processed"? Can you please post what you are sending and what output you see? That is a pretty meaningless description.
Logged

indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At the first program, I sent the following data:

SK,2000,SETLAIN,1,SETGT,1,SETTEMP,1,SETPH,1,SETAT,1,SETLEMBAB,1,SET,1,SETK,1,SETLL,0,

This data was sent when the arduino processed the delay in 5 second in the void kontroler ().
When the ardino has finished running the void kontroler (), it supposed to run void store ().
Normally, the data supposed to be processed in the void store()
and it should be appeared on the serial screen when currentCommand == 20.
The problem: it did not show up in the screen.

My expectation: to see

SK,2000,SETLAIN,1,SETGT,1,SETTEMP,1,SETPH,1,SETAT,1,SETLEMBAB,1,SET,1,SETK,1,SETLL,0,
2000,1,1,1,1,1,1,1,1,0


in the serial screen

A different result happened when I used the second program (after ledpin 13 was active and was doing delay), I sent the data :d.
After the arduino finished with the delay, the data was processed in the void store () and it was successfully appeared in the serial screen.

How did it happen?
Why it gave a different respond?

this is my first program and my second program

* second_program.txt (0.37 KB - downloaded 6 times.)
* first_program.txt (9.35 KB - downloaded 7 times.)
« Last Edit: November 28, 2012, 01:40:01 am by alva » Logged

indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Someone have any idea?? smiley-roll-sweat
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're sending 85 characters and possibly more if you're got a CR or LF on the end. In the more recent versions of the IDE, that's enough to overflow the 64 byte read buffer. If you're early in your delay when the data is sent, some of it will get thrown away so your code will never see the 20th comma that triggers your serial prints of your results.

I'd suggest moving the printing code so that it runs every time you've received a character so you can see what you're getting. Or change the check to print when the currentCommand exceeds 10.

Once you satisfy yourself what's wrong though, you're going to want to ditch both the delays and the use of String; which sadly, looks like practically a complete rewrite. When you do, take a look at strtok - it's one way to parse out your data from a char array.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Someone have any idea?? smiley-roll-sweat

Don't use delay.
Logged

Pages: [1] 2   Go Up
Jump to: