serial input to arduino

hey i m preety new to arduino.. i m using arduino168 for my project .my problem is i have sent byte from pc using java programming and assembled max232 circuit which is working perfectly fine but i cant figure out the way to take this serial input and view it in arduino.. i m using rs232 to connect arduino with pc. any help

i cant figure out the way to take this serial input and view it in arduino.

Then how do you know it's "working perfectly"?

What input are you using for this RS-232 data?

You can just echo the bytes using Serial.print() and view them with the serial monitor.


Rob

Simple serial code that sends back what it has received.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
      Serial.begin(9600);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {
        char c = Serial.read();
        readString += c;}
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      
      readString="";
      } 
   }

I don't think this works for generating a string:-
char c = Serial.read();
readString += c;}
It will not add a character onto the end of a string like you think.

@zoomkat
You really need to fix this sample code.

        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {

The if test will not be reached unless the while test concluded that there was serial data. Do you suppose that sometime while the Arduino was twiddling it's thumbs the data went away?

The if test will not be reached unless the while test concluded that there was serial data.

Not true. Look it up and find out why!

I don't think this works for generating a string:-
char c = Serial.read();
readString += c;}
It will not add a character onto the end of a string like you think.

Did it not produce a string when you tried it? With my arduino the string I put in the serial monitor text box is sent back to the serial monitor intact.

The if test will not be reached unless the while test concluded that there was serial data. Do you suppose that sometime while the Arduino was twiddling it's thumbs the data went away?

No, the arduino loops faster than the serial input buffer is fulled, and may see the buffer as empty on some loops. Comment out the delay, upload, open the serial monitor, and paste the below string or similar in the serial monitor box, and send it to the arduino. Is the string returned as one string, or is it broken into parts seperated with cr/lf?

qwertyuiopasdfghjklzxcvbnm

No, the arduino loops faster than the serial input buffer is fulled, and may see the buffer as empty on some loops.

This is also wrong.

Serial.available returns -1 when the receive buffer is empty (not zero). So given the OP's code, he will never escape past the while loop.

Not true. Look it up and find out why!

How can the if test get executed if the while test did not determine that there was serial data available?

If the while test found that there was serial data available, how can the delay make it go away?

Where would you expect me to look up these answers?

Mmmm..., I think I'm actually the one that has run the code with and without the delay and evaluated the differences. Those who haven't actually run the code with and without the delay appear to be talking out their *ss. My read on the code is "while" there are characters detected in the serial buffer, the code loops there building the "readString". When no characters are detected in the buffer, the code execution drops out of the while loop and prints the captured readString with a cr/lf attached on the end. When I run the code with no delay, small parts of the origional string are printed with the cr/lf on the end. Unless there is something strange going on, the only way the small parts of the string can be printed is if sometimes when the buffer is being chcked in the while loop, the buffer appears empty. Perhaps somebody can buckle down to the observed behavior and explain it a different way.

Unless there is something strange going on, the only way the small parts of the string can be printed is if sometimes when the buffer is being chcked in the while loop, the buffer appears empty.

Of course that's why the while loop terminates.

The while statement says "continue doing this IF the conditional part is true". My question remains. If the while statement sees that there is data, the delay statement can only result in an increase in the amount of data, not a decrease, since the only way to decrease the amount of data is to read it or flush it, neither of which the delay statement does. So, why is the if statement necessary?

The delay() masks the fact that there is no checking to see that a complete packet has arrived, because, coincidentally, if you wait long enough, the whole packet arrives, and you can pretend that all the data that did arrive constitutes a packet and nothing but a packet.

My question remains. If the while statement sees that there is data, the delay statement can only result in an increase in the amount of data, not a decrease, since the only way to decrease the amount of data is to read it or flush it, neither of which the delay statement does. So, why is the if statement necessary?

Your belief that the delay is used to decrease the amount of data in the buffer is something you have created in your mind and only stated by you. I keep posting that the delay is to allow a short amount of time for all of a reasonibly short string to be put in the input buffer before the individual characters are extracted from the buffer and formed into a string. I find this to be very convienent when doing string testing in the arduino using the serial monitor. If the "if" statrment is not needed, then post the working code that doesn't use it and I will modify my test code accordingly.

The delay() masks the fact that there is no checking to see that a complete packet has arrived, because, coincidentally, if you wait long enough, the whole packet arrives, and you can pretend that all the data that did arrive constitutes a packet and nothing but a packet.

Wow, I think the light is starting to come on! That is all the delay does. It provides a short delay to help ensure the whole string is in the buffer before the buffer is capured into a string. With out the delay even fairly short test strings sent from the serial monitor will be captured in fragments, adding undesired issues into string testing activities.

This really will be the last thing I post on this topic. You have (more than once) posted code that contains:

while(Serial.available())
{
   delay();
   [glow]if(Serial.available() > 0)[/glow]
  {
     // Read the serial data
  }
}

The question that I have asked more than once, and that you have not answered, is what is the purpose of the highlighted statement?

If there is no serial data, the body of the while loop, where the delay statement and if statement are will not be executed.

If there is serial data, the delay statement will be executed. While the delay is going on, the amount of serial data waiting to be read may increase. But, there is no possible way for the amount of serial data waiting to be read to decrease.

So, after the delay is done, why is there a need to test whether there is serial data available to be read?

Your belief that the delay is used to decrease the amount of data in the buffer is something you have created in your mind and only stated by you.

I have never, ever said that the delay could in any way reduce the amount of data available. Quite to the contrary, I have repeatedly said that the amount of data can only increase during the (completely unnecessary) delay.

I have never, ever said that the delay could in any way reduce the amount of data available. Quite to the contrary, I have repeatedly said that the amount of data can only increase during the (completely unnecessary) delay.

You are the only one posting stuff like "If the while test found that there was serial data available, how can the delay make it go away?" Where did you get the idea that the delay would make the data go away? I don't think anybody except you has ever posted any reference to the delay making data go away. Perhaps there is a connection with lunar eclipse. How about it science?

This really will be the last thing I post on this topic. You have (more than once) posted code that contains: if(Serial.available() > 0)
The question that I have asked more than once, and that you have not answered, is what is the purpose of the highlighted statement?

The only reason it is included in the code is the fact that the code works with it included. I really know very little about C coding (only what I see posted in this forum and the arduino help pages), so I have to find code that works and modify it for my needs. I use a lot of code that has components that I can't explain why it is there other than the code won't work without it. If the "if" is not needed, then appropriately modify the code and then post it up, and if it works the same without the "iF", I will modify my test code. Otherwise, "This really will be the last thing I post on this topic." is appropriate.

Guys, please,

  1. the code works with the serial monitor (hint),
  2. not so well with a terminal emulator,
  3. no-one likes a "delay".

Are we agreed?

You are the only one posting stuff like "If the while test found that there was serial data available, how can the delay make it go away?" Where did you get the idea that the delay would make the data go away?

I was pointing out that the only thing that happens between the two conditional tests was a delay statement. I've repeatedly said that the delay statement can not make the data go away. You are the one twisting my question around.

So, I will categorically state that the while test needs a >0 at the end AND that the if test is completely unnecessary.

Merry Christmas.