10 Replies Latest reply on Apr 23, 2015 1:32 PM by FGT

    Serial communication not working in arduino

    NithishChauhan

      This is my code................

       

      int led=13;

      int led2=5;

       

       

      char dataBuff[10];

      int count=0;

      boolean b=true;

       

       

       

       

      void setup()

      {

      pinMode(led,OUTPUT);

      pinMode(led2,OUTPUT);

       

       

      Serial.begin(9600);

      delay(100);

       

       

      }

       

       

      void loop()

      {

      digitalWrite(led,LOW);

      digitalWrite(led2,HIGH);

      Serial.write("Waiting for Data");

      Serial.println();

       

       

      delay(1000);

        

          while(b);

        

      digitalWrite(led2,LOW);

      digitalWrite(led,HIGH);    

      delay(1000);

       

       

      Serial.write("The Received Bytes are:");

      Serial.println();

      Serial.write("Count Value");

      Serial.write(count);

      Serial.println();

       

       

      int i=0;

      for(i=0;i<10;i++)

            {

                Serial.write(dataBuff[i]);

                Serial.write("-");

                delay(100);

          

            }

       

       

      while(true);

       

       

       

       

      }

       

       

       

       

      void serialEvent(){

       

        char ch;

          while(Serial.available()>0)

                {

                    ch=(char)Serial.read();

                    if(ch=='*')          // '*' this indicates end of transmission

                        {

                          b=false;

                          break;

                        }

                    if(ch==' ')          // '  ' spaces between bytes of data...

                        {

                            break;

                        }

                     dataBuff[count]=ch;

                      ++count; 

                

                }

       

       

       

       

       

       

      }

       

      i am trying to read 8 bytes of data sent serially with 100ms delay in between trasmission from Atmega16 at 9600 There is no problem in Atmega16 i hav tested it using LED set data is comming as i send them ....

      But the code is not working the arduino is reading data but not storing it in array of char...? where am i going wrong...and the boolean 'b' value does not get effected..i hav tried declaring count as static but no output and even count value is also not displayed ....and also tried having delay in while loop to break after 10s but still the data buffer is empty....it prints data like '----------'.....Plz help me out here....?

        • 1. Re: Serial communication not working in arduino
          DinoT_Intel

          Hi,

           

          How are you connecting the Galileo to the Atmega16?

           

          Are you using pins 0 and 1? If so then you will need to use Serial1

           

          Serial is used mainly for debug only.

          • 2. Re: Serial communication not working in arduino
            NithishChauhan

            Yes i am using 0 an 1...serial1 i did't get...plz explain

            • 3. Re: Serial communication not working in arduino
              DinoT_Intel

              pins 0 and 1 uses the Serial1 object.

               

              In your code replace Serial with Serial1 for non-debug statements.

               

              Serial.available() -> Serial1.available()

              Serial.read() -> Serial1.read()

               

              Also what is the output of your code on the Arduino Serial console?

              I can see few issues on your code that can cause problems.

              It looks like serialEvent() is never called so you never really try to read the serial port

              You have two while statements that are basically infinite loops so the code just gets stuck there forever

              • 4. Re: Serial communication not working in arduino
                NithishChauhan

                Yes i am using the serial terminal option in proteus...

                serialEvent() is an interrupt code means that it supposed to be called when data is available on serial port...

                first while statement is because i don' t want my arduino to do anything till all data comes in...

                and second its just that i hav code to write below but wanted to see whether the code is actually receiving the proper data that i have sent.....that's it...

                its just the piece of code in my project but i am stuck with this....

                and tried Serial1.read() ....its showing error during compilation...'Serial1' was not declared in this scope...

                • 5. Re: Serial communication not working in arduino
                  DinoT_Intel

                  I do not think serialEvent is supported on the Galileo

                   

                  Additionally from the description of SerialEvent() on http://arduino.cc/en/Tutorial/SerialEvent

                  it looks like serialEvent gets run in between each iteration of loop(). So its not really an interrupt.

                  So if you have a blocking while() statment inside loop() it will just be stuck there forever.

                   

                  Have you tried removing the while() statements, and replace the Serial with Serial1 as I mentioned above?

                  Also you will need to add Serial1.begin() inside setup()

                  Also try manually calling serialEvent() inside your loop();

                  • 6. Re: Serial communication not working in arduino
                    NithishChauhan

                    Like u said the serialEvent() was actually never called.....

                    i even wrote like blinking led in serialEvent() but did not worked actually

                    and i also tried Serial1.begin() its showed error for Serial1.begin(9600)....like earlier...

                    but  i got the output i just took that was in serialEvent and attached that to main loop...like this and it worked ...

                        while(b)

                              { 

                                if(Serial.available()){

                                        ch=Serial.read();

                                        //str+=ch;

                                            if(ch=='*')          // '*' this indicates end of transmission

                                                  {

                                                    b=false;

                                                    break;

                                                  }

                                              if(ch==' ')          // '  ' spaces between bytes of data...

                                                  {

                                                     continue;

                                                  }

                                        dataBuff[count]=ch;

                                        ++count;

                                }   

                               

                            }

                    and was able to receive all bytes which i had sent....

                    Thank u sir ....you helped lot in making me think in different way......Thanks a lot

                    • 7. Re: Serial communication not working in arduino
                      NithishChauhan

                      Sir i hav been doing many projects related to 8051, AVR and Arduino ..but i now hav intel galileo the most powerful microcontroller i ever used....My goal is that i want this galileo interfaced with camera connected to USB host ,take images check for a match available in SD card, if match is as required then store this on to SD card ...at the same time run the arduino code in it to perform necessary actions according to that by controlling some devices connected to its gpio pins....the problem is i don't know anything about Yacto image, booting from SD card , how to store image onto SD card, and writing linux code for that....

                      my question is

                      where do i get started with image processing using galileo..?

                      is the knowledge of Linux OS for this is necessary..?

                      is that possible to do within a 1 and half month..?(as for now i am full free)

                      • 8. Re: Serial communication not working in arduino
                        NithishChauhan

                        Sir i hav been doing many projects related to 8051, AVR and Arduino ..but i now hav intel galileo the most powerful microcontroller i ever used....My goal is that i want this galileo interfaced with camera connected to USB host ,take images check for a match available in SD card, if match is as required then store this on to SD card ...at the same time run the arduino code in it to perform necessary actions according to that by controlling some devices connected to its gpio pins....the problem is i don't know anything about Yacto image, booting from SD card , how to store image onto SD card, and writing linux code for that....

                        my question is

                        where do i get started with image processing using galileo..?

                        is the knowledge of Linux OS for this is necessary..?

                        is that possible to do within a 1 and half month..?(as for now i am full free)

                        • 9. Re: Serial communication not working in arduino
                          FGT

                          A little bit late, but just to confirm

                          a) "I do not think serialEvent is supported on the Galileo"

                          and

                          b) "it looks like serialEvent gets run in between each iteration of loop(). So its not really an interrupt."

                           

                          a) The code at Arduino - SerialEvent doesn't work in Galileo (Gen 1) - Win 7.

                          b) A little bit beyond the original question, but related: SerialEvent() does not interrupt the loop{ } code, since trying

                           

                              // wait for...

                             while (!stringComplete);

                           

                             // print the string when a newline arrives:

                             if (stringComplete) {

                          ...

                           

                          (Arduino Uno - Win 7) does nothing at all, SerialEvent() is never called.

                           

                          Fernando.

                          • 10. Re: Serial communication not working in arduino
                            FGT

                            I forgot to mention in my previous post that SerialEvent is reported as tested in  Release Notes (1.0.0) for Arduino (http://www.intel.com/support/galileo/sb/CS-035037.htm) "on at least one of the three supported OSs". Maybe Windows was not one of them for this specific example.

                             

                            Fernando.