Go Down

Topic: Serial problem (Read 958 times) previous topic - next topic

alva

Nov 27, 2012, 08:09 am Last Edit: Nov 28, 2012, 07:26 am by alva Reason: 1
Hii... I have a problem ,.. :~
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

alva

#1
Nov 27, 2012, 08:22 am Last Edit: Nov 27, 2012, 02:10 pm by alva Reason: 1
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);
}
}




AWOL

I'm sorry, I don't know what you're asking, but your first example screams "use arrays". (And whispers "don't use Strings"))
"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.

Nick Gammon


Code: [Select]
  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.

Nick Gammon

Quote
Code: [Select]
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.).

alva

#5
Nov 27, 2012, 09:39 am Last Edit: Nov 28, 2012, 07:27 am by alva Reason: 1
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?


PaulS

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.

alva

#7
Nov 27, 2012, 01:55 pm Last Edit: Nov 28, 2012, 07:27 am by alva Reason: 1
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?

AWOL

Quote
In the second program, In the first program,
Could you maybe take another look at that last paragraph, and see how it reads?
"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.

alva

#9
Nov 27, 2012, 02:19 pm Last Edit: Nov 28, 2012, 07:27 am by alva Reason: 1
Sorry for grammatical error... try to fix it

Nick Gammon


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.

alva

#11
Nov 28, 2012, 07:19 am Last Edit: Nov 28, 2012, 07:40 am by alva Reason: 1
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

alva

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

wildbill

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.

Nick Gammon


Go Up