3 Replies Latest reply on Nov 17, 2015 6:55 PM by FGT

    The Serial Communication example doesn't work

    AlecH.

      I tested the example "SerialEvent" from Arduino IDE 1.5.3 for Windows,

      and the "serialEvent()" wasn't called when I sent anything from the Serial Monitor.

      Function of "Serial.println()" is normal.

      I also checked the "Serial.available()", and it is always false.

        • 1. Re: The Serial Communication example doesn't work
          arduino_4_life

          Hi AlecH,

          Could you give a little bit more detail please, along with all of the code you used?

           

          Thanks

          Erik.

          • 2. Re: The Serial Communication example doesn't work
            AlecH.

            String inputString = "";         // a string to hold incoming data

            boolean stringComplete = false;  // whether the string is complete

             

             

            void setup() {

              // initialize serial:

              Serial.begin(9600);

              // reserve 200 bytes for the inputString:

              inputString.reserve(200);

            }

             

             

            void loop() {

              // print the string when a newline arrives:

              if (stringComplete) {

                Serial.println(inputString);

                // clear the string:

                inputString = "";

                stringComplete = false;

              }

            }

             

             

            /*

              SerialEvent occurs whenever a new data comes in the

            hardware serial RX.  This routine is run between each

            time loop() runs, so using delay inside loop can delay

            response.  Multiple bytes of data may be available.

            */

            void serialEvent() {

              while (Serial.available()) {

                // get the new byte:

                char inChar = (char)Serial.read();

                // add it to the inputString:

                inputString += inChar;

                // if the incoming character is a newline, set a flag

                // so the main loop can do something about it:

                if (inChar == '\n') {

                  stringComplete = true;

                }

              }

            }

             

            You can find this program from the IDE:

            File > Examples > 04.Communication > SerialEvent

            • 3. Re: The Serial Communication example doesn't work
              FGT

              The example doesn't work because, as explained in other thread/post, serialEvent is not implicitly called each time a serial event occurs. Actually, it does not work as an event handler in Arduino either... but that's another issue (an Arduino one...).

               

              Also: when using the Serial Monitor, the whole idea of "events" in Galileo is "reduced" to exactly one: the "enter" has been pressed. At that time, every char is available (I guess there is some buffer limit, of course, but I didn't check for limits) and the last one is \n. This happens only when the Serial Monitor is set to "New Line", or at least it was the only "consistent" setting in the sense that it worked as expected.

               

              Btw: I got the example to work with

              a) The USB client connected

              b) Adding a call to serialEvent() at the beginning or end of the loop function

              c) The serial monitor set to "New Line"

               

              and, as explained above: all chars are available once the return key has been pressed, not before (i.e. char by char as entered in the keyboard).

               

              HTH,

               

              Fernando.