Go Down

Topic: Not receiving data trough serial port (Read 929 times) previous topic - next topic

Arnau3899

Hi,
I am trying to send commands to the Arduino Nano 33 BLE, but something weird is happening and I don't know how to solve it. The serial port is initiated succesfully since it gets to the loop having a "while(!Serial)" on the setup. It prints things trough serial port succesfully but it don't recognise any entering data.

The arduino is connected to my computer with the micro-usb cable. I am testing this with the SerialEvent example, so there are not any code errors. Drivers and IDE succesfully updated. Programming with avrisp mk2.

Thx in advance. :smiley-confuse:

Klaus_K

#1
Apr 27, 2020, 04:18 am Last Edit: Apr 27, 2020, 04:19 am by Klaus_K
SerialEvent is not supported on all Arduino boards. See the following link. The Arduino team likely has not updated the reference page to include the note for the Arduino Nano 33 BLE.

https://www.arduino.cc/reference/en/language/functions/communication/serial/serialevent/

gerrikoio

I'm getting to know my Nano BLE 33, so thought to investigate as a learning by doing exercise.


TL;DR My attempt failed using Serial.attach() method.


I thought to try this by creating a mbed style callback to the SerialEvent function. I simply tried using the Serial.attach method (as seen in USBSerial.h) but I could not get this to work either.

I just added "Serial.attach(&serialEvent);" in the setup routine and it compiles and uploads ok.

I know this is doing something as my serial port locks up fairly quickly when entering characters.

The problem is that I cannot detach the callback while within the serial Event function. I tried a Serial.attach(NULL); but this produces an error, call of overloaded 'attach(NULL)' is ambiguous


Here's the function within the header file:

/**
    * Attach a callback called when a packet is received
    *
    * @param fptr function pointer
    */
   void attach(void (*fptr)(void))
   {
       USBCDC::lock();

       if (fptr != NULL) {
           rx = mbed::Callback<void()>(fptr);
       }

       USBCDC::unlock();
   }

    /**
     * Attach a Callback called when a packet is received
     *
     * @param cb Callback to attach
     */
    void attach(mbed::Callback<void()> &cb)
    {
        USBCDC::lock();

        rx = cb;

        USBCDC::unlock();
    }



So, maybe this is not another way to go about it...

Klaus_K

#3
Apr 28, 2020, 10:54 pm Last Edit: Apr 28, 2020, 10:54 pm by Klaus_K
Here is a very simple solution to your problem. I have just taken the SerialEvent example sketch, moved the serial event code inside loop and modified it a bit.

Code: [Select]
String inputString = "";         // a String to hold incoming data
bool 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() {
  if (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;
    }
  } 
 
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

Using an if() instead of a while() will make sure the sketch behaves very responsive when you add more functionality. The serial is so slow that you can have your sketch do a lot of other things until a new character arrives.

gregIrl

Hi,
I am trying to send commands to the Arduino Nano 33 BLE, ....

The arduino is connected to my computer with the micro-usb cable. I am testing this with the SerialEvent example, so there are not any code errors. Drivers and IDE succesfully updated. Programming with avrisp mk2.

Thx in advance. :smiley-confuse:
Are you sure you have Nano BLE? what do you need avrisp mkw for? NANO BLE is programmed using BOSSAC.EXE program.
Serial - is the USB port, Serrial1 is TTL port, and the issue I had(USB port)  was related to the terminal settings of RTS/CTS or DSR/DTR.
If was helpful, please add Karma, good manners matters.

Go Up